rewrite the code cocos2d (objective-c) on cocos2d-x (c++) - c++

Now rewrite the code from cocos2d (objective-c) to cocos2d-x (c + +) and collided with a problem:
I have two classes, two CCLayer. In one class there CCMenu by pressing the button calls the second class:
CCMenuItem * button = [CCMenuItemImage itemWithNormalImage: # "1.png" selectedImage: nil block: ^ (id sender) {
             
              HelloWorldLayer * helloWorldLayer = (HelloWorldLayer *) [self.parent getChildByTag: 777];
              [helloWorldLayer createSprite: self];
          }];
And in the second grade, I assign Layer tag:
         self.tag = 777;
and that method:
  - (void) createSprite: (id) sender {
..}
How to rewrite this code in C + +?))

It is simple.
Let class in which menu is Class- A
CCMenuItem * button=CCMenuItemImage::create("normal", "selected", "disable", menu_selector(A::Method));
The Method is
void A::Method(CCObject *pSender){
HelloWorldLayer * helloWorldLayer = (HelloWorldLayer *)this->parent->getChildByTag(777);
helloWorldLayer->createSprite(this);
}

Related

Refresh game scene in cocos2d

Hey folks I've being working around as follows. I get 2 layers, one's for control and the other's for game stuff. The problem is that the game's stucked ( the game's scene is refreshed and showed but it's out of control) when I trigerred a function for restart from my control layer. Have no idea where the problem is.
//restart function from input layer
......
-(id) init
{
if ((self = [super init]))
{......}
return self;
}
......
-(void)restart
{
GameScene* game = [GameScene sharedGameScene];
[game restartScene];
}
//gameLayer
......
static GameScene* instanceOfGameScene;
#implementation GameScene
+(GameScene*) sharedGameScene
{
if(instanceOfGameScene == nil)
instanceOfGameScene = [[self alloc]init];
return instanceOfGameScene;
}
+(id) scene
{
CCScene* scene = [CCScene node];
GameScene* layer = [GameScene node];
[scene addChild:layer z:0];
InputLayer* inputLayer = [InputLayer node];
[scene addChild:inputLayer z:1];
return scene;
}
-(void) restartScene
{
CCScene * newScene = [GameScene scene];
[[CCDirector sharedDirector] replaceScene:\
[CCTransitionFade transitionWithDuration:0.7f scene:newScene]];
}
......
[scene:newScene] does not seem to be valid ObjC syntax, does this even compile?
Anyway, you'll want to use newScene there and nothing else:
-(void) restartScene
{
CCScene * newScene = [GameScene scene];
[[CCDirector sharedDirector] replaceScene:newScene];
}

cocos2d:Drawing CCSprite from one layer to another

cocos2d:In general , this situation : There are two layers :
1) HelloWorldLayer (main ) and Hudlayer :
# implementation HelloWorldLayer.
+ (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];
    
    HudLayer * hud = [HudLayer node];
    [scene addChild: hud];
    layer.hud = hud;
/ / Return the scene
return scene;
}
- (id) init
{
if ((self = [super init])) {
...}
In HelloWorldLayer have a method that should be added to the layer CCSprite HelloWorldLayer
- (void) CreateSprite {
        CGSize winSize = [CCDirector sharedDirector]. WinSize;
        CCSprite * sprite = [CCSprite spriteWithFile: # "bird.png"];
                    sprite.position = ccp (winSize.width / 2 , winSize.height / 2);
        [self addChild: sprite];
}
In Hudlayer a button and a method that works when you press the
# implementation HudLayer
- (Id) init
{
    self = [super init];
    if (self) {
                CGSize size = [CCDirector sharedDirector]. WinSize;
       CCMenuItem * Button = [CCMenuItemImage
                    itemFromNormalImage: # "1.png" selectedImage: # "1.png" target: self selector: # selector (Build :)];
        CCMenu * menu = [CCMenu menuWithItems: Button, nil];
        [menu alignItemsHorizontallyWithPadding: 20] ;
        [menu setPosition: ccp (-Button.contentSize.width / 2 + size.width, size.height- 100 ) ] ;
        [self addChild: menu];
    }
    return self;
    
}
- (void) create: (id) sender
{
    HelloWorldLayer * create = [[HelloWorldLayer alloc] init];
    [create CreateSprite];
}
Appears on the screen button , when clicked, the method should work HelloWorldLayer CreateSprite and the screen should be added Tstssprite . Method that works , but why is not the sprite is added , no error is displayed
Actually what you are doing is you are making all new object of HellowroldLayer and then you are calling create function from that object. So your sprite will add to that object. but as your new object is completely different from the old one so you're not getting sprite added to old object.
So in this case what you can do is give tag to your object,like,
self.tag = 777;
then in your create method,you can take that object from tag and then you can create method.
CCScene* scene1 = [[CCDirector sharedDirector] runningScene];
if (scene1) {
id layer1 = [scene1 getChildByTag:777];
[layer1 CreateSprite];
}
No, need to make new object.

Cocos2d - Alternative for CCNode userData

I am using CCNode userData property to define whether a touched sprite has had a node built upon it (using Ray Wenderlich's - Tower Defence Tutorial).
The problem I am having is that for every new sprite position I touch and build upon, the previous ones are not retained in the sense that I am unable to sell the building built on it using my sell method.
What is the best way to save data to an individual CCSprite that is selected by touch, and defined by a property through a for loop?
I have read that subclassing CCSprite is an option, or creating a struct for userData etc. I have tried to implement these strategies but have been unsuccessful getting them to work.
touch method with sprite property:
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
for(UITouch *touch in touches){
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
for(CCSprite *tb in self.BuildPositions){
//BuildPositions contains CCSprites from a class defining build positions and contained in a mutable array.
if(CGRectContainsPoint([tb textureRect],[tb convertToNodeSpace:location ])){
_selectedBuildPosition = tb;
[self buildMenuTapped];
}
}
}
}
build menu method:
-(void)buildMenuTapped{
[self removeChild:_buildSelectMenu cleanup:YES];
[self removeChild:_sellBuildingMenu cleanup:YES];
if (_selectedBuildPosition.userData == nil) {
CCMenuItem *buildOption = [CCMenuItemImage itemWithNormalImage:#"building.png" selectedImage:#"building.png" target:self selector: #selector(buildOption)];
buildOption.position = ccpAdd(_selectedBuildPosition.position,ccp(0,40));
_buildSelectMenu = [CCMenu menuWithItems:BuildOption, nil];
_buildSelectMenu.position = CGPointZero;
[self addChild:_buildSelectMenu z:100];
}else{
[self sellBuildingMenu];
}
}
build upon method:
-(void)buildOption{
if([self canBuyBuilding] && !_selectedBuildPosition.userData){
_playerEssence -= kBUILD_COST;
_lvl1BuildNodeInstance = [lvl1BuildNode nodeWithTheGame:self location:_selectedBuildPosition.position];
//method as seen in RW tutorial. Generates a lvl1BuildNode class node within the HelloWorldLayer.
_selectedBuildPosition.userData = (__bridge void*)(_lvl1BuildNodeInstance);
}
}
sell menu method:
-(void)sellBuildingMenu{
[self removeChild:_buildSelectMenu cleanup:YES];
[self removeChild:_sellBuildingMenu cleanup:YES];
CCMenuItem *buildingSell = [CCMenuItemImage itemWithNormalImage:#"sell.png" selectedImage:#"sell.png" target:self selector:#selector(sellBuilding)];
buildingSell.position = ccpAdd(_selectedBuildPosition.position,ccp(40,0));
_sellBuildingMenu = [CCMenu menuWithItems:buildingSell, nil];
_sellBuildingMenu.position = CGPointZero;
[self addChild:_sellBuildingMenu z:100];
}
sell method:
-(void)sellBuilding{
if (_selectedBuildPosition.userData == (__bridge void*)(_lvl1BuildNodeInstance)){
[self removeChild:_lvl1GemNodeInstance cleanup:YES];
[_buildings removeObject:_lvl1BuildNodeInstance];
_playerEssence += kBUILD_COST;
_selectedBuildPosition.userData = nil;
}else{
}
}
Thank you for your time.

cocos2d add layers on scene

I know that my question might be stupid but I searched and I can't find why it's not working.
I create a CCLayer class BackgroundLayer with implementation below:
#import "BackgroundLayer.h"
#implementation BackgroundLayer
- (id)init {
if (self != nil) {
CCSprite *background = [CCSprite spriteWithFile:#"menu.png"];
background.anchorPoint = ccp(0, 0);
[self addChild:background z:-1];
NSLog(#"test");
}
return self;
}
#end
and I want to add it on main menu scene and I have:
#import "MainMenuScene.h"
#import "BackgroundLayer.h"
#implementation MainMenuScene
+ (id)scene {
CCScene *scene = [CCScene node];
BackgroundLayer *backgroundLayer = [BackgroundLayer node];
[scene addChild:backgroundLayer];
return scene;
}
- (id)init {
self = [super init];
if (self != nil) {
}
return self;
}
#end
My problem is that NSLog test appears but the background doesn't load. If I add the background on the init method of MainMenuScene it works... Shouldn't I suppose that the layer works this way?
Not sure if related but you forgot self = [super init]; in BackgroundLayer.
Try commenting out the anchor point line to see if the image shows up then.

how to do a function for a sprite or menu in cocos2d?

I created two CCLayers, one is gamelayer, another is howlayer. The code of gamelayer.m is
-(id)init{
if (self = [super init]) {
CCSprite *gamebg = [CCSprite spriteWithFile:#"bg.png"];
gamebg.anchorPoint = CGPointZero;
[self addChild:gamebg z:0 tag:1];
HowLayer *howLayer = [HowLayer node];
[self addChild:howLayer];
[self schedule:#selector(showthegamecontent:) interval:0.4];
}
return self;
}
the code of howlayer is
-(id)init{
if (self=[super init]) {
CCSprite *howbg = [CCSprite spriteWithFile:#"translucentbg.png"];
howbg.anchorPoint = CGPointZero;
[self addChild:howbg z:5 tag:1];
CCMenuItem *howmenu = [CCMenuItemImage itemFromNormalImage:#"how.png"
selectedImage:#"how.png"
target:self
selector:#selector(startgame:)];
CCMenu *ccMenuhowmenu = [CCMenu menuWithItems:howmenu, nil];
ccMenuhowmenu.position=ccp(517,384);
[self addChild:ccMenuhowmenu z:5 tag:2];
}
return self;
}
-(void)startgame:(id)sender{
[self removeAllChildrenWithCleanup:YES];
}
I want to do function like this:
When I click the menu on howlayer, the Howlayer will be removed (I have done), and then the game starts, calls the selector 'showthegamecontent', so how should I do?
Simple hack in your howlayer:
-(void)startgame:(id)sender{
gameLayer* parent = (gameLayer*) self.parent;
[parent showthegamecontent];
}
but it may leave you with a warning.. But it works..
The implementation without warning is that you have to store a reference to the parent with you init. Which i feel its unnecessary as you only need to reference it once.