Group Group Group Group Group Group Group Group Group

What do these methods mean?

#1

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)
}

#2

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 https://developer.apple.com/documentation/uikit/uiresponder/1621142-touchesbegan

cheers,

Jayant

1 Like
closed #3

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