SHARE
TWEET

Untitled

a guest Aug 12th, 2017 42 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -(void) fireWeapon{
  2.    
  3.     //NSLog(@"Fired Weapon");
  4.    
  5.     CGSize winSize = [[CCDirector sharedDirector] winSize];
  6.     CCSprite *projectile = [CCSprite spriteWithFile:@"projectile.png" rect:CGRectMake(0, 0, 30, 15)];
  7.    
  8.     int projectileStartX = self.weapon.contentSize.width;
  9.     int projectileStartY = self.weapon.contentSize.height /2 + 3;
  10.    
  11.     projectile.position = ccp(projectileStartX, projectileStartY);
  12.        
  13.     [self.weapon addChild:projectile];
  14.    
  15.     //Determine where we wish to shoot the projectile to
  16.     int realX = winSize.width + (projectile.contentSize.width/2);
  17.    
  18.     if (facing == facingLeft) {
  19.        
  20.         projectile.position = ccp(projectileStartX - projectileStartX, projectileStartY);
  21.         projectile.rotation = 180;
  22.         realX = -realX;
  23.     }
  24.  
  25.     CGPoint realDest = ccp(realX, projectileStartY);
  26.     // Determine the length of how far we're shooting
  27.     int offRealX = realX - projectile.position.x;
  28.     float length = sqrtf((offRealX*offRealX));
  29.     float velocity = 480/1; // 480pixels/1sec
  30.     float realMoveDuration = length/velocity;
  31.    
  32.     // Move projectile to actual endpoint
  33.     [projectile runAction:[CCSequence actions:
  34.                            [CCMoveTo actionWithDuration:realMoveDuration position:realDest],
  35.                            [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
  36.                            nil]];
  37.    
  38. }
  39.  
  40. -(void)spriteMoveFinished:(id)sender {
  41.      NSLog(@"spriteMovedFinished");
  42.     CCSprite *sprite = (CCSprite *)sender;
  43.     [self.weapon removeChild:sprite cleanup:YES];
  44. }
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top