I have been trying to get a layer into my scene. My scene is called "Survival". My layer is called "SSpriteLayer". The scene initializes, but the layer doesn't.
SURVIVAL.H:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "SSpriteLayer.h"
#interface Survival : CCLayer {}
#end
SURVIVAL.M:
#implementation Survival
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
Survival *layer = [Survival node];
SSpriteLayer *spriteLayer = [SSpriteLayer node];
// add layer as a child to scene
[scene addChild: layer];
[scene addChild: spriteLayer];
// return the scene
return scene;
}
-(id) init
{
if( (self=[super init]))
NSLog(#"SCENE HAS INIT");
return self;
}
#end
SSPRITELAYER.H:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#interface SSpriteLayer : CCLayer {
}
#end
SSPRITELAYER.M:
#import "SSpriteLayer.h"
#implementation SSpriteLayer
-(id) init
{
if( (self=[super init]))
NSLog(#"SPRITELAYER HAS INIT");
return self;
}
#end
What I do not understand is why my layer isn't initizializing, and why I am not getting the message "SPRITELAYER HAS INIT" in the debug area.
// add layer as a child to scene
[self addChild: layer];
[self addChild: SpriteLayer];
Change it And Try
Make sure, that you call
[Survival scene];
not
[Survival node];
and post code with calling your scene here
Related
I'm trying to draw an empty rectangle with a SKShapeNode but does appear on the screen. If i set a color to the fill property the rectangle appears. What could be the issue?
//MyScene.h
#import <SpriteKit/SpriteKit.h>
#interface MyScene : SKScene
#end
//MyScene.m
#import "MyScene.h"
#implementation MyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
SKShapeNode *shapeNode = [SKShapeNode shapeNodeWithRectOfSize:CGSizeMake(self.frame.size.width/2, self.frame.size.height/2)];
shapeNode.strokeColor = [SKColor redColor];
shapeNode.lineWidth = 3;
[self addChild:shapeNode];
}
return self;
}
#end
I hope to make some custom picture(e.x some logo and information of the game developer)before game is launched.In the cocos2d temple,there is only a IntroLayer to show logo,so I decide to add more scene to display other information before entering the main menu of the game.In IntroLayer.h
//this is the template code
#interface IntroLayer : CCLayer
{
}
+(CCScene *) scene;
#end
//this is the new scene,I hope to display after the IntroLayer
#interface SecondScene : CCLayer
{
}
+(CCScene*) scene;
#end
//In IntroLayer.m
#implementation IntroLayer
//I change the replaceScene function from [HelloWorldLayer node] to[SecondScene scene]
-(void) onEnter
{
[super onEnter];
[[CCDirector sharedDirector] replaceScene:
[CCTransitionFade transitionWithDuration:1.0 scene:[SecondScene scene]]];
}
#end
#implementation SecondScene
+(CCScene*)scene
{
CCScene *scene = [CCScene node];
SecondScene *layer = [SecondScene node];
[scene addChild:layer];
return scene;
}
-(id)init
{
if (self = [super init]) {
CGSize size = [[CCDirector sharedDirector] winSize];
CCSprite *background = [CCSprite spriteWithFile:#"LOGO.png"];
background.position = ccp(size.width/2, size.height/2);
[self addChild: background];
}
return self;
}
//I hope the main game scene display after the SecondScene
-(void)onEnter{
[[CCDirector sharedDirector] replaceScene:
[CCTransitionFade transitionWithDuration:1.0 scene:[HelloWorldLayer scene]]];
[super onEnter];
}
#end
But when I run this code,I found it doesn't run as I expected.The SecondScene and the HelloWorldLayer display nearly at the same time,actually at last there is only SecondScene left in the screen and the HelloWorldLayer quickly disappear.I am really confused with that.Can anyone give some advice?Really Thanks for that.
If you want the second transition to follow the first, you need to override the onEnterTransitionDidFinish method:
-(void)onEnterTransitionDidFinish{
[super onEnterTransitionDidFinish];
[[CCDirector sharedDirector] replaceScene:
[CCTransitionFade transitionWithDuration:1.0 scene:[HelloWorldLayer scene]]];
}
I'm currently trying to access a function on another file.(another layer, to be more precise).
Both layers are on a scene.
Third layer is trying to get a function from first layer...
Here's how I'm doing this:
Here's my scene in scene.h
#import "firstLayer.h"
#import "secondLayer.h"
#import "thirdLayer.h"
#interface myScene : CCScene
{
// custom instance vars here...
}
#end
Here's how I cast my scene in scene.m
-(id)init {
self = [super init];
if(self != nil){
firstLayer *firstLayerz = [firstLayer node];
[firstLayerz setTag:111];
[self addChild:firstLayerz z:0];
secondLayer *secondLayerz = [secondLayer node];
[secondLayer setTag:112];
[self addChild:secondLayer z:2];
thirdLayer *thirdLayerz = [thirdLayer node];
[thirdLayerz setTag:113];
[self addChild:thirdLayerz z:4];
Here's how I cast the function in thirdLayer.m
#import "scene.h"
#implementation thirdLayer.m
-(id)init {
self = [super init];
if(self != nil){
firstLayer* firstLayerz = (firstLayer*)[self.parent getChildByTag:111];
[firstLayerz functionNeeded];
}
Here's functionNeeded in firstLayer.m (right below init(
-(void)functionNeeded {
NSLog(#"inside fnnction needed");
}
Of course the log ain't showing...
I do the proper cast in firstLayer.h
#interface firstLayer : CCLayer {
}
-(void)functionNeeded;
#end
In your firstLayer's init method write
self.tag = 111;
Now in your ThirdLayer where you want to call method of first layer write :
CCScene *current = [[CCDirector sharedDirector] runningScene];
if (current) {
id layer = [current getChildByTag:111];
if (layer) {
[layer functionNeeded];
}
}
I've recently decided to subclass my sprite, but I am a bit clueless on how to add them to a scene. At the moment, I have created my CCSprite subclass, using New File>Cocos2d>CCNode>Subclass of... CCSprite. Then, I have made my sprite in the Sprite.h file:
#interface Mos : CCSprite {
CCSprite *mos;
}
Once this is done, in the Sprite.m I code this:
#implementation Mos
-(id) init
{
if( (self=[super init])) {
mos = [CCSprite spriteWithFile:#"sprite_mos.png"];
}
return self;
}
What I want to know is how to then add this sprite into my game's scene.
Here is how to correctly subclass CCSprite as the documentation says:
#interface Mos : CCSprite {
// remove the line CCSprite *mos;
}
#implementation Mos
// You probably don't need to override this method if you will not add other code inside of it
-(id) initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect
{
if( (self=[super initWithTexture:texture rect:rect]))
{
}
return self;
}
+ (id)sprite
{
return [Mos spriteWithFile:#"sprite_mos.png"];
}
#end
Then in your code, you can use Mos normally:
Mos *mos = [Mos sprite];
[scene addChild:mos];
The same way you add CCSprites and other classes.
Mos *newMos = [[Mos alloc] init];
// set coordinates and other properties
[scene addChild:newMos];
[newMos release];
Edit:
#interface Mos : CCSprite {
// some member variables go here
}
#implementation Mos
-(id)init
{
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:#"sprite_mos.png"];
if( texture ) {
CGRect rect = CGRectZero;
rect.size = texture.contentSize;
// set members to some values
return [self initWithTexture:texture rect:rect];
}
[self release];
return nil;
}
And then in your scene class
// ...
Mos *obj = [[Mos alloc] init];
// set position, etc
[scene addChild:obj];
[obj release];
// ...
I have a class in which i am calling a method(with a SEL parameter) of another class like as follows
// class 1
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// returns a CClayer Object
id myClassLayer = [myClass objWithBackgroundSprite:objBackground withRect:CGRectMake(160,100, 300,120) atPoistion:pos_Top];
SEL callOnClick;
[myClassLayer AddButtonWithImage:ButtonBackground selector:callOnClick withDisabledImage:disabledBackground];
[scene addChild: myClassLayer];
// return the scene
return scene;
}
// class 2
-(void)AddButtonToHUDWithImage:(NSString *)imageName selector:(SEL)selector withDisabledImage:(NSString *)disbdImageName
{
CCMenuItem *menuItem1 = [CCMenuItemImage itemFromNormalImage:imageName selectedImage:imageName disabledImage:disbdImageName target:self selector:#selector(selector)];
CCMenu *menu = [CCMenu menuWithItems:menuItem1,nil];
[menu setPosition:ccp(160,100)];
[self addChild:menu];
}
-(void)selector
{
// not creating any object .. some other way if possible
}
but i would like to declare the selector method(callOnClick) in HelloWorld only(myClass being a Generic Class).
How can i call the selector in HelloWorld only??
If you need no instance, then you can use a class method and the target/object will be [SOMEType class] or [self class]:
+ (void)selector // << + not -
{
// not creating any object .. some other way if possible
}