views:

114

answers:

2

I have written my code in following way in cocos2d.

id actionTo = [CCFadeOut actionWithDuration:4.0f];  
id actionTo0 = [CCSequence actionWithDuration:2.0f];
if (m < enemyNumber)
    id actionTo1 = [CCCallFunc actionWithTarget:self selector:@selector(goToNextScene)];
else
    id actionTo1 = [CCCallFunc actionWithTarget:self selector:@selector(goToEndScene)];
id actionSeq = [CCSequence actions:actionTo, actionTo0, actionTo1, nil];
[targetE runAction: [CCSequence actions:actionSeq, nil]];  

error: expected expression before 'id'  

I am getting the above error. Should not we use (id) in if condition ? I want to get called two selectors by using the if- else condition. How can I make it ? Thank You.

+5  A: 

You cannot declare new variable in that place. What you should do is declare your actionTo1 variable before if-condition and set its value there:

...
id actionTo1 = nil;
if (m < enemyNumber)
    actionTo1 = [CCCallFunc actionWithTarget:self selector:@selector(goToNextScene)];
else
    actionTo1 = [CCCallFunc actionWithTarget:self selector:@selector(goToEndScene)];
...
Vladimir
Thank You Vladimir.
srikanth rongali
you can declare the variable there just fine but it won't be visible outside the scope of the if or else block
GamingHorror
Yes, but to be able to declare a variable inside if-else statement you will need to put code into {} block and yes - that will limit variable visibility to that block only. P.S. nice blogs btw
Vladimir
+3  A: 

It's simpler to write

id actionTo1 = [CCCallFunc actionWithTarget:self selector:
                (m<energyNumber ? @selector(goToNextScene) : @selector(goToEndScene))];
KennyTM
Yes.That was really simpler. THank You Kenny.
srikanth rongali