1

我的游戏中有小鸟在一个框架内飞行,但我只能让它们朝两个不同的方向飞行。如果有 2 只鸟,它们会朝两个不同的方向前进。如果有 3 只鸟,其中 2 只朝一个方向飞,另一只朝不同方向飞。我希望鸟儿随机向四个不同的方向前进。右上、右下、左上和左下,这是我的代码。

-(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];
}
4

1 回答 1

1

我认为这些是你的两个方向向量:

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

另外两个方向是:

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

而且,当然,而不是 if/else 基于i%2,您应该使用基于以下的开关:

arc4random()%4
于 2010-02-14T06:55:54.057 回答