Last active
January 29, 2016 12:36
-
-
Save lucasprag/cdb9213c16a2e1742e7b to your computer and use it in GitHub Desktop.
Spaw enemies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// omitted | |
class GameScene: SKScene { | |
// omitted | |
var enemies = [SKSpriteNode]() | |
override func update(delta: CFTimeInterval) { | |
// loop all enemies and make then walk to collide with the player | |
for enemy in enemies { | |
enemy.position = CGPoint(x: Double(enemy.position.x) - 5, y: Double(enemy.position.y)) | |
} | |
spawEnemy() | |
} | |
func spawEnemy(){ | |
// create the sprite node the red square | |
let enemy = SKSpriteNode(imageNamed: "red") | |
// define a name to the node | |
enemy.name = "enemy" | |
// define a size equal of the player | |
enemy.size = CGSize(width: 50, height: 50) | |
// define the position to be outside of the screen | |
enemy.position = CGPoint(x: CGRectGetMidX(self.frame) + 350, y: CGRectGetMidY(self.frame) - 20) | |
// create a physics body with the size of the player | |
enemy.physicsBody = SKPhysicsBody(rectangleOfSize: enemy.size) | |
// affected by gravity as the player | |
enemy.physicsBody?.affectedByGravity = true | |
// make it collide with the floor and the player | |
enemy.physicsBody?.categoryBitMask = Physics.Character | |
enemy.physicsBody?.collisionBitMask = Physics.Character | |
// add the enemy to the scene | |
addChild(enemy) | |
// store the enemy to use later | |
enemies.append(enemy) | |
} | |
// omitted | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment