staticinlineCGPointrwAdd(CGPointa,CGPointb){returnCGPointMake(a.x+b.x,a.y+b.y);}staticinlineCGPointrwSub(CGPointa,CGPointb){returnCGPointMake(a.x-b.x,a.y-b.y);}staticinlineCGPointrwMult(CGPointa,floatb){returnCGPointMake(a.x*b,a.y*b);}staticinlinefloatrwLength(CGPointa){returnsqrtf(a.x*a.x+a.y*a.y);}// Makes a vector have a length of 1staticinlineCGPointrwNormalize(CGPointa){floatlength=rwLength(a);returnCGPointMake(a.x/length,a.y/length);}
-(void)touchesEnded:(NSSet*)toucheswithEvent:(UIEvent*)event{// 1 - Choose one of the touches to work withUITouch*touch=[touchesanyObject];CGPointlocation=[touchlocationInNode:self];// 2 - Set up initial location of projectileSKSpriteNode*projectile=[SKSpriteNodespriteNodeWithImageNamed:@"projectile"];projectile.position=self.player.position;// 3- Determine offset of location to projectileCGPointoffset=rwSub(location,projectile.position);// 4 - Bail out if you are shooting down or backwardsif(offset.x<=0)return;// 5 - OK to add now - we've double checked position[selfaddChild:projectile];// 6 - Get the direction of where to shootCGPointdirection=rwNormalize(offset);// 7 - Make it shoot far enough to be guaranteed off screenCGPointshootAmount=rwMult(direction,1000);// 8 - Add the shoot amount to the current position CGPointrealDest=rwAdd(shootAmount,projectile.position);// 9 - Create the actionsfloatvelocity=480.0/1.0;floatrealMoveDuration=self.size.width/velocity;SKAction*actionMove=[SKActionmoveTo:realDestduration:realMoveDuration];SKAction*actionMoveDone=[SKActionremoveFromParent];[projectilerunAction:[SKActionsequence:@[actionMove,actionMoveDone]]];}