Zombie Conga not working as intended, missing pages?

I’ve followed all the instructions in the book, yet when I run the app ( I’m at page 70), when I tap directly in front of the zombie, he speeds moves WAY too fast. and if I click behind him, he disappears from the view, and can’t be brought back.

I’ve downloaded the source code of the finished app, and it does look different, but nothing of this “new code” is in the pages that I’ve read so far.

How can I fix this odd behaviour of the zombie as described above, the code looks exactly like the one in the book, I have double checked.

Here’s my source code

import SpriteKit

class GameScene: SKScene {

let zombie = SKSpriteNode(imageNamed: "zombie1")
var lastUpdateTime: NSTimeInterval = 0
var dt: NSTimeInterval = 0
let zombieMovePointPerSec: CGFloat = 480.0
var velocity = CGPoint.zero
let playableRect: CGRect

override init(size: CGSize) {
    let maxAspectRatio: CGFloat = 16.0 / 9.0 //1
    let playableHeight = size.width / maxAspectRatio // 3
    let playableMargin = (size.height - playableHeight) / 2.0
    playableRect = CGRect(x: 0, y: playableMargin, width: size.width, height: playableHeight)
    
    super.init(size: size)
}

required init(coder aDecoder: NSCoder) {
    fatalError("Init(coder:) has not been implemented")
}



override func didMoveToView(view: SKView) {
    backgroundColor = SKColor.blackColor()
    
    let background = SKSpriteNode(imageNamed: "background1")
    background.position = CGPoint(x: size.width, y: size.height)
    background.anchorPoint = CGPoint(x: 0.5, y: 0.5) // default
    background.zPosition = -1
    addChild(background)
    
    let mySize = background.size
    print("Size: \(mySize)")
    
    zombie.position = CGPoint(x: 400, y: 400)
    addChild(zombie)
}
// zombie.setScale(2.0)

override func update(currentTime: NSTimeInterval) {
    if lastUpdateTime > 0 {
        dt = currentTime - lastUpdateTime
    } else {
        dt = 0
    }
    lastUpdateTime = currentTime
    print("\(dt*1000) ms since last update")
    moveSprite(zombie, velocity: velocity)
    boundsCheckZombie()
}

func moveSprite(sprite: SKSpriteNode, velocity: CGPoint) {
    let amountToMove = CGPoint(x: velocity.x * CGFloat(dt), y: velocity.y * CGFloat(dt))
    print("Amount to move: \(amountToMove)")
    
    // 2
    sprite.position = CGPoint(
        x: sprite.position.x + amountToMove.x,
        y: sprite.position.y + amountToMove.y)
}

func moveZombieToward(location: CGPoint) {
    let offset = CGPoint(x: location.x - zombie.position.x, y: location.y - zombie.position.y)
    
    let length = sqrt(Double(offset.x * offset.y * offset.y))
    
    let direction = CGPoint(x: offset.x / CGFloat(length), y: offset.y / CGFloat(length))
    
    velocity = CGPoint(x: direction.x * zombieMovePointPerSec, y: direction.y * zombieMovePointPerSec)
}

func sceneTouched(touchLocation: CGPoint) {
    moveZombieToward(touchLocation)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    guard let touch = touches.first else {
        return
    }
    let touchLocation = touch.locationInNode(self)
    sceneTouched(touchLocation)
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    guard let touch = touches.first else {
        return
    }
    
    let touchLocation = touch.locationInNode(self)
    sceneTouched(touchLocation)
}

func boundsCheckZombie() {
    let bottomLeft = CGPointZero
    let topRight = CGPoint(x: size.width, y: size.height)
    
    if zombie.position.x <= bottomLeft.x {
        zombie.position.x = bottomLeft.x
        velocity.x = -velocity.x
    }
    
    if zombie.position.x >= topRight.x {
        zombie.position.x = topRight.x
        velocity.x = -velocity.x
    }
    
    if zombie.position.y <= bottomLeft.y {
        zombie.position.y = bottomLeft.y
        velocity.y = -velocity.y
    }
    
    if zombie.position.y >= topRight.y {
        zombie.position.y = topRight.y
        velocity.y = -velocity.y
    }
    
    
}

}

It might not be the only thing, but this formula (from page 68) isn’t correct:

This one should be

let length = sqrt(Double(offset.x * offset.x + offset.y * offset.y))

Double-check your equations, and see if anything else is different.

1 Like

Oh my, I’m sorry if I came across as rude, it was just this one line of code that messed it all up.

Many, many thanks.