使CCSprite转向其他CCSprite

时间:2014-01-04 16:15:37

标签: objective-c cocos2d-iphone ccsprite

我知道之前已经问了很多,但我找不到解决办法。屏幕上有一个CCSprite,播放器,用加速度计控制。然后在屏幕顶部,其他CCSprites每2秒产生一次敌人。我希望所有的敌人跟随玩家,如果玩家移动玩家,敌人应该改变方向并朝着CCSprite移动。这是我的代码:

- (void)spawnEnemies
{
    monsterTxt = [[CCTexture2D alloc] initWithCGImage:[UIImage imageNamed:@"obj.png"].CGImage resolutionType:kCCResolutionUnknown];
    monster = [CCSprite spriteWithTexture:monsterTxt];

    ... 
    //random spawn position etc.


     CCMoveTo *movemonster = [CCMoveTo actionWithDuration:7.0 position:ccp(_rocket.boundingBox.origin.x, _rocket.boundingBox.origin.y)];
    [monster runAction:[CCSequence actions:movemonster, nil]];

    [_monsters addObject:monster];  //adds the sprite to a mutable array                
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    ...
    //determines new position and move sprite there

    [monster stopAllActions];

    CCMoveTo *movemonster = [CCMoveTo actionWithDuration:7.0 position:ccp(_rocket.boundingBox.origin.x, _rocket.boundingBox.origin.y)];
    [monster runAction:[CCSequence actions:movemonster, nil]];

}

现在当我开始游戏时,精灵正朝向玩家,但当玩家移动时,敌人不会更新他们的目的地,他们只会继续向下并停在玩家的y坐标处。过了一会儿,应用程序崩溃了。我做错了什么?

1 个答案:

答案 0 :(得分:0)

我的猜测是,问题可能出在您未发布的代码段中。

在Cocos2d的v2.1中,要在您的图层中接收加速度计测量值:

在init或onEnterTransitionDidFinish调用中执行此操作:

   self.accelerometerEnabled = YES;

重载is方法:

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
   CCLOG(@"Acceleration (%f,%f,%f)",
         acceleration.x,
         acceleration.y,
         acceleration.z);
   // Do something meaningful...you probably want to send a notification to
   // some other scene/layer/logic.  It should cache that the acceleration value
   // and then do something on an update(...) call with it.  Then reset it so that
   // it does not get used the next update cycle untila new value comes in (here).
}

看来你有这个...而且你已经表示你正在接收它们......所以我有点困惑。

SO(SANITY CHECK TIME)...

我创建了一个示例项目(here),它有一个玩家精灵被多个怪物精灵追逐。当用户触摸屏幕时(我没有使用加速度计),玩家精灵改变位置,精灵“追逐”他。我在他们的目标位置添加了一些随机偏移量,因此它们不会全部聚集在玩家之上。

这是该文件(最重要的部分)的代码,它是一个cocos2d v2.1项目附带的HellowWorldLayer的修改版本(我是从模板创建的):

-(void)createPlayer
{
   playerMoved = NO;

   if(self.player != nil)
   {
      [self removeChild:player];
      self.player = nil;
   }
   CGSize scrSize = [[CCDirector sharedDirector] winSize];
   CCSprite* playerSprite = [CCSprite spriteWithFile:@"Icon.png"];
   playerSprite.scale = 2.0;
   playerSprite.position = ccp(scrSize.width/2,scrSize.height/2);
   [self addChild:playerSprite];
   self.player = playerSprite;
}

-(void)createMonsters
{
   const int MAX_MONSTERS = 4;
   if(self.monsters.count > 0)
   {  // Get rid of them
      for(CCSprite* sprite in monsters)
      {
         [self removeChild:sprite];
      }
      [self.monsters removeAllObjects];
   }
   CGSize scrSize = [[CCDirector sharedDirector] winSize];
   for(int idx = 0; idx < MAX_MONSTERS; idx++)
   {
      CCSprite* sprite = [CCSprite spriteWithFile:@"Icon.png"];
      float randomX = (arc4random()%100)/100.0;
      float randomY = (arc4random()%100)/100.0;
      sprite.scale = 1.0;
      sprite.position = ccp(scrSize.width*randomX,scrSize.height*randomY);
      [self addChild:sprite];
      [monsters addObject:sprite];
   }
}

-(void)update:(ccTime)delta
{
   if(playerMoved)
   {  // Modify all the actions on all the monsters.
      CGPoint playerPos = player.position;
      for(CCSprite* sprite in monsters)
      {
         float randomX = (1.0)*(arc4random()%50);
         float randomY = (1.0)*(arc4random()%50);
         CGPoint position = ccp(playerPos.x+randomX,playerPos.y+randomY);
         [sprite stopAllActions];
         [sprite runAction:[CCMoveTo actionWithDuration:3.0 position:position]];
      }
      playerMoved = NO;
   }
}

我有一个CCSprite *对象(怪物)的保留数组和玩家的CCSprite *。我有一个标志告诉我玩家是否改变了位置(玩家移动)。在更新循环中,如果玩家已移动,请停止对怪物的所有操作并更新它们。

在屏幕上看起来像这样:

enter image description here

然后当我移动主精灵......

enter image description here

他们遵循它......

enter image description here

这有用吗?