Cocos2D replaceScene doesn't load image - ios

I am working with Cocos2D v1.0.1 and I have a bug with this code
CCScene *scene = [TDGameHandler scene:kGameSolo andViewController:self];
if(![[CCDirector sharedDirector] runningScene])
{
[[CCDirector sharedDirector] runWithScene:scene]; // RUN THE SCENE
} else {
[[CCDirector sharedDirector] replaceScene:scene];
}
The first time there is no problem, but the second time there is no image load.
I tried to pop and push the scene but it didn't work either, I also tried to add startAnimation but the application crash.
I just tried to end the CCScene with this code :
[[CCDirectorIOS sharedDirector] end];
[[CCDirector sharedDirector] runWithScene:scene];
But it crash.
Thanks for your help.

try this:
[[CCDirector sharedDirector] replaceScene: [TDGameHandler scene]];

Related

COCOS2D Touch detection after application minimisation by gesture(iPad)

Currently I'm fixing an old Cocos2D game. There is a strange problem with it: when the application is being sent to background by the four-finger gesture(iPad) after returning it behaves like the touch began and didn't end, and there no way to end it because the application does not respond to touches. Also when I launch the application and do nothing, the screen becomes locked and after that the application some times does not respond to touches.
I guess that there is something I need to do in the following AppDelegate methods, but I can't figure out exactly:
- (void)applicationWillResignActive:(UIApplication *)application {
[[CCDirector sharedDirector] pause];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[CCDirector sharedDirector] resume];
}
enter code here
- (void)applicationDidEnterBackground:(UIApplication*)application {
[[CCDirector sharedDirector] stopAnimation];
}
-(void) applicationWillEnterForeground:(UIApplication*)application {
[[CCDirector sharedDirector] startAnimation];
}

Cocos2d's skew action exceeds itself when there is a framerate hiccup

It's simple: I have some sprites that are repeating a Skew action to one side and then the other in my map.
When there is a framerate hiccup, you can see that suddenly the skew exceeds itself - that is, if it should be at 2.0f, it looks like it is at 50.0f or something. If you wait a bit, it stabilises alright.
I suspect it is due to a framerate hiccup because it happens in the following scenarios:
When the phone is locked and then unlocked.
The application comes to foreground from the background.
The map scene was frozen in the Director's scene stack (because I used pushScene) and then it was resumed (by calling popScene).
It doesn't happen always. I've seen it occur in iPhones 4 and 5 - as well as iPads.
Is there a way to control this skew so it doesn't exceed itself ever?
cocos2d-iphone 1.0.1
When I had a problem like that it was because CCDirector was not paused properly when backgrounded.. so have you made sure that you are pausing/resuming the director and starting/stopping animation in all the right places?
-(void) applicationSignificantTimeChange:(UIApplication *)application
{
[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
}
-(void) applicationDidEnterBackground:(UIApplication*)application
{
[[CCDirector sharedDirector] stopAnimation];
}
-(void) applicationWillEnterForeground:(UIApplication*)application
{
[[CCDirector sharedDirector] startAnimation];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[CCDirector sharedDirector] resume];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
[[CCDirector sharedDirector] pause];
}

Cocos2D blocked buttons after replceScene and removeAllDelegates

I have a serious problem with Cocos2d.
1) I have one scene with few buttons in it
2) I have another scene in which i use
-(void) onEnter {
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowTouches:YES];
[super onEnter];
}
and also
-(void) onExit {
[[[CCDirector sharedDirector] touchDispatcher] removeDelegate:self];
[super onExit];
}
3) After replacing 1st scene to the 2nd everything is ok
4) But after returing to 1st scene (by replaceScene) all buttons on 1st scene are blocked. Nothing works
5) If i'm not using "removeDelegate" in "onExit()" buttons on 1st scene work, but touches from the 2nd scene are still active.

Importance of Cocos2D CCScenes?

I recently followed Ray Wenderlich's Cocos2D tutorial for putting Cocos2D in a UIKit app. I am currently only using Cocos2D in only one of my UIViews. In the tutorial he uses a CCScene which is a specific .h and .m class in the project. In the scene is some code which I do not know the important of either. I must say that I am new to Cocos2D and I am wondering what is the point of a CCScene? Do I need to use it in my situation? And if so, how?
Thanks!
Edit1: Is this correct? I am also adding a simple game loop so should I do it in the way I am doing it below or should I use CADisplayLink?
//Cocos2D methods
-(id) init {
if((self=[super init]))
{
CCScene *scene = [CCScene node];
CCLayer *layer = [CCLayer node];
[scene addChild:layer];
[[CCDirector sharedDirector] runWithScene:scene];
[self performSelector:#selector(cocosgameLoop:) withObject:nil afterDelay:1/60.0f];
}
return self;
}
- (void)cocosgameLoop:(ccTime)dT {
//Check for collisions here and add gravity, etc....
}
- (void)viewDidUnload {
[super viewDidUnload];
[[CCDirector sharedDirector] end];
}
//
CCScene is a root node in a scene graph. A node that have no parents. CCDirector operates with CCScenes only. Read this for more information: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:lesson_3._menus_and_scenes
ANSWER TO YOUR COMMENT
Actually you don't have to understand how CCScene is implemented. But if you want something to be rendered with cocos2d you have to create a CCScene and pass it to CCDirector. The common way is:
CCScene *scene = [CCScene node];
CCLayer *layer = [CCLayer node];
[scene addChild:layer];
[[CCDirector sharedDirector] runWithScene:scene];
Usually you have to subclass a CCLayer and reimplement init method to add your staff (sprites for example). Take a look at programming guide for more more detailed answer:
http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:index

Where should I go from here? (Cocos2D+UIKit)

Currently I have this code in my UIViewController:
//Cocos2D methods
-(id) init {
if((self=[super init]))
{
CCScene *scene = [CCScene node];
CCLayer *layer = [CCLayer node];
[scene addChild:layer];
[[CCDirector sharedDirector] runWithScene:scene];
[[CCDirector sharedDirector] setDisplayFPS:NO];
[self performSelector:#selector(cocosgameLoop:) withObject:nil afterDelay:1/60.0f];
}
return self;
}
- (void)cocosgameLoop:(ccTime)dT {
//Check for collisions here and add gravity, etc....
}
- (void)viewDidUnload {
[super viewDidUnload];
[[CCDirector sharedDirector] end];
}
//
My view controller is somewhat Objective-C code, but I want to add Cocos2D to the UIView. I think the code above is initializing it, but I am not sure. Also should the CCScene have its own class dedicated to everything that goes on in the scene? if so how would I do that?
I know these are a lot of questions, but my game is very similar to Doodle Jump and I need to know where to go from the current state I am in.
Does anyone have any ideas/tips?
Thanks!
performSelector will call the cocosgameLoop only once. You will want to call CCDirector startAnimation, and then schedule updates in your cocos2d nodes.
My Learn Cocos2D book (2nd Edition) explains how to add and setup Cocos2D in a UIKit view (regular iPhone) app.

Resources