views:

618

answers:

1

I have birds flying within a frame in my game, but I can only get them to fly in two different directions. If there are 2 birds they go in two different directions. If there are 3 birds, 2 of them go in one direction and the other one goes in a different direction. I want the birds to randomly go in four different directions. Up right, down right, up left and down left, here is my code.

-(void) AddBirdIntoArray: (int) BirdCount {
 for(int i=0; i< BirdCount ; i++){


  if(appDelegate.enemyselect == 0){

  imgBird[i]=[[UIImageView alloc] initWithImage:firstImage];
  [imgBird[i] setAnimationImages:birdArrayConstant];
  }

  else if(appDelegate.enemyselect == 1){

   imgBird[i]=[[UIImageView alloc] initWithImage:firstImagegreenorange];
   [imgBird[i] setAnimationImages:birdArrayConstant3];
  }

  else if(appDelegate.enemyselect == 2){
   imgBird[i]=[[UIImageView alloc] initWithImage:firstImageblueyellow];
   [imgBird[i] setAnimationImages:birdArrayConstant4];
  }

  else if(appDelegate.enemyselect == 3){
   imgBird[i]=[[UIImageView alloc] initWithImage:firstImagebluewhite];
   [imgBird[i] setAnimationImages:birdArrayConstant2];
  }

  else if(appDelegate.enemyselect == 4){
   imgBird[i]=[[UIImageView alloc] initWithImage:firstImagepinkpurple];
   [imgBird[i] setAnimationImages:birdArrayConstant5];
  }

  else if(appDelegate.enemyselect == 5){
   imgBird[i]=[[UIImageView alloc] initWithImage:firstImagebluegreen];
   [imgBird[i] setAnimationImages:birdArrayConstant6];
  }

  else if(appDelegate.enemyselect == 6){
   imgBird[i]=[[UIImageView alloc] initWithImage:firstImageorangewhite];
   [imgBird[i] setAnimationImages:birdArrayConstant7];
  }

  else if(appDelegate.enemyselect == 7){
   imgBird[i]=[[UIImageView alloc] initWithImage:firstImageredblue];
   [imgBird[i] setAnimationImages:birdArrayConstant8];
  }



  [imgBird[i] setAnimationDuration:1.0];
  [imgBird[i] startAnimating];

  if(i%2==0){
   pos[i]=CGPointMake(-1,1);
  }

  else{
   pos[i]=CGPointMake(1,-1);
  }

  xvalue = arc4random()%250;
  yvalue = arc4random()%250;
  CGRect TempRect = CGRectMake(xvalue ,yvalue , 22 , 22);
  imgBird[i].frame = TempRect;
  [birdImageViewArray addObject:imgBird[i]];
  [self addSubview:imgBird[i]];
  [imgBird[i] release];
  }




 [birdArray release];
}
+1  A: 

I think these are your two direction vectors:

pos[i]=CGPointMake(-1,1);
pos[i]=CGPointMake(1,-1);

The other two directions are:

pos[i]=CGPointMake(-1,-1);
pos[i]=CGPointMake(1,1);

And, of course, rather than the if/else based on i%2, you should use a switch based on:

arc4random()%4
gerry3
would you mind being more specific? How would i write the if statement for arc4random()%4?
NextRev
i figured it out, thanks for your answer though, definitely helped
NextRev