Game Development Reference
In-Depth Information
-(void)touchesMoved:(NSSet*)touches
withEvent:(UIEvent*)event {
if (self.isPaddleTapped) {
// 2 Get touch location
UITouch* touch = [touches anyObject];
CGPoint touchLocation = [touch
locationInNode:self];
CGPoint previousLocation = [touch
previousLocationInNode:self];
// 3 Get node for paddle
SKSpriteNode* paddle = (SKSpriteNode*)[self
childNodeWithName: paddleCategoryName];
// 4 Calculate new position along x for paddle
int paddleX = paddle.position.x +
(touchLocation.x - previousLocation.x);
// 5 Limit x so that the paddle will not
leave the screen to left or right
paddleX = MAX(paddleX, paddle.size.width/2);
paddleX = MIN(paddleX, self.size.width -
paddle.size.width/2);
// 6 Update position of paddle
paddle.position = CGPointMake(paddleX,
paddle.position.y);
}
}
Initially, check if the paddle is tapped. If yes, then update the paddle position
based on the user's touch location. While repositioning, we just need to make sure
that the position y of the paddle does not change.
9. Finally add the following function after touchedMoved in the GameScene.m
file:
-(void)touchesEnded:(NSSet*)touches
withEvent:(UIEvent*)event {
self.isPaddleTapped = NO;
}
Just turn the isPaddleTapped flag off in the touch end function.
Search WWH ::




Custom Search