2010-02-14 7 views
1

J'ai des oiseaux qui volent dans un cadre dans mon jeu, mais je peux seulement les faire voler dans deux directions différentes. S'il y a 2 oiseaux, ils vont dans deux directions différentes. S'il y a 3 oiseaux, 2 d'entre eux vont dans une direction et l'autre dans une direction différente. Je veux que les oiseaux partent au hasard dans quatre directions différentes. En haut à droite, en bas à droite, en haut à gauche et en bas à gauche, voici mon code.CGPointMake question pour iphone?

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

Répondre

1

Je pense que ce sont vos deux vecteurs de direction:

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

Les deux autres directions sont:

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

Et, bien sûr, plutôt que le if/else basé sur i%2 , vous devez utiliser un commutateur basé sur:

arc4random()%4 
+0

cela vous dérangerait-il d'être plus précis? Comment j'écrirais l'instruction if pour arc4random()% 4? – NextRev

+0

Je l'ai compris, merci pour votre réponse, certainement aidé – NextRev