What do these methods mean?

Can somebody explain me?How all these methods work.And what do they mean?It is part from game Zombie Conga.Or can you give me links to the apple documentation.

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

Hi @misterdave,
The code you refer to below:

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

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

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

There are three functions, of which the touchesBegan and the touchesMoved are system events where as the sceneTouched is a user defined function.

So what it does is when the user touches the screen, a touchesBegan event is triggered, this tells the system that a point on the screen was touched. However with multitouch, there could be more than 1 finger that touches the screen, so this takes the first one only (for simplicity) and determines the point in the view from that touch and then calls the sceneTouched function which calls the moveZombieToward function which moves the zombie towards the point that was touched on the screen.

You can read up more on this with the Apple documentation found at Apple Developer Documentation

cheers,

Jayant

1 Like

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