我们如何在Update方法中枚举和修改CCsprite的位置数组?

时间:2015-06-17 13:53:53

标签: cocos2d-iphone ccsprite

我们如何为每个增量时间枚举和更改对象的位置(包含在数组中)?

我把一些CCsprite对象放在数组中,然后我在场景中显示它们,但我也想让它们随着修改更新方法而移动,我在最后一部分失败了。

我该如何解决这个问题?

  @implementation GameScene

{
    Hunter *_hunter;
    Bird *_bird;

    NSMutableArray *_arrayOfBirds;

   }

-(void)update:(CCTime)delta{

    CGSize viewSize = [CCDirector sharedDirector].viewSize;

    float birdSpeed = 50;

    for (Bird *birds in _arrayOfBirds) {
        if (birds.position.x < 0) {
            birds.flipX = YES;
        }
        if (birds.position.x > viewSize.width) {
            birds.flipX = NO;
        }
        float distanceToMove = birdSpeed * delta;
        float direction = birds.flipX ? 1 : -1;
        float newX = birds.position.x + direction * distanceToMove;
        float newY = birds.position.y;
        birds.position = ccp(newX, newY);
    }
}

-(void)addBird{
    CGSize viewSize = [CCDirector sharedDirector].viewSize;

    for (int i=0; i < 4; i++) {

        _bird = [[Bird alloc]initWithBirdType:(i)];
        _bird.position = ccp(viewSize.width * 0.5f + 30 * i , viewSize.height * 0.9f - 15* i);
        [self addChild:_bird];
        [_arrayOfBirds addObject:_bird];
    }
}

1 个答案:

答案 0 :(得分:0)

您忘了初始化数组

这里(假设是ARC)

-(id) init {
      if(self=[super init]) {
          _arrayOfBirds = [[NSMutableArray alloc] init]; 
          // the rest
      }
      return self;
}