I am developing a game using cocos2D game engine for IOS6. I use the following code for starting the game for the first time :
-(void)addGameScene
{
CCDirector *director = [CCDirector sharedDirector];
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeMainLoop];
else {
[CCDirector setDirectorType:kCCDirectorTypeDisplayLink];
}
// Init the View Controller
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
if(!glView)
glView = [[EAGLView alloc] initWithFrame:[window bounds]];
[director setOpenGLView:glView];
[director setOpenGLView:glView];
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
[viewController setView:glView];
[window setRootViewController:viewController];
[window addSubview: viewController.view];
[window makeKeyAndVisible];
[[CCDirector sharedDirector] runWithScene: [HelloWorldLayer scene]];
}
When i go out of the game scene i do not end the cocos2D game engine, instead i just stop the animations and hide the glView. I use this code.
[[CCDirector sharedDirector] stopAnimation];
CATransition *animation3 = [CATransition animation];
[animation3 setDuration:0.5f];
[animation3 setType:kCATransitionFade];
[animation3 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[[CCDirector sharedDirector].openGLView layer] addAnimation:animation3 forKey:#"SwitchToView"];
[[CCDirector sharedDirector].openGLView setHidden:YES];
When i again start the game to be played, I use this code :
[[CCDirector sharedDirector].openGLView setHidden:NO];
[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer scene]];
[[CCDirector sharedDirector] startAnimation];
It works fine.
But when I start the game for the first time, and the i go back from the game scene, and then I exit the application by pressing the home button of device, then I again start the application, and then I restart the game, I get a crash in this scenario.
The console prints :
2012-12-12 15:53:24.847 CasinoApp[2856:12203] -[HelloWorldLayer init] : Screen width 480.00 screen height 320.00
2012-12-12 15:53:24.848 CasinoApp[2856:12203] 10.000000
2012-12-12 15:53:24.849 CasinoApp[2856:12203] -[CCTexture2D(Image) initWithImage:resolutionType:] : cocos2d: CCTexture2D. Can't create Texture. UIImage is nil
2012-12-12 15:53:24.850 CasinoApp[2856:12203] -[CCTextureCache addImage:] : cocos2d: Couldn't add image:lines in CCTextureCache
2012-12-12 15:53:24.850 CasinoApp[2856:12203] *** Assertion failure in -[CCDirectorTimer startAnimation], /Users/rakesh/Desktop/Ryan/Code/CasinoApp/CasinoApp/libs/cocos2d/Platforms/iOS/CCDirectorIOS.m:498
Can Anyone tell me the reason for this. It would be appreciated.. Thanks.
When you can the scene method of a CCScene class, the class gets reinitialised and the whole scene is constructed again. therefore, you do not have to start the animations again if you are replacing the scene with the same scene, also it you are hiding the glView you can try pausing the director and resume it again before you unhide the glView.
[[CCDirector shareDirector]pause];
[[CCDirector sharedDirector].openGLView setHidden:YES];
and
[[CCDirector shareDirector]resume];
[[CCDirector sharedDirector].openGLView setHidden:YES];
[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer scene]];
Try
[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] pause];
and
[[CCDirector sharedDirector] stopAnimation]; // call this to make sure you don't start a second display link
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];
Hope this helps!
Related
I am using MPMoviePlayerController to play a video in the background of a CCMenu. So I have a video playing behind some buttons. When I switch views/scenes, I want the moviePlayer to leave the scene the same as the buttons/images on that layer/scene.
The code to play the movie in the background is as follows:
-(void)playMainMenuVideo{
NSString *path=[[NSBundle mainBundle] pathForResource:#"MYMMainMenu" ofType:#"mov"];
MPMoviePlayerViewController * player=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
player.moviePlayer.controlStyle = MPMovieControlStyleNone;
[[player view] setFrame:[[[CCDirector sharedDirector]view] bounds]];
player.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
[[[CCDirector sharedDirector]view] addSubview:[player view]];
[[[CCDirector sharedDirector]view] sendSubviewToBack:player.view];
UIView* glView = [CCDirector sharedDirector].view; // attention
[glView.superview insertSubview:player.view atIndex:0]; // attention
glView.opaque = NO; // attention
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // attention
}
This works perfect, In the app delegate I also changed
CCGLView *glView = [CCGLView viewWithFrame:[window_ bounds]
pixelFormat:kEAGLColorFormatRGB565 // To kEAGLColorFormatRGBA8
Lastly I want to do a page turn like so:
[[CCDirector sharedDirector] replaceScene: [CCTransitionPageTurn transitionWithDuration:1.0 scene:[FirstPageViewController scene]]];
But only the sprites will leave the scene, the video stays playing in the background. How can I fix this? Thanks in advance
I figured it out.
1) Setup notification for when video is done playing.
2) When its done playing, place an image of the last frame of the video, over the video.
3) Then set these properties back to original
UIView* glView = [CCDirector sharedDirector].view; // attention
glView.opaque = YES; // attention
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
4)Remove MPMoviePlayerController
Happy Coding everyone :)
I have a an app using cocos2D for a mini-game.
When I push a button, I launch a cocos2D scene with the game.
I try to load just a image in background (bg.png) in my cocos2D scene. I'm using sprites.
It works on iPhone 5 and iPhone 4S (iOS 6), but it doesn't work on the iPhone 4.
On the iPhone 4 I have a black screen. I don't understand why.
sprites-hd.png does 3762 × 1252, 1,4 Mo. Is it too big for the iPhone 4 ?
In my GameViewController_iPhone.m :
-(IBAction) playGame {
CCDirector *director = [CCDirector sharedDirector];
CCGLView *glView = [CCGLView viewWithFrame:[[ISAMAppDelegate_iPhone sharedISAMAppDelegate].window bounds]
pixelFormat:kEAGLColorFormatRGB565 // kEAGLColorFormatRGBA8
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];
// attach the openglView to the director
[director setView:glView];
if( ! [director enableRetinaDisplay:YES] )
MyLog(#"Retina Display Not supported");
[[CCDirector sharedDirector] setAnimationInterval:1.0/60];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
[self.view addSubview:glView];
[[CCDirector sharedDirector] runWithScene:[Game sceneWithGameViewController:self]];
}
In my Game.m :
+ (CCScene *)sceneWithGameViewController:(GameViewController_iPhone*) gvc
{
CCScene *game = [CCScene node];
Game *layer = [[Game alloc] initWithGameViewController:gvc];
[game addChild:layer];
return game;
}
- (id) initWithGameViewController:(GameViewController_iPhone*) gvc {
if(![super init]) return nil;
if((self = [super init])) {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"sprites.plist"];
CCSpriteBatchNode* batchNode = [CCSpriteBatchNode batchNodeWithFile:#"sprites.png" capacity:50];
[self addChild:batchNode];
CCSprite *someSprite = [CCSprite spriteWithSpriteFrameName:#"bg.png"];
[batchNode addChild:someSprite];
}
}
You can't load textures larger than 2048x2048 in iPhone 4, so you may split your current sprite atlas.
So I have been trying to create a menu for a game I have developed.
I am using Cocos2d and the game is set in portrait orientation. The menu.m file that I have written looks like this.
// Import the interfaces
#import "Menu.h"
#import "BankerInfo.h"
// HelloWorldLayer implementation
#implementation MenuLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
MenuLayer *layer = [MenuLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
// ask director the the window size
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite *menubackground = [CCSprite spriteWithFile:#"Ninja Menu Background.png"];
menubackground.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:menubackground z:-1];
CCLabelTTF *title = [CCLabelTTF labelWithString:#"Catch It If You Can!" fontName:#"Zapfino" fontSize:22];
title.position = ccp(160, 420);
[self addChild: title];
CCLayer *menuLayer = [[CCLayer alloc] init];
[self addChild:menuLayer];
CCMenuItem *Bankerbutton = [CCMenuItemImage
itemFromNormalImage:#"Bankernotpressed.png"
selectedImage:#"Bankerpressed.png"
target:self
selector:#selector(startBanker:)];
//Bankerbutton.position = ccp(100, 175);
CCMenuItem *Babybutton = [CCMenuItemImage
itemFromNormalImage:#"Babynotpressed.png"
selectedImage:#"Babypressed.png"
target:self
selector:#selector(startBaby:)];
//Babybutton.position = ccp(100, 75);
CCMenuItem *Mommaduckbutton = [CCMenuItemImage
itemFromNormalImage:#"Mommaducknotpressed.png"
selectedImage:#"Mommaduckpressed.png"
target:self
selector:#selector(startMommaduck:)];
//Mommaduckbutton.position = ccp(350, 175);
CCMenuItem *Baseballbutton = [CCMenuItemImage
itemFromNormalImage:#"Baseballnotpressed.png"
selectedImage:#"Baseballpressed.png"
target:self
selector:#selector(startArcher:)];
//Baseballbutton.position = ccp(350, 75);
CCMenuItem *Newtonbutton = [CCMenuItemImage
itemFromNormalImage:#"Newtonnotpressed.png"
selectedImage:#"Newtonpressed.png"
target:self
selector:#selector(startArcher:)];
//Newtonbutton.position = ccp(350, 75);
CCMenu *menu = [CCMenu menuWithItems: Bankerbutton, Babybutton, Mommaduckbutton, Baseballbutton, Newtonbutton, nil];
menu.position = ccp(winSize.width/2, (winSize.height/2)-30);
[menu alignItemsInRows:
[NSNumber numberWithInt:2], [NSNumber numberWithInt:2], nil];
[menuLayer addChild: menu];
}
return self;
}
- (void) startBanker: (id) sender
{
[[CCDirector sharedDirector] replaceScene:[BankerInfoLayer scene]];
}
/*- (void) startBaby: (id) sender
{
[[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Soldier scene]];
}
- (void) startMommaduck: (id) sender
{
[[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Mage scene]];
}
- (void) startBaseball: (id) sender
{
[[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Archer scene]];
}
- (void) startNewton: (id) sender
{
[[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Archer scene]];
}
*/
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
#end
And the error I'm getting looks like this.
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '+[NSInvocation invocationWithMethodSignature:]:
method signature argument cannot be nil'
Any Ideas?
The error states that thi is happening because of a bad call to an unexisting method signature. If you provide the functions used on the menu creation, does it still crash?
STEP 1: Make sure that the baseball button and newton button direct to the right selector, and not just the "startArcher:" that doesnt exist.
CCMenuItem *Baseballbutton = [CCMenuItemImage
itemFromNormalImage:#"Baseballnotpressed.png"
selectedImage:#"Baseballpressed.png"
target:self
selector:#selector(startBaseball:)]; // CHANGED HERE
//Baseballbutton.position = ccp(350, 75);
CCMenuItem *Newtonbutton = [CCMenuItemImage
itemFromNormalImage:#"Newtonnotpressed.png"
selectedImage:#"Newtonpressed.png"
target:self
selector:#selector(startNewton:)]; // CHANGED HERE
STEP 2: Uncomment the functions so you actually have valid selectors
- (void) startBanker: (id) sender
{
[[CCDirector sharedDirector] replaceScene:[BankerInfoLayer scene]];
}
- (void) startBaby: (id) sender
{
[[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Soldier scene]];
}
- (void) startMommaduck: (id) sender
{
[[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Mage scene]];
}
- (void) startBaseball: (id) sender
{
[[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Archer scene]];
}
- (void) startNewton: (id) sender
{
[[CCDirector sharedDirector] replaceScene:[Backgroundinfo_Archer scene]];
}
I am trying to place an OpenGL view on top of a standard UIView with a UIImageView. When I draw anything into the OpenGL view, I see slight alpha blending issues that result in the white lines seen below:
CCDirector *director = [CCDirector sharedDirector];
EAGLView *glView = [EAGLView viewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
pixelFormat:kEAGLColorFormatRGBA8 // kEAGLColorFormatRGBA8
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];
[glView setMultipleTouchEnabled:YES];
[director setOpenGLView:glView];
if(![director enableRetinaDisplay:YES]) NSLog(#"Retina Display Not supported");
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
[director setAnimationInterval:1.0/60];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
[[CCDirector sharedDirector] runWithScene:[PPAvatarEditorScene scene]];
[[CCDirector sharedDirector] resume];
[glView setOpaque:NO];
[avatarImage addSubview:glView];
I am using Cocos2D to draw into the OpenGL view. Is there an issue with the Alpha Pixel Format or Texture Format?
Try setting the glClearColor to black:
glClearColor(0, 0, 0, 0);
I am new in cocos 2d game development and developing a game where I need to reload scenes from view controller many times.For this i remove the scene and run it again.After 2 or more times the scene loads but white screen occurs and in console the error "OpenGL error 0x0506 in -[EAGLView swapBuffers]" is shows.
here is my code to add the scene-
if ([[CCDirector sharedDirector] runningScene] == NULL)
{
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector *director = [CCDirector sharedDirector];
glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8
depthFormat:GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:YES
numberOfSamples:4];
[director setOpenGLView:glView];
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
[director setAnimationInterval:1.0/60];
[window addSubview:glView];
[[CCDirector sharedDirector] runWithScene: [HelloWorldLayer node]];
}
and code for remove the scene-
[[CCDirector sharedDirector].openGLView removeFromSuperview];
[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] end];
[[CCDirector sharedDirector] release];
Please help me I am not getting where is the problem.
Thanks.
Two things of note:
don't release CCDirector! All you need to do is call stopAnimation and later startAnimation
don't remove the openGLView from its super view. Instead, just hide it: [CCDirector sharedDirector].openGLView hidden:YES]