我的游戏中有一只鸟在一个框架内飞行,但我只能让它们在两个不同的方向飞行。如果有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];
}
答案 0 :(得分:1)
我认为这是你的两个方向向量:
pos[i]=CGPointMake(-1,1);
pos[i]=CGPointMake(1,-1);
另外两个方向是:
pos[i]=CGPointMake(-1,-1);
pos[i]=CGPointMake(1,1);
当然,除了基于i%2
的if / else之外,您应该使用基于以下内容的开关:
arc4random()%4