Skip to content

Commit

Permalink
action
Browse files Browse the repository at this point in the history
  • Loading branch information
superMonsterc committed Jan 15, 2019
1 parent 62390db commit 31711dc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions mygame/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,13 @@ extension CGPoint {
return atan2(y, x)
}
}

extension CGFloat {
static func random() -> CGFloat {
return CGFloat(Float(arc4random()) / Float(UInt32.max))
}
static func random(min: CGFloat, max: CGFloat) -> CGFloat {
assert(min < max)
return CGFloat.random() * (max - min) + min
}
}
32 changes: 32 additions & 0 deletions mygame/ZombieScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,40 @@ class ZombieScene: SKScene {
addChild(zombie1)

debugDrawPlayableArea()

spawnEnemy()
}

func spawnEnemy() {
let enemy = SKSpriteNode(imageNamed: "enemy")
enemy.position = CGPoint(x: size.width - enemy.size.width/2, y: size.height/2)
addChild(enemy)

// 1

let actionMidMove = SKAction.moveBy(x: -size.width/2-enemy.size.width/2, y: -playableRect.height/2 + enemy.size.height/2, duration: 1)

// 2
let actionMove = SKAction.moveBy(
x: -size.width/2-enemy.size.width/2,
y: playableRect.height/2 - enemy.size.height/2,
duration: 1.0)

let logMessage = SKAction.run {
print("Reached bottom!")
}

let waitAction = SKAction.wait(forDuration: 2)

let reverseMid = actionMidMove.reversed()
let reverseMove = actionMove.reversed()

// 3
let sequence = SKAction.sequence([waitAction, actionMidMove, logMessage, waitAction, actionMove, reverseMove, logMessage, waitAction, reverseMid])

// 4
enemy.run(SKAction.repeatForever(sequence))
}

override func update(_ currentTime: TimeInterval) {
// zombie1.position = CGPoint(x: zombie1.position.x+8, y: zombie1.position.y)
Expand Down

0 comments on commit 31711dc

Please sign in to comment.