Game Development Reference
In-Depth Information
And the thing that we need to do last is remove the selected property after the touch
has ended:
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
ERGPlayer *player = (ERGPlayer *)[self
childNodeWithName:playerName];
if (player.selected) {
player.selected = NO;
}
}
Now, if you build and run the project, you will be able to drag the character sprite
around. Another option to consider is to move the score label around the screen to
the position that you would like.
You might have noticed that we are checking the view hierarchy each time we want
to grab a pointer to the player. This is not the optimal way to do it. Another option
to consider is using a player as a property of the scene. This looks like a fast and
easy way to do it, but consider that you might have a few objects on screen, or even
dozens of them that need interaction. Having 20 properties for this reason does not
seem feasible.
You should also know that traversing a node tree is not a very fast or efficient
operation. Imagine that you have 1,000 nodes. If you need to find a node by name
and it is not there, the code will traverse each of these nodes as it needs to make
sure that there is none with your name.
This traversal might happen a few times during just one frame calculation, and since
this happens many times per second, you might get a very real slowdown from this.
You might want to cache certain nodes in weak pointers in case you need them later.
Usually, you might want to avoid many node tree traversals.
Using gesture recognizers
Gesture recognizers allow us to not bother with the low-level code that was
explained earlier in this chapter. Without gesture recognizers, certain things
might be extremely hard to implement, such as pinching in and out or rotating.
Thankfully, Apple has handled all of this for us.
We might want to increase and decrease the speed of scrolling for testing reasons,
so we will implement this feature.
 
Search WWH ::




Custom Search