Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

James Brown
James Brown
2,095 Points

How to count states of enumerateChildNodes in the update method

I have an enumerateChildNodes update working in my game, but I want to record the state of childNodes running through the block and count the state each of them is in, in order to establish game over or next level. I thought I could do this with an integer variable outside the block that would act as a counter. But the "stateThrees = stateThrees+1" line is throwing up an error "variable is not assignable (missing _block type specifier). Can someone tell me how I make the integer assignable? And is this a standard way of counting states?

  • (void) update:(NSTimeInterval)currentTime {

int stateOnes = 0; int stateTwos = 0; int stateThrees = 0;

[self enumerateChildNodesWithName:@"Neut" usingBlock:^(SKNode *node, BOOL *stop) {

BOMNeutNode neutChild = (BOMNeutNode)node;

if (neutChild.Value == 0) { neutChild.texture = [SKTexture textureWithImageNamed:@"Neut_State1"]; } else if (neutChild.Value > 0) { neutChild.texture = [SKTexture textureWithImageNamed:@"Neut_State2"]; } else if (neutChild.Value < 0) { stateThrees = stateThrees+1; neutChild.texture = [SKTexture textureWithImageNamed:@"Neut_State3"]; } }]; }

if (stateThrees < 1) { [self nextLevel];

}

1 Answer

Stone Preston
Stone Preston
42,016 Points

put __block in front of the variable name when you declare it:

__block int stateOnes = 0; __block  int stateTwos = 0; __block  int stateThrees = 0;

although your current implementation is not going to work since these variables will be reset to 0 every second when update is called. add these variables as properties of your scene instead. then you wont even have to mess with __block stuff

James Brown
James Brown
2,095 Points

Thanks Stone,

Ah, that's so simple. Thanks. The implementation I was running was just a place marker, I've now got it to work. I needed the variables to be reset to 0 every update because I'm tracking states that determine whether you win or lose the level, so if every childNode is in stateOne you win, if every childNode is in stateThree game over. I just had write the implementation to count up to the total number of nodes rather than down to 0.