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

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

Related

Cocos2d on iOS: EXC_BAD_ACCESS in CCGraphicsBufferGLUnsynchronized when leaving app after ad

I have integrated the SKD of UnityAds into my game to show full screen interstitial video ads.
When the video is finished, a link to the AppStore is provided by the ad framework. When I select this link, the AppStore is opened. My app crashes in this moment, throwing EXC_BAD_ACCESS in CCGraphicsBufferGLUnsynchronized.
When starting my app, the UnityAds SDK is initialized like this:
[[UnityAds sharedInstance] startWithGameId:UNITYADS_MYAPP_ID
andViewController:[CCDirector sharedDirector]
];
As you can see, I am passing [CCDirector sharedDirector] as the view controller. I am mentioning this, as this might by a part of the problem?
Later I am calling the UnityAds SDK from within a Cocos2D scene like this:
-(void)showFullscreenAd {
// Stop Cocos2D rendering
[[CCDirector sharedDirector] pause];
[[CCDirector sharedDirector] stopAnimation];
// Is an ad available?
if ([[UnityAds sharedInstance] canShowAds]) {
// Display the ad
[[UnityAds sharedInstance] setZone:#"rewardedVideoZone"];
[[UnityAds sharedInstance] show];
}
}
As you can see I am stopping Cocos2D rendering before displaying the add.
When I select the AppStore link in the add, my app crashes.
This is the code Xcode points me to after the crash (in class CCGraphicsBufferGLUnsynchronized)
-(void)prepare
{
_count = 0;
GLenum target = (GLenum)_type;
glBindBuffer(_type, _buffer);
==> _ptr = _mapBufferRange(target, 0, (GLsizei)(_capacity*_elementSize), BUFFER_ACCESS_WRITE);
glBindBuffer(target, 0);
CC_CHECK_GL_ERROR_DEBUG();
}
Can someone point me into the right direction for debugging?
I am running my app on an iPad and iPhone under iOS 8.1.3
OK, I found a solution myself. Seems like the crash occurs because the app is in the background and rendering continues. So all I had to do was to make sure that rendering is not resumed after the app was moved to the background.
I do it like this now:
// Added a flag to control resuming of rendering
#property (readwrite) bool resumeRendering;
[...]
-(void)showFullscreenAd {
// Stop Cocos2D rendering
[[CCDirector sharedDirector] pause];
[[CCDirector sharedDirector] stopAnimation];
// Set to resume redering by default
_resumeRendering = YES;
// Is an ad available?
if ([[UnityAds sharedInstance] canShowAds]) {
// Display the ad
[[UnityAds sharedInstance] setZone:#"rewardedVideoZone"];
[[UnityAds sharedInstance] show];
}
}
[...]
#pragma mark - UnityAdsDelegate
// This method is called when the interstitial ad is discarded
// check if rendering should be resumed
-(void)unityAdsDidHide {
if ( _resumeRendering ) {
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];
}
// do anything else here
// In my case I transfer to the next scene
[...]
}
-(void)unityAdsWillLeaveApplication {
// Will leave application. Make sure rendering is not resumed anymore
_resumeRendering = NO;
}
When the user clicks the AppStore link in the ad now, the method unityAdsWillLeaveApplication will be called and I can mark resumeRendering as false.
Hope this helps other with the same problem.

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 replaceScene doesn't load image

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

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.

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