NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

100 days of Swift - Day 63 - Project 17 - Space Race - Challenge 1

Forums > 100 Days of Swift

@teine  

Sorry to say I'm befuddled by the first challenge, did the other 2 no problem, but I'm really not sure what your asking for.

  1. Stop the player from cheating by lifting their finger and tapping elsewhere – try implementing touchesEnded() to make it work.

TouchesEnded - happens when the user picks up their finger...

Is this about event bubbling? Is the idea to cancel/handle the event so it doesn't run touches moved? ok hint plz.

Or just explode the spaceship if they lift their finger? That seems rather punishing and unintuitive.

The cheat to me, it seems to happen on touchesBegan/Moving, where it doesn't check that the user actually tapped the spaceship.. (a check nicely shown in Proj 14)

I'm really at a loss at what I should be understanding in touchesEnded. At touchEnded, it's already moved the space ship... I could move the update to position code to Ended, but then it would only update on finger lifted

i must be going down a blind alley, please help me back on track, thanks.

   

Not sure how many hints do you want :-) But the key is to implement both touchesBegan and touchesEnded when checking if the player is indeed touching the spaceship and not trying to cheat.

   

@teine  

huh, thanks will do that :D

So there's no special "one-liner hack" for touchesEnded, i was missing?

   

@teine  

I ended up sticking with touchesMoved, using excepts from Paul's code for node in nodes(at:touch.location(in self)) { if node == player { //update player.position

Is that bad from from a performance angle? The above code seemed clear & consise to me...

Using touchesBegan/Ended seemed cluttered, and possibly bad for multitouch?

if it had a bool touchedShip set on tBegan, update ship's position if touchedShip is true in tMoved, the set it to false in tEnded...

but... how well would that handle leaving a finger on the screen, then add a new touch, or multi touch? I may be over thinking things... it just seemed to complicate things in this case.

   

I think your approach is perfectly viable, what I did was a very similar thing:

  1. I created var isPlayerTouched: Bool
  2. in touchesBegan - if the touched node is the ship: isPlayerTouched = true.
  3. in touchesEnded - if the touched node is the ship: isPlayerTouched = false.

That way, I can control when the ship can be moved and when not. So in my case, touchesMoved works with the ship node only when isPlayerTouched = true.

EDIT: Ah, now I remember. My first approach was identical to yours. However, I noticed that my finger was 'losing' the ship when I was moving it very fast. The above method got rid of all the problems.

   

Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.