How do we enumerate and modify positions array of CCsprite inside Update method?

45 views Asked by At

How do we enumerate and alter our object's position (contained in array) for each delta time?

I put some CCsprite objects inside array, then I displayed them in scene, but also I wanted to make them move with modifying update method, I failed on last part.

How do I get around this ?

  @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

There are 1 answers

0
YvesLeBorg On BEST ANSWER

You forgot to initialize your array

here (assuming ARC)

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