Skip to content

Instantly share code, notes, and snippets.

@grandadmiral-thrawn
Created December 31, 2016 20:26
Show Gist options
  • Save grandadmiral-thrawn/6df0142a2adc3b3f180e286b8c0b79fe to your computer and use it in GitHub Desktop.
Save grandadmiral-thrawn/6df0142a2adc3b3f180e286b8c0b79fe to your computer and use it in GitHub Desktop.
enemy animations from RW tutorials that are not implemented in production
// The RW tutorials with game kit show a variety of useful "enemy" functions. Want a simple place to grab from without opening the PDF later.
func spawnEnemy() {
// basic enemy positioning
let enemy = SKSpriteNode(imageNamed: "enemy")
enemy.position = CGPoint(
x: size.width + enemy.size.width/2,
y: CGFloat.random(
min: playableRect.minY + enemy.size.height/2,
max: playableRect.maxY - enemy.size.height/2))
enemy.setScale(0.3)
// adding to the scene
addChild(enemy)
// how the "enemy" appears and disappears
let actionMove =
SKAction.moveTo(x: -enemy.size.width/2, duration: 2.0)
let actionRemove = SKAction.removeFromParent()
// initialization of the movement and removal
enemy.run(SKAction.sequence([actionMove, actionRemove]))
// this cannot go when the other stuff above is going -- it's a different set of animation. This is for sequenced motion
let enemy = SKSpriteNode(imageNamed: "enemy")
enemy.position = CGPoint(x: size.width + enemy.size.width/2, y: size.height/2)
addChild(enemy)
let actionMidMove = SKAction.moveBy(
x: -size.width/2-enemy.size.width/2,
y: -playableRect.height/2 + enemy.size.height/2,
duration: 1.0)
let actionMove = SKAction.moveBy(
x: -size.width/2-enemy.size.width/2,
y: playableRect.height/2 - enemy.size.height/2,
duration: 1.0)
// 3 make the enemy wait around
let wait = SKAction.wait(forDuration: 0.25)
let logMessage = SKAction.run() {
print("Reached bottom!")
}
// this cannot go when the above is going from the last marker to here. This reverses the animation.
let reverseMid = actionMidMove.reversed()
let reverseMove = actionMove.reversed()
let sequence = SKAction.sequence([
actionMidMove, logMessage, wait, actionMove,
reverseMove, logMessage, wait, reverseMid
])
// this cannot go when the above is gong from the last marker to here. This halves the animation for finer control
let halfSequence = SKAction.sequence(
[actionMidMove, logMessage, wait, actionMove])
let sequence = SKAction.sequence(
[halfSequence, halfSequence.reversed()])
// 4 THIS MUST GO NO MATTER THE ABOVE TO KEEP THE SEQUENCE IN PLACE
// Run sequence of enemy forever or just for this one interval(enemy.run)
let repeatAction = SKAction.repeatForever(sequence)
enemy.run(repeatAction)
enemy.run(sequence)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment