Game Development Reference
In-Depth Information
- (void)touchesBegan:(NSSet *)touches
withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch
locationInNode:self.scene];
}
Now, using this touchLocation and SpaceShip position, the code decides when to
apply an up or a down action on the spaceship. It also checks the bounds of the screen so
that the spaceship does not move outside the screen.
This is what the code looks like:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent
*)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch
locationInNode:self.scene];
CGPoint spaceShipPosition =
self.spaceShipSprite.position;
CGFloat minYLimitToMove =
SPACE_BG_ONE_TIME_MOVE_DISTANCE;
CGFloat maxYLimitToMove =
self.frame.size.height -
SPACE_BG_ONE_TIME_MOVE_DISTANCE;
if(touchLocation.y > spaceShipPosition.y)
{
if (spaceShipPosition.y < maxYLimitToMove)
{
[self.spaceShipSprite
runAction:self.moveUpAction];
}
}
Search WWH ::




Custom Search