1

ゲームのフレーム内で鳥が飛んでいますが、鳥を2つの異なる方向にしか飛ばすことができません。2羽の鳥がいる場合、2つの異なる方向に進みます。3羽の鳥がいる場合、そのうちの2羽は一方向に進み、もう1羽は別の方向に進みます。鳥を4つの異なる方向にランダムに移動させたいです。右上、右下、左上、左下、これが私のコードです。

-(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

これらはあなたの2つの方向ベクトルだと思います:

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

他の2つの方向は次のとおりです。

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

そしてもちろん、に基づくif / elseではなく、以下に基づくi%2スイッチを使用する必要があります。

arc4random()%4
于 2010-02-14T06:55:54.057 に答える