Advertisement
Guest User

COCOS2D Scoring Help (stackoverflow.com)

a guest
Aug 15th, 2012
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Import the interfaces
  2. #import "HelloWorldLayer.h"
  3. #import "SimpleAudioEngine.h"
  4. #import "GameOverScene.h"
  5.  
  6. // HelloWorldLayer implementation
  7.  
  8. NSMutableArray *_targets;
  9. NSMutableArray *_projectiles;
  10.  
  11. @implementation HelloWorldLayer
  12.  
  13. +(CCScene *) scene
  14. {
  15.     // 'scene' is an autorelease object.
  16.     CCScene *scene = [CCScene node];
  17.    
  18.     // 'layer' is an autorelease object.
  19.     HelloWorldLayer *layer = [HelloWorldLayer node];
  20.    
  21.     // add layer as a child to scene
  22.     [scene addChild: layer];
  23.    
  24.     // return the scene
  25.     return scene;
  26. }
  27. -(void)spriteMoveFinished:(id)sender {
  28.        CCSprite *sprite = (CCSprite *)sender;
  29.     [self removeChild:sprite cleanup:YES];
  30.         if (sprite.tag == 1) { // target
  31.         [_targets removeObject:sprite];
  32.     } else if (sprite.tag == 2) { // projectile
  33.         [_projectiles removeObject:sprite];
  34.     }
  35. }
  36. -(void)addTarget {
  37.    
  38.     CCSprite *target = [CCSprite spriteWithFile:@"sprout.png"
  39.                                            rect:CGRectMake(0, 0, 40, 40)];
  40.    
  41.     target.tag = 1;
  42.     [_targets addObject:target];
  43.     // Determine where to spawn the target along the Y axis
  44.     CGSize winSize = [[CCDirector sharedDirector] winSize];
  45.     int minY = target.contentSize.height/2;
  46.     int maxY = winSize.height - target.contentSize.height/2;
  47.     int rangeY = maxY - minY;
  48.     int actualY = (arc4random() % rangeY) + minY;
  49.    
  50.     // Create the target slightly off-screen along the right edge,
  51.     // and along a random position along the Y axis as calculated above
  52.     target.position = ccp(winSize.width + (target.contentSize.width/2), actualY);
  53.     [self addChild:target];
  54.    
  55.     // Determine speed of the target
  56.     int minDuration = 1.5;
  57.     int maxDuration = 4.0;
  58.     int rangeDuration = maxDuration - minDuration;
  59.     int actualDuration = (arc4random() % rangeDuration) + minDuration;
  60.    
  61.     // Create the actions
  62.     id actionMove = [CCMoveTo actionWithDuration:actualDuration
  63.                                         position:ccp(-target.contentSize.width/2, actualY)];
  64.     id actionMoveDone = [CCCallFuncN actionWithTarget:self
  65.                                              selector:@selector(spriteMoveFinished:)];
  66.     [target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
  67.  
  68.    
  69. }
  70. - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  71.     UITouch *touch = [touches anyObject];
  72.     CGPoint location = [touch locationInView:[touch view]];
  73.     location = [[CCDirector sharedDirector] convertToGL:location];
  74.     //
  75.     [[SimpleAudioEngine sharedEngine] playEffect:@"pew.wav"];
  76.     //
  77.     CGSize winSize = [[CCDirector sharedDirector] winSize];
  78.     CCSprite *projectile = [CCSprite spriteWithFile:@"projectile.png"
  79.                                                rect:CGRectMake(0, 0, 20, 5)];
  80.     projectile.position = ccp(20, winSize.height/2);
  81.     //
  82.     int offX = location.x - projectile.position.x;
  83.     int offY = location.y - projectile.position.y;
  84.     //
  85.     if (offX <= 0) return;    
  86.     //
  87.     [self addChild:projectile];
  88.     projectile.tag = 2;
  89.     [_projectiles addObject:projectile];
  90.     //
  91.     int realX = winSize.width + (projectile.contentSize.width/2);
  92.     float ratio = (float) offY / (float) offX;
  93.     int realY = (realX * ratio) + projectile.position.y;
  94.     CGPoint realDest = ccp(realX, realY);
  95.     //
  96.     int offRealX = realX - projectile.position.x;
  97.     int offRealY = realY - projectile.position.y;
  98.     float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
  99.     float velocity = 800/1; // 480pixels/1sec
  100.     float realMoveDuration = length/velocity;
  101.     //
  102.     [projectile runAction:[CCSequence actions:
  103.                            [CCMoveTo actionWithDuration:realMoveDuration position:realDest],
  104.                            [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
  105.                            nil]];
  106. }
  107. - (void)update:(ccTime)dt {
  108.     NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
  109.     for (CCSprite *projectile in _projectiles) {
  110.         CGRect projectileRect = CGRectMake(
  111.                                            projectile.position.x - (projectile.contentSize.width/2),
  112.                                            projectile.position.y - (projectile.contentSize.height/2),
  113.                                            projectile.contentSize.width,
  114.                                            projectile.contentSize.height);
  115.        
  116.         NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
  117.         for (CCSprite *target in _targets) {
  118.             CGRect targetRect = CGRectMake(
  119.                                            target.position.x - (target.contentSize.width/2),
  120.                                            target.position.y - (target.contentSize.height/2),
  121.                                            target.contentSize.width,
  122.                                            target.contentSize.height);
  123.            
  124.             if (CGRectIntersectsRect(projectileRect, targetRect)) {
  125.                 [targetsToDelete addObject:target];            
  126.             }                      
  127.         }
  128.        
  129.         for (CCSprite *target in targetsToDelete) {
  130.             [_targets removeObject:target];
  131.             [self removeChild:target cleanup:YES];                                 
  132.         }
  133.                 if (targetsToDelete.count > 0) {
  134.             [projectilesToDelete addObject:projectile];
  135.         }
  136.         [targetsToDelete release];
  137.     }
  138.    
  139.     for (CCSprite *projectile in projectilesToDelete) {
  140.         [_projectiles removeObject:projectile];
  141.         [self removeChild:projectile cleanup:YES];
  142.     }
  143.     [projectilesToDelete release];
  144. }
  145. // on "init" you need to initialize your instance
  146. -(id) init
  147. {
  148.    
  149.     if( (self=[super init] )) {
  150.         [self schedule:@selector(update:)];
  151.  
  152.         CGSize winSize = [[CCDirector sharedDirector] winSize];
  153.         CCSprite *player = [CCSprite spriteWithFile:@"player.png"
  154.                                                rect:CGRectMake(0, 0, 40, 40)];
  155.         player.position = ccp(player.contentSize.width/2, winSize.height/2);
  156.         [self addChild:player];
  157.         //
  158.         self.isTouchEnabled = YES;
  159.         [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"techno_loop.wav"];
  160.         _targets = [[NSMutableArray alloc] init];
  161.         _projectiles = [[NSMutableArray alloc] init];
  162.         [self schedule:@selector(update:)];
  163.  
  164.        
  165.     }
  166.     [self schedule:@selector(gameLogic:) interval:1.0];
  167.     return self;
  168. }
  169. -(void)gameLogic:(ccTime)dt {
  170.     [self addTarget];
  171. }
  172. // on "dealloc" you need to release all your retained objects
  173. - (void) dealloc
  174. {
  175.     // in case you have something to dealloc, do it in this method
  176.     // in this particular example nothing needs to be released.
  177.     // cocos2d will automatically release all the children (Label)
  178.    
  179.     // don't forget to call "super dealloc"
  180.     [_targets release];
  181.     _targets = nil;
  182.     [_projectiles release];
  183.     _projectiles = nil;
  184.  
  185.     [super dealloc];
  186. }
  187. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement