The following code snippet is objective c. It is calling base class init method.
[super init]
can you assist me to call the same in objective c++(COCOS2DX)?
In cocos2d-iphone,
self=[super init] is used to initialize self as a object of its parent type. So For example if parent of self is CCLayer then it will be initialize as type CCLayer.
So in cocos2d-x,
What you can do is, You have to call init method of it parent. For the same case, as i describe above,
You have to call init method of CCLayer
so the translation of [super init] will be CCLayer::init()
Related
So in my project I am using Cocos2D with CocosBuilder. I have assigned a few of my characters to be subclasses of CCNode with child CCSprites, etc.
I want these CCNodes to be reactive to touch - for example, if I touch any of them, they'd play a context sensitive animation. I only want to know how to make the node reactive to touch (or perhaps, having the layer reactive to touch which detects whether you've touched a sprite or not), the animation part is fine.
Any ideas? that would be lovely.
Sam
Turns out that this is fairly easy. In the header file of your class, you must define the class as implementing the protocol , like so:
#interface Foo : CCNode <CCTouchOneByOneDelegate>
{
}
and you must implement onEnter and onExit like this:
- (void)onEnter
{
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[super onEnter];
}
- (void)onExit
{
[[[CCDirector sharedDirector] touchDispatcher] removeDelegate:self];
[super onExit];
}
and you must implement ccTouchBegan (if you're using the OneByOneDispatcher)
i'm new cocos2d-iphone programmer, i developer a small game, I am confused about the object of the game is to inherit CCNODE or NSObject.
thank you!
Inherit CCNode if the object has to be added to the cocos2d node hierarchy via addChild. Inherit NSObject otherwise.
Learning cocos2d/kobold2d and puzzled by the scenes and layers. As I understand a scene usually contains layers where the layers contain UI elements like buttons and all the logic.
My first project is based on the kobold2d helloworld example and there I see that the layer is subclassed from CCLayer instead of CCScene. While searching about this topic I saw the helloworld example is with the layer derived from CCScene. Anyway, why does the helloworld example not derive from CCScene while it apparently did before?
I looked at some other examples (doodle and pinball) and there also CCLayer is used and not CCScene.
Is there a definitive guide on scenes and layers? Does kobold2d promote using just layers or how should I look at this?
Regards,
Rob
In Kobold2D, if your first class derives from CCLayer instead of CCScene, Kobold2D silently wraps it in a CCScene object. That way you don't have to write the +(id) scene method anymore.
This is great for scenes that only use a single layer, less code to write. And you can still add more layers to the scene by adding them to the layer's parent (which is the scene): [self.parent addChild:..]
Btw, if your first class is indeed a CCScene class or it implements the +(id) scene method, then that scene is used.
I was playing around with a Joystick moving a Sprite around in one layer. Now, according to best practice, the Joystick and the Sprite have to be in different Layers of the same scene. I have managed to separate these, yet I am now completely stuck having absolutely no clue whatsoever how to pass joystick commands from one layer to another? What is the recommended way to do this?
Scene
Game Play Layer
Sprite
Game Control Layer
Joystick
When the joystick is manipulated I need to pass this info to the GamePlayLayer to control the sprite.
Well, I got a great Cocos2d book, written by Rod Strougo and Ray Wenderlich, by the name "Learning Cocos2d". In their book, they implement a game, which has a joystick implemented and all, using your initial setup. The GamePlayLayer contains both the joyStick and the hero sprite. (See book page 40).
I don't believe they would use bad practices in their book, given they are very talented!
...
With that being said, I have possible solutions, if you wish to implement them on separate layers:
GameScene
|
|__>GameLayer
|
|__>ControlLayer
That's your basic setup. But, intuitively, what is the purpose of the control layer? Control the game layer's content! So, I would suggest you hold a (weak) reference to the GameLayer within the ControlLayer. That way, using a simple:
#property (nonatomic, assign) CCSprite* hero;
you now have access to the hero from the ControlLayer!
Extra (if needed):
//GameScene init:
- (id)init {
....
gameLayer = [GameLayer node];
controlLayer = [ControlLayer node];
[controlLayer setGameLayerRef:gameLayer];
...
}
// Control layer:
#property (nonatomic, assign) GameLayer* gameLayerRef;
Even though I just suggested that way, I don't use it in my games :)
What I normally do is:
Make the GameScene class a "Semi-Singleton". (I learned this method from "Learn iPhone and iPad Game Dev" By Itterheim (aka gaming horror, Kobold2d publisher ... etc).
Then, inside the control layer, I would call the GameScene object:
[[GameScene sharedScene] doSomethingToTheGameLayer];
Yeah, the gameScene has simplistic methods that just relies what the control need to update in the game layer.
Edit:
Implementing the Semi-singleton pattern, as described by Itterheim in his book.
But, what is semi-singleton?
It has the singleton pattern's property: you can access the object instance from anywhere using a static call.
[GameScene sharedScene];
However, singleton objects are usually retained, after being created for the first time, till the end of the application's life. In the Semi-singleton pattern, this is not the case.
Once you create the instance, you cannot create another instance before destroying the old one, BUT once you are done with the instance, you destroy it (dealloc). Creating another one when necessary.
Recap:
Semi-Singleton: Create many object from it, but only one at any given time. Only recreate after destroying the old.
Implementation:
Of course, as you do with any singleton class, you first declare a static variable of the same type of the class:
//In GameScene.m
static GameScene* instance = nil;
#implementation
//Here we have the static method to access the instance:
+(GameScene*)sharedScene {
//you must have created one before trying to access it "Globally".
///Typically, created where you transition the GameScene using CCDirector's replaceScene.
NSAssert(instance != nil, #"GameScene not instantiated!!");
return instance;
}
-(id)init {
if((self = [super init])) {
//Make sure you don't have another instance of the class created
//NOTE: Possible race condition that I wouldn't worry about, since it should never happen.
NSAssert(instance == nil, #"Another instance is already created!!");
instance = self;
...
}
return self;
}
//Don't forget!! Dealloc!!
- (void)dealloc {
//the only instance we had is dealloc'd! We are back to zero. Can create a new one later! :D
instance = nil;
[super dealloc];
}
Edit2:
So, the timeline:
CCScene* scene = [GameScene node];
[[CCDirector sharedDirector] replaceScene:scene];
...
[[GameScene sharedScene] doSomething];
...
[[CCDirector sharedDirector] replaceScene:otherScene];
//After the new scene replaces GameScene, dealloc will be called, implicitly. Making instance = nil;
instance = nil;
[super dealloc];
...
//Loop again
CCScene* scene = [GameScene node];
[[CCDirector sharedDirector] replaceScene:scene];
...
I'm trying to create a custom cocos2d and box2d object comprised of multiple bodies and need I little help with the structure.
I want the object to be composed of two bodies and one sprite. This is the header file I have created:
#interface NewBlock : CCNode {
CCSprite *sprite;
b2Body *body1;
b2Body *body2;
b2World *world;
}
So I'm inheriting from CCNode which I assume is the correct thing to do. Here is my implementation method:
-(id)initWithWorld:(b2World*)theWorld atLocation:(CGPoint)location {
if (self = [super init]) {
sprite = [CCSprite spriteWithFile:#"level_2.png"];
...box2d stuff...
[self addChild:sprite];
}
return self;
}
And in the scene I want the object, i would create it like:
NewBlock *block = [[NewBlock alloc] initWithWorld:world atLocation:ccp(100,100)];
[self addChild:block];
I know I could inherit from CCSprite but I might want to add more sprites to the object at some point so a more general example would be useful to me.
I have tried an example with the code above and get the following message in the console:
sharedlibrary apply-load-rules all
Current language: auto; currently c++
Is my structure for creating a custom cococs2d/box2d object correct? Am I missing anything?
The design is good. Personally I favor subclassing CCNode too, for the exact same reason: you never know when your CCSprite needs to contain multiple CCSprite. Thus the CCNode class becomes the controller, and CCSprite and other classes are treated as the View. The b2Body would be the model in this case.
Your error is unrelated to how you designed the class. Does the code still work, or does it crash? Maybe it's one of the many messages that can be ignored.
The usual caveats apply when adding Box2D code. All your classes should use the .mm extension, even those which may not be using Box2D code directly.