One of my layers won't show up!

Forums Programming cocos2d support (graphics engine) One of my layers won't show up!

This topic contains 4 replies, has 2 voices, and was last updated by  layschip 2 years, 9 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
Author Posts
Author Posts
July 19, 2011 at 6:56 pm #233452

layschip
@layschip

Hi! Please help!

The vikingSprite pointer was created in the GamePlayLayer.h file.

//  GamePlayLayer.m

#import "GamePlayLayer.h"

@implementation GamePlayLayer

-(id) init {
self = [super init];
if (self != nil) {
CGSize screenSize = [[CCDirector sharedDirector] winSize];
vikingSprite = [CCSprite spriteWithFile:@"sv_anim_1.png"];
self.isTouchEnabled = YES;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
vikingSprite.position = ccp(screenSize.height * 0.5, screenSize.width * 0.17);
[self addChild:vikingSprite];
}
else {
[vikingSprite setScaleX:screenSize.height / 1024.0f];
[vikingSprite setScaleY:screenSize.width / 768.0f];
[self addChild:vikingSprite];
}

}
return self;
}
@end

//  BackgroundLayer.m

#import "BackgroundLayer.h"

@implementation BackgroundLayer

-(id) init {
self = [super init];
if (self != nil) {
CCSprite *backgroundImage;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
backgroundImage = [CCSprite spriteWithFile:@"background.png"];
}
else {
backgroundImage = [CCSprite spriteWithFile: @"backgroundiPhone.png"];
}
CGSize windowSize = [[CCDirector sharedDirector] winSize];
backgroundImage.position = ccp(windowSize.width * 0.5, windowSize.height * 0.5);

[self addChild:backgroundImage z:0 tag:0];
}
return self;
}
@end

//  GameScene.m

#import "GameScene.h"

@implementation GameScene

-(id) init {
self = [super init];
if (self != nil) {
BackgroundLayer *myBackgroundLayer = [BackgroundLayer node];
[self addChild:myBackgroundLayer];

GamePlayLayer *myGamePlayLayer = [GamePlayLayer node];
[self addChild:myGamePlayLayer z:5];
}
return self;
}
@end

In the AppDelegate file I imported GamePlayScene, and uncommented

if( ! [director enableRetinaDisplay:YES] )
CCLOG(@"Retina Display Not supported");

And changed

CCDirector sharedDirector] runWithScene: [HelloWorld scene;

to

CCDirector sharedDirector] runWithScene: [GameScene node;

Thank you!

July 19, 2011 at 7:12 pm #339330

clarus
Moderator
@clarus

Do you have double-sized -hd.png versions of all of your png files? If you enable the retina display support, you need those when you run on a retina device (including the Simulator in iPhone4 mode). If your debug build preprocessor defines include COCOS2D_DEBUG=1 (which it should), you’ll get a debug assert if that’s the problem, though.

Do you get any asserts, crashes, etc? How does it fail? Just nothing shows up? Does it all work if you switch your scene back or comment out the enableRetinaDisplay line?

July 19, 2011 at 8:50 pm #339331

layschip
@layschip

Thanks for the reply! I have an hd version for the background (how do you check to see if they’re double sized?), but now that I check I don’t have an hd version of “sv_anim_1.png”. In the iPhone, iPhone (retina), iPad the background is being displayed, but not the vikingSprite. Even when I comment the enableRetinaDisplayLine. I’m not getting any errors, warnings, crashes or anything. Sorry for not including that in my first post! I’m a newb, so I don’t know about debug assert.

July 19, 2011 at 9:57 pm #339332

clarus
Moderator
@clarus

Double-sized images: I meant that you should have images at say, 320×480 and 640×960 (double the width, double the height of any you’re using which is actually 4X the original). Those are used on retina devices. Anyway, you make those in whatever graphics app you’re working in. If you commented out the line and it didn’t make a difference, then that’s not the problem, but you do want to include those larger images in the end.

In your build settings, is the COCOS2D_DEBUG=1 setting set for the type of build (presumably debug) that you’re doing? If not, add that because you’ll get feedback on what’s going on. You want to click the top entry in your Xcode project to get to the project settings, then go to the your target->build settings and then under “LLVM GCC 4.2 – Preprocessing” Spin down “Preprocessor Macros”, double-click to the right of Debug and add “COCOS2D_DEBUG=1″ without quotes. That will give you debug feedback from cocos2d.

One thing I noticed:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// This is the iPad case.
vikingSprite.position = ccp(screenSize.height * 0.5, screenSize.width * 0.17);
[self addChild:vikingSprite];
}
else
{
// This is the iPhone/iPod touch case. I see that you're scaling your
// iPad sized graphics down, but you never set the position of the sprite.
// You probably want to do that too.

[vikingSprite setScaleX:screenSize.height / 1024.0f];
[vikingSprite setScaleY:screenSize.width / 768.0f];
[self addChild:vikingSprite];
}

See the comments above.

Is vikingSprite non-nil when you’re adding it? You might want to check that to make sure the image is being found. With cocos2d debugging enabled, your code will grind to halt if that’s the case. Without debugging enabled, it’s probably a silent failure. If the sprite is nil, check the file name to make sure it exactly matches.

I’m not seeing anything wrong except for the missing position code above…

If you get more feedback/different results after looking at the above, post those and I’ll try to help.

July 19, 2011 at 11:58 pm #339333

layschip
@layschip

Thanks so much; adding the position of the sprite in the else statement fixed it! Thanks for teaching me about the debug alert too! (Lol, it was set to COCOS2D_DEBUG=1)

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.