我的游戏中有小鸟在一个框架内飞行,但我只能让它们朝两个不同的方向飞行。如果有 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];
}