CCSprite doesn't display on iPhone 4 - iphone

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.

Related

cocos2d CCScene background orientation

I'm trying to add a background image for my scene using the following code:
CCSprite* background = [CCSprite spriteWithFile:#"background-hd.png"];
background.tag = 1;
background.anchorPoint = CGPointMake(0,0);
[self addChild:background];
My apps orientation is landscape and the image's orientation is landscape as well, however I get the image orientated in portrait:
I think this is the solution with Layer which set the scene with orientation...
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
StartLayer *layer = [StartLayer node];
CCSprite *background = [CCSprite spriteWithFile:#"background-hd.png"];
background.anchorPoint = ccp(0,0);
[layer addChild:background z:-1];
[scene addChild: layer];
return scene;
}
See this link for More information Cocos2d-Scenes-and-Layers....
Hope this helpful to you...

Game Scene crashes when I restart the game

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!

cocos2d v2 scale my background

i have install cocos2d v2.0, and i am working a little on it, to see what changed against v1, i have create a simple cocos2d + box2d project, and i run it and all work fine, i have deleted the IntroLayer and the Helloworld Layer, i have tried to display a simple background layer, so i have do this:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#interface BackgroundLayer : CCLayer
#end
#import "BackgroundLayer.h"
#implementation BackgroundLayer
-(id)init {
self = [super init];
if (self != nil) {
CCSprite *backgroundImage = [CCSprite spriteWithFile:#"background.png"];
CGSize screenSize = [[CCDirector sharedDirector] winSize];
backgroundImage.position = ccp(screenSize.width/2, screenSize.height/2);
[self addChild:backgroundImage z:0 tag:0];
}
return self;
}
#end
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "BackgroundLayer.h"
#interface GameScene : CCScene
#end
#import "GameScene.h"
#implementation GameScene
-(id)init {
self = [super init];
if (self != nil) {
BackgroundLayer *backgroundLayer = [BackgroundLayer node];
[self addChild:backgroundLayer z:0];
}
return self;
}
#end
and then in the app delegate i have changed this:
[director_ pushScene: [IntroLayer scene]];
in this:
[director_ pushScene: [GameScene node]];
but the seem scaled of the 0.5, i have created the hd and the not hd image, for retina and not retina, what i wrong? this is the result:
http://img706.imageshack.us/img706/4778/schermata072456124alle1.png
EDIT:
if i change the BackgroundLayer.m in this way, all work fine:
#implementation BackgroundLayer
/*-(id)init {
self = [super init];
if (self != nil) {
CCSprite *backgroundImage = [CCSprite spriteWithFile:#"background.png"];
CGSize screenSize = [[CCDirector sharedDirector] winSize];
backgroundImage.position = ccp(screenSize.width/2, screenSize.height/2);
[self addChild:backgroundImage z:0 tag:0];
}
return self;
}*/
-(void) onEnter
{
[super onEnter];
CCSprite *backgroundImage = [CCSprite spriteWithFile:#"background.png"];
CGSize screenSize = [[CCDirector sharedDirector] winSize];
backgroundImage.position = ccp(screenSize.width/2, screenSize.height/2);
[self addChild:backgroundImage z:0 tag:0];
}
#end
anyone can explain me why? i remember that in the old version of cocos2d i can add the layer in the init method...
Use this style, worked in cocos2d 2.0 also.
#interface GameScene : CCLayer
{
}
+(CCScene *) scene;
#end
#import "GameScene.h"
#implementation GameScene
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
GameScene *gameScenelayer = [GameScene node];
// add layer as a child to scene
[scene addChild: gameScenelayer];
// return the scene
return scene;
}
-(id)init {
self = [super init];
if (self != nil) {
CCSprite *backgroundImage = [CCSprite spriteWithFile:#"background.png"];
CGSize screenSize = [[CCDirector sharedDirector] winSize];
backgroundImage.position = ccp(screenSize.width/2, screenSize.height/2);
[self addChild:backgroundImage z:0 tag:0];
}
return self;
}
#end
//Call this
[director_ pushScene: [GameScene scene]];

Alpha Blending With Apple UI on iPhone

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

White screen to load cocos2d scene

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]