0
votes

I have built a battleship game where the player and computer take turns to fire bomb at a 10X10 grid.

I am using cocos2d 2.0 for iphone.

I have two scenes, the PlayerScene and AIScene.

In Playerscene.m, I use

[[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInR transitionWithDuration:1.0 scene:[AIScene sceneWithPositions:otherpos andHits:otherhits andOtherPositions: rects andOtherHits: prev]]];

to advance to the AIScene after the player has chosen the position.

this works well.

However, in the AIScene, I uses a

[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[CombatScene sceneWithParameters:OtherPositions andHits:OtherHits andOtherPositions: Positions andOtherHits: Hits]]];

to go back and this is not working. The game remained at the AIScene.

However, I am able to touch the screen and the game will flash me the PlayerScene with the bomb I just placed and return back to AIScene.

What is wrong?

LATEST INFO: I added a button in the AIscene to trigger the replaceScene event and it works. However, if i add it to the end of the onEnter() Method, it does not work.

2
are you sure you need scene replacing here? it seems quite strange in this case.Morion

2 Answers

1
votes

Take that code out of onEnter method, create a timer or something like that, for example put this into the onEnter method:

[self performSelector:@selector(replaceSceneAfterDelay:)withObject:nil afterDelay:3.0];

and still in the AIScene, create the method:

-(void)replaceSceneAfterDelay
{
  [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[CombatScene sceneWithParameters:OtherPositions andHits:OtherHits andOtherPositions: Positions andOtherHits: Hits]]];
}

Please note that 3 secs. is way too much, try your own value.

The following text was taken from the book "Learn IPhone 5 and IPad 2 Cocos2d Game Development". Written by Steffen Itterheim. Link: http://www.amazon.com/Learn-cocos2d-Game-Development-iOS/dp/1430238135

"Scenes and Memory:

Keep in mind that when you replace one scene with another, the new scene is loaded into memory before the old scene’s memory is freed."

2
votes

You can't replaceScene within the onEnter method (nor the init method). In other words you can't call replaceScene from a scene that is currently still in the process of replacing another scene.

You can schedule a selector once and then call replaceScene from the scheduled selector. That way replacing occurs only after the scene has been replaced.