ZombieCogna no errors, won't run?

(New to programming, self learning.). I have gotten to the point in which the zombie is suppose to move by touches on the screen but it is not. The FPS is changing from the amount I should be moving in my console… here is my movement code as shown directly the same from the book what is wrong???

override func update(_ currentTime: CFTimeInterval) {
if lastUpdateTime > 0 {
dt = currentTime - lastUpdateTime // dt defined
} else {
dt = 0 }
lastUpdateTime = currentTime
print(“(dt*1000) milleseconds since last update”)

    move(sprite: zombie, velocity: velocity)
    
}


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

func sceneTouched(touchLocation:CGPoint) {
    moveZombieToward(location: touchLocation)
}
override func touchesBegan(_ touches: Set<UITouch>,
                           with event: UIEvent?) {
    guard let touch = touches.first else {
        return
    }
    let touchLocation = touch.location(in: self)
    sceneTouched(touchLocation: touchLocation)
}
override func touchesMoved(_ touches: Set<UITouch>,
                           with event: UIEvent?) {
    guard let touch = touches.first else {
        return
    }
    let touchLocation = touch.location(in: self)
    sceneTouched(touchLocation: touchLocation)
} 

func moveZombieToward(location: CGPoint) {
    let offset = CGPoint(x: location.x - zombie.position.x,
                         y: location.y - zombie.position.y) //creates how much they differr
    let length = sqrt(
        Double(offset.x * offset.x + offset.y * offset.y)) //A^2 + b^2 = c^2, c = √a^2 + b^2 // draws mathmatically vector direction line

    
    let direction = CGPoint(x: offset.x / CGFloat(length),
                            y: offset.y / CGFloat(length))



    velocity = CGPoint(x: direction.x * zombieMovePointsPerSec,
                       y: direction.y * zombieMovePointsPerSec) //distance with a speed to make action.

    //12/14 does not work up to the point of "touches should work."
    
    
}

}

Hi @jonahprogramma

I would suggest you to take a look at the velocity parameter of the ‘func move(sprite:…’, try to print it out to the console, maybe it is always 0 or near that.
Also make sure that ‘zombie’ variable is storing the actual zombie sprite and not something else.

Hope this is helpful

Nikita

This topic was automatically closed after 166 days. New replies are no longer allowed.