《How To Make A Simple iPhone Game with Cocos2D Tutorial》

在Cocos2D里,屏幕的左下角是(0,0)点,随着你往右上方向移动,x和y值会随之增加。

每当设置一个对象的坐标,默认情况下设置的是该对象自身中心的位置。

-(id) init
{
  if( (self=[super init] )) {
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    CCSprite *player = [CCSprite spriteWithFile:@"Player.png" rect:CGRectMake(0, 0, 27, 40)]; //加载精灵,可省略rect:,让Cocos2d自己处理
    player.position = ccp(player.contentSize.width/2, winSize.height/2);
    [self addChild:player];        
  }
  return self;
}
id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(-target.contentSize.width/2, actualY)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)];
[target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]]; //运行动作
-(void)spriteMoveFinished:(id)sender {
  CCSprite *sprite = (CCSprite *)sender;
  [self removeChild:sprite cleanup:YES]; //移除精灵
}
[self schedule:@selector(gameLogic:) interval:1.0]; //每秒执行
self.isTouchEnabled = YES; //启用触摸
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 
  UITouch *touch = [touches anyObject]; //从touches对象集合中选择一个
  CGPoint location = [touch locationInView:[touch view]];
  location = [[CCDirector sharedDirector] convertToGL:location]; //把此坐标转换为当前屏幕模式适应的

  //...
}

碰撞检测

- (void)update:(ccTime)dt {
 
  NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
  for (CCSprite *projectile in _projectiles) {
    CGRect projectileRect = CGRectMake(
      projectile.position.x - (projectile.contentSize.width/2), 
      projectile.position.y - (projectile.contentSize.height/2), 
      projectile.contentSize.width, 
      projectile.contentSize.height);
 
    NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
    for (CCSprite *target in _targets) {
      CGRect targetRect = CGRectMake(
        target.position.x - (target.contentSize.width/2), 
        target.position.y - (target.contentSize.height/2), 
        target.contentSize.width, 
        target.contentSize.height);
 
      if (CGRectIntersectsRect(projectileRect, targetRect)) {
        [targetsToDelete addObject:target];    //必须将这些对象加入到toDelete数组中,因为无法在当前循环中从数组中删掉
      }                        
    }
 
    for (CCSprite *target in targetsToDelete) {
      [_targets removeObject:target];
      [self removeChild:target cleanup:YES];                                    
    }
 
    if (targetsToDelete.count > 0) {
      [projectilesToDelete addObject:projectile];
    }
    [targetsToDelete release];
  }
 
  for (CCSprite *projectile in projectilesToDelete) {
    [_projectiles removeObject:projectile];
    [self removeChild:projectile cleanup:YES];
  }
  [projectilesToDelete release];
}
[self schedule:@selector(update:)]; //每一帧都执行
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"background-music-aac.caf"]; //播放背景音乐

[[SimpleAudioEngine sharedEngine] playEffect:@"pew-pew-lei.caf"]; //播放音效
[_player release]; //释放内存
_player = nil;

 

posted @ 2013-02-14 11:48  joojoosue  阅读(160)  评论(1编辑  收藏  举报