cocos2d:Drawing CCSprite from one layer to another - cocos2d-iphone

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.

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];
}

rewrite the code cocos2d (objective-c) on cocos2d-x (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);
}

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.

Cocos2d Displaying a Layer Issue

I'm trying to display a pause game layer from the applicationDidEnterBackground: method and for some reason it does call the method but nothing happens.
Delegate
- (void)applicationDidEnterBackground:(UIApplication*)application {
ship = [[Ship alloc] init];
[ship pause];
Pause Method
- (void)pause
{
BOOL isPaused = [[CCDirector sharedDirector] isPaused];
if(!isPaused)
{
//Pause the game
ccColor4B c = {100,100,0,100};
PauseLayer *pauseLayer = [[[PauseLayer alloc] initWithColor:c] autorelease];
[self.leftMenuItem setIsEnabled:NO];
[self.rightMenuItem setIsEnabled:NO];
[self.fireMenuItem setIsEnabled:NO];
[self addChild:pauseLayer z:10 tag:100];
[[CCDirector sharedDirector] pause];
}
}
PauseLayer
+ (id)scene
{
CCScene *scene = [CCScene node];
PauseLayer *layer = [PauseLayer node];
[scene addChild:layer];
return scene;
}
- (id)initWithColor:(ccColor4B)color
{
if((self = [super initWithColor:color]))
{
self.isTouchEnabled = YES;
[CCMenuItemFont setFontName:#"Marker Felt"];
[CCMenuItemFont setFontSize:40];
CCMenuItemFont *resumeGameItem = [CCMenuItemFont itemFromString:#"Resume" target:self selector:#selector(resumeGame)];
CCMenuItemFont *menuGameItem = [CCMenuItemFont itemFromString:#"Menu" target:self selector:#selector(goToGameMenu)];
CCMenu *menu = [CCMenu menuWithItems:resumeGameItem,menuGameItem,nil];
[menu alignItemsVerticallyWithPadding:40.00];
[self addChild:menu];
}
return self;
}
Thanks!
If you init the ship in the delegate, it isn't added to any cocos layer that I can see. you would have to get a reference to the current scene and add the ship to it (assuming ship is a sub-class of Cocos node).