Game Development Reference
In-Depth Information
How to do it
Following are the steps involved in making a tank move on touch and also sync on a re-
mote device by sending and receiving packets:
1. On touching the screen, these methods of SKScene are called, which will be used
to move the tank in the local device.
-(void)touchesBegan:(NSSet *)touches
withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches
withEvent:(UIEvent *)event
-(void)touchesEnded:(NSSet *)touches
withEvent:(UIEvent *)event
2. The first method has already been implemented. Let's add some updated lines of
tank info on touch begins when the game state is kGameStatePlaying as
shown in the following:
UITouch *thumb = [[event allTouches] anyObject];
CGPoint thumbPoint = [thumb locationInNode:self];
// hold to move, second finger to fire
if(thumb.tapCount==0) {
tankStatsForLocal.tankDestination = thumbPoint;
tankStatsForLocal.tankDirection = atan2(
thumbPoint.y - tankStatsForLocal.tankPosition.y,
thumbPoint.x - tankStatsForLocal.tankPosition.x ) -
(M_PI/2.0);
// keep us 0-359
if(tankStatsForLocal.tankDirection < 0)
tankStatsForLocal.tankDirection += (2.0*M_PI);
else if(tankStatsForLocal.tankDirection >
(2.0*M_PI))
tankStatsForLocal.tankDirection -= (2.0*M_PI)
[self updateLocalTank];
}
3. In this piece of code from the event received by the touch method, a UITouch
object is fetched and, using that, the location of touch is determined with respect to
Search WWH ::




Custom Search