can a function receive two arguements one each from two different functions? - c++

i have 2 void functions(trying to implement radio button), i want them to send value to a third function by swapping values. and that function returning value to main function?
CODE OF MY MyScene.h FILE
#ifndef __MY_SCENE_H__
#define __MY_SCENE_H__
#include "cocos2d.h"
USING_NS_CC;
class MyScene : public cocos2d::CCLayerColor
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommand to return the exactly class pointer
static cocos2d::CCScene* scene();
CCMenuItemToggle *R;
CCMenuItemToggle *L;
// a selector callback
void swapL(CCObject *sender);
void swapR(CCObject *sender);
// implement the "static node()" method manually
LAYER_NODE_FUNC(MyScene);
};
#endif // __HELLOWORLD_SCENE_H__
CODE OF MY MyScene.cpp FILE
#include "MyScene.h"
USING_NS_CC;
CCScene* MyScene::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::node();
// 'layer' is an autorelease object
MyScene *layer = MyScene::node();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool MyScene::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayerColor::initWithColor(ccc4(255,255,255,255) ))
{
return false;
}
//////////////////////////////
// 2. add your codes below...
CCSize WinSize= CCDirector::sharedDirector()->getWinSizeInPixels();
CCSprite * fish=CCSprite::spriteWithFile("fish_bg.png");
fish->setPosition(CCPointZero);
fish->setAnchorPoint(CCPointZero);
fish->setScaleX(WinSize.width/480);
fish->setScaleY(WinSize.height/395);
this->addChild(fish,0,0);
CCSprite * on1=CCSprite::spriteWithFile("on.png");
CCSprite * on2=CCSprite::spriteWithFile("on.png");
CCSprite * on3=CCSprite::spriteWithFile("on.png");
CCSprite * on4=CCSprite::spriteWithFile("on.png");
CCSprite * off1=CCSprite::spriteWithFile("off.png");
CCSprite * off2=CCSprite::spriteWithFile("off.png");
CCSprite * off3=CCSprite::spriteWithFile("off.png");
CCSprite * off4=CCSprite::spriteWithFile("off.png");
CCMenuItem *LeftOn=CCMenuItemSprite::itemFromNormalSprite(on1,on2);
CCMenuItem *RightOn=CCMenuItemSprite::itemFromNormalSprite(on3,on4);
CCMenuItem *LeftOff=CCMenuItemSprite::itemFromNormalSprite(off1,off2);
CCMenuItem *RightOff=CCMenuItemSprite::itemFromNormalSprite(off3,off4);
CCMenuItemToggle *Left = CCMenuItemToggle::itemWithTarget(this, menu_selector(MyScene::swapL),LeftOn,LeftOff,NULL);
CCMenuItemToggle *Right = CCMenuItemToggle::itemWithTarget(this, menu_selector(MyScene::swapR),RightOn,RightOff,NULL);
CCMenu *Radio= CCMenu::menuWithItems(Left,Right,NULL);
Radio->alignItemsHorizontallyWithPadding(20);
Radio->setPosition(ccp(WinSize.width/2,WinSize.height/2));
this->addChild(Radio);
//////////////////////////////
return true;
}
void MyScene::swapL(CCObject *sender)
{
L= (CCMenuItemToggle*)sender;
CCLOG("L= %d",L->getSelectedIndex());
int i=(L->getSelectedIndex());
}
void MyScene::swapR(CCObject *sender)
{
R= (CCMenuItemToggle*)sender;
CCLOG("R= %d",R->getSelectedIndex());
int j=(R->getSelectedIndex());
}

Is it possible to have 2 void functions to send arguements to a third function one each from those 2 functions ?
Yes, It's possible, Why do you think it is not possible?
Online Sample:
#include<iostream>
void doSomething(int &i)
{
i = 10;
}
void doSomethingMore(int &j)
{
j = 20;
}
void FinalDoSomething(const int i, const int j, int &k)
{
k = i + j;
}
int main()
{
int i = 0;
doSomething(i);
int j = 0;
doSomethingMore(j);
int k = 0;
FinalDoSomething(i,j,k);
std::cout<<k;
return 0;
}

You could have a method between that calls. This function stores the first call value into a member and on the second call it calls the function you want to pass the two parameters (using the previously passed one + the member

Related

Cocos2d-x Scene MenuItemLabel CC_CALLBACK_1 error

OS : Win10
Language : c++
Cocos2d-x Ver : 3.8.1
Tool : Visual Studio 2013
//MainMenuScene.h
#ifndef ProjectV_MainMenuScene_h
#define ProjectV_MainMenuScene_h
#include "cocos2d.h"
class MainMenuScene :public cocos2d::CCLayerColor
{
public:
virtual bool init();
static cocos2d::CCScene *createScene();
CREATE_FUNC(MainMenuScene);
cocos2d::Label *PlayLabel;
void goPlayScene(cocos2d::Ref *pSender);
};
#endif
//MainMenuScene.cpp
#include "MainMenuScene.h"
#include "PlayScene.h"
USING_NS_CC;
CCScene *MainMenuScene::createScene()
{
CCScene *scene = CCScene::create();
MainMenuScene *layer = MainMenuScene::create();
scene->addChild(layer);
return scene;
}
bool MainMenuScene::init()
{
if (!CCLayerColor::initWithColor(Color4B(255, 255, 255, 255)))
{
return false;
}
PlayLabel = Label::createWithTTF("Play", "fonts/consola.ttf", 18);
PlayLabel->setColor(Color3B::BLACK);
auto PlayBtn = MenuItemLabel::create(
PlayLabel,
CC_CALLBACK_1(MainMenuScene::goPlayScene, this));
// i thought CC_CALLBACK_1(MainMenuScene::goPlayScene,this) mean call goPlayScene(Ref* pSender) when click or touch the label
PlayBtn->setPosition(Vec2(240, 100));
auto pMenu = Menu::create(PlayBtn, NULL);
pMenu->setPosition(Vec2::ZERO);
this->addChild(pMenu);
return true;
}
void MainMenuScene::goPlayScene(Ref* pSender)
{
CCScene *pScene = PlayScene::createScene();
TransitionScene *pTransScene = TransitionFade::create(1.0f, pScene, Color3B::WHITE);
Director::getInstance()->replaceScene(pTransScene);
}
i don't know why function don't call when i clicked the label
You is not passed an argument in the calling function
CC_CALLBACK_1(MainMenuScene::goPlayScene, this)
You need either remove the Ref* pSender from void MainMenuScene::goPlayScene() or add an your sender in CC_CALLBACK_1(MainMenuScene::goPlayScene, this) after this.

cocos2d-x beta3 LayerColor doesn't change background color

can't change the color of a background i have this simple class :
here is the c++ file :
#include "HelloWorldScene.h"
USING_NS_CC;
HelloWorld::HelloWorld()
{
;
}
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !LayerColor::initWithColor(Color4B(20,0,0,255)) )
{
return false;
}
winSize = Director::getInstance()->getWinSize();
visibleSize = Director::getInstance()->getVisibleSize();
origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu, 1);
schedule( schedule_selector(HelloWorld::tick) );
return true;
}
void HelloWorld::onExit()
{
LayerColor::onExit();
}
void HelloWorld::onEnter()
{
LayerColor::onEnter();
auto cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("interface/sprites.plist", "interface/sprites.png");
SpriteBatchNode* batch = SpriteBatchNode::create("interface/sprites.png");
this->addChild(batch,BATCH_Z);
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
void HelloWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
{
for( auto& touch : touches)
{
}
}
void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
{
for( auto& touch : touches)
{
}
}
void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
{
for( auto& touch : touches)
{
startAnim = true;
};
}
void HelloWorld::tick(float dt)
{
if(startAnim)
{
}
}
void HelloWorld::draw()
{
LayerColor::draw();
}
HelloWorld::~HelloWorld()
{
// Removes Touch Event Listener
_eventDispatcher->removeEventListener(_touchListener);
}
void HelloWorld::menuCloseCallback(Object* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
and the h file :
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class GameObj;
class ReelGameObj;
class HelloWorld : public cocos2d::LayerColor
{
public:
HelloWorld();
~HelloWorld();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(Object* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
void tick(float dt);
virtual void draw();
virtual void onEnter();
virtual void onExit();
void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
protected:
cocos2d::CustomCommand _customCommand;
void onDraw();
private:
cocos2d::EventListenerTouchOneByOne* _touchListener;
cocos2d::Size winSize;
cocos2d::Size visibleSize;
cocos2d::Point origin;
GameObj* pMainWindowObjCenter;
ReelGameObj* pReelGameObj;
bool startAnim;
};
#endif // __HELLOWORLD_SCENE_H__
and nothing no color in the background , why ?
im working on windows with VC 2012
I think your color is too dark. Try changing the values to (255,25,255,255) and check the result.
I created a sample project (in Beta 2), and only changed these lines:
.h:
class HelloWorld : public cocos2d::LayerColor
.cpp:
if( !LayerColor::initWithColor(Color4B(255,255,255,255)) )
The result is a white background.
I cleaned up and updated your code (I use cocos2dx 2.2.1) and I could change the layer's color to red.
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayerColor
{
public:
HelloWorld();
~HelloWorld();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
virtual void onEnter();
void draw();
private:
cocos2d::CCSize winSize;
cocos2d::CCSize visibleSize;
cocos2d::CCPoint origin;
bool startAnim;
};
#endif // __HELLOWORLD_SCENE_H__
The cpp file
#include "HelloWorldScene.h"
USING_NS_CC;
HelloWorld::HelloWorld()
{}
HelloWorld::~HelloWorld()
{}
CCScene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = CCScene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
ccColor4B c = ccc4(255,0,0,255);
if ( !CCLayerColor::initWithColor(c) )
{
return false;
}
return true;
}
void HelloWorld::onEnter()
{
CCLayerColor::onEnter();
}
void HelloWorld::draw()
{
CCLayerColor::draw();
}

Bad Access exception in Cocos2d-x

I am somewhat new to C++ programming and to Cocos2d-x and I am having some trouble with my first Cocos2d-x game. I am literally just trying to simulate gravity and update the player's position based on that. I am running Xcode 4.6.1 and for some reason I keep getting a BAD_ACCESS exception after one run (if I restart it seems to work). It happens at libobjc.A.dylib`objc_release. I have tried doing an allexceptions breakpoint and it breaks at a memory address in the dylib. This only happened after I added a velocity variable to my Player class, so maybe I am not properly allocating pointers and the such? Here are the relevant classes I believe.
#include "cocos2d.h"
using namespace cocos2d;
class Player : public Sprite{
private:
Point velocity;
public:
static Player* create(const char *filename);
void update(float delta);
void setVelocity(const Point &v);
const Point& getVelocity() const;
~Player();
};
#include "Player.h"
USING_NS_CC;
using namespace cocos2d;
Player::~Player(){
}
Player* Player::create(const char *filename){
Player* self = (Player *) Sprite::create(filename);
self->setVelocity(Point::ZERO);
return self;
}
void Player::update(float delta){
log("In player update");
Point gravity = Point(0.0, -450.0);
Point gravityStep = gravity * delta;
this->setVelocity(this->getVelocity() + gravityStep);
log("Velocity after setting: %f", getVelocity().y);
Point stepVelocity = this->getVelocity() * delta;
this->setPosition(this->getPosition() + stepVelocity);
log("Position after setting: %f, %f", getPosition().x, getPosition().y);
}
void Player::setVelocity(const Point &v){
this->velocity = v;
}
const Point& Player::getVelocity() const
{
return this->velocity;
}
#include "GameLayer.h"
USING_NS_CC;
Scene* GameLayer::scene()
{
// 'scene' is an autorelease object
Scene *scene = Scene::create();
// 'layer' is an autorelease object
GameLayer *layer = GameLayer::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
GameLayer::~GameLayer(){
CC_SAFE_RELEASE(player);
CC_SAFE_RELEASE(map);
}
bool GameLayer::init()
{
if ( !Layer::init() )
{
return false;
}
LayerColor *blueSky = LayerColor::create(*new Color4B(100, 100, 250, 255));
this->addChild(blueSky);
map = TMXTiledMap::create("level1.tmx");
this->addChild(map);
player = Player::create("koalio_stand.png");
player->setPosition(Point(100,50));
map->addChild(player, 15);
setKeyboardEnabled(true);
this->scheduleUpdate();
return true;
}
void GameLayer::update(float dt){
player->update(dt);
}
EDIT: So I figured out that the constructor of player was the problem. However, I'm wondering if it is kosher to do the following (since Player inherits from Sprite) to copy the Sprite function:
Sprite* Sprite::create(const char *filename)
{
Sprite *sprite = new Sprite();
if (sprite && sprite->initWithFile(filename))
{
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE(sprite);
return NULL;
}
and have the Player function be:
Player* Player::create(const char *filename){
Player *sprite = new Player();
if (sprite && sprite->initWithFile(filename))
{
sprite->setVelocity(Point::ZERO);
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE(sprite);
return NULL;
}
Or perhaps is there a cleaner way to call the create of Sprite, but to create a Player object which inherits and sets the velocity?

Schedule selector error

I'm trying to port my iPhone app to windows 8, I have a problem with this line (it's objective-c) :
[self schedule:#selector(fire:)];
The equivalent on c++ should be :
this->schedule(schedule_selector(AirScene::fire));
Where AirScene is the name of my class, but I have this error returning by Visual Studio 2012 :
error C2064: term does not evaluate to a function taking 1 arguments
So, in other words the function schedule(selector) is not found. It's strange because I have no problem with unschedule method, do you have any idea please ?
EDIT : AirScene.h
#include "cocos2d.h"
#include "Box2D\Box2D.h"
#include "AirSceneDelegate.h"
class AirScene : public cocos2d::CCLayer {
public:
AirScene::~AirScene(void);
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
static cocos2d::CCScene* scene();
LAYER_NODE_FUNC(AirScene);
//Methods
void selectSpriteForTouch(cocos2d::CCPoint touchLocation);
cocos2d::CCPoint checkPuckPosition(cocos2d::CCPoint newPosition);
void panForTranslation(cocos2d::CCPoint translation);
void updateQuestion(string question);
void restoreScene();
void enableScreensaver();
void disableScreensaver(cocos2d::CCPoint touchPosition);
//Setters
void setDelegate(AirSceneDelegate *delegate);
private:
// Init
void initBackground(cocos2d::CCSize winSize);
void initPuck(cocos2d::CCSize winSize, cocos2d::CCPoint position);
void initHoles(cocos2d::CCSize winSize);
void initQuestion(cocos2d::CCSize winSize);
// Methods
void fire(cocos2d::ccTime dt = 0);
void updateHoles(cocos2d::ccTime dt);
void validateVote(bool isPositiveVote);
float getVelocity();
// Attributes
...
//Delegate
AirSceneDelegate* _delegate;
};
AirScene.cpp
#include <iostream>
#include "AirScene.h"
USING_NS_CC;
#pragma region Delete
AirScene::~AirScene(void)
{
///TODO : DELETE ALL
delete(_delegate);
_delegate = 0;
}
#pragma endregion Delete
#pragma region Init
CCScene* AirScene::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::node();
// 'layer' is an autorelease object
AirScene *layer = AirScene::node();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool AirScene::init()
{
// Super Init
if (!CCLayer::init())
return false;
//Init Attributes
screenSaverMode = false;
hasVoted = false;
schedule = false;
_delegate = 0;
_selSprite = NULL;
//Create World
b2Vec2 gravity = b2Vec2(0, 0);
_world = new b2World(gravity);
CCSize size = CCDirector::sharedDirector()->getWinSize();
//Inits
AirScene::initBackground(size);
AirScene::initPuck(size, ccp(512, 200));
AirScene::initHoles(size);
AirScene::initQuestion(size);
return true;
}
// Init Background and set walls
void AirScene::initBackground(CCSize winSize)
{
...
}
/** Init Puck : create puck body and shape */
void AirScene::initPuck(CCSize winSize, CCPoint position)
{
...
}
void AirScene::initHoles(CCSize winSize)
{
...
}
// Set Question Label
void AirScene::initQuestion(CCSize winSize)
{
...
}
#pragma endregion Init
#pragma region Private
void AirScene::fire(ccTime dt)
{
_world->Step(dt, 8, 8);
//CCSprite *ballData = ((CCSprite *)_body->GetUserData())
((CCSprite *)_body->GetUserData())->setPosition(ccp(_body->GetPosition().x * PTM_RATIO, _body->GetPosition().y * PTM_RATIO));
_puckShadow->setPosition(ccp(((CCSprite *)_body->GetUserData())->getPosition().x + 2, ((CCSprite *)_body->GetUserData())->getPosition().y - 2));
if (screenSaverMode)
AirScene::updateHoles(0);
}
//Ajust Glow Effect and Validate Vote
void AirScene::updateHoles(cocos2d::ccTime dt)
{
...
}
float AirScene::getVelocity()
{
...
}
void AirScene::validateVote(bool isPositiveVote)
{
...
}
#pragma endregion Private
#pragma region Public
void AirScene::selectSpriteForTouch(CCPoint touchLocation)
{
...
}
// Check if the puck is not outside the view
CCPoint AirScene::checkPuckPosition(CCPoint newPosition)
{
...
}
// Move Puck
void AirScene::panForTranslation(CCPoint translation)
{
...
}
// Update Question
void AirScene::updateQuestion(string question)
{
...
}
void AirScene::restoreScene()
{
...
}
void AirScene::enableScreensaver()
{
screenSaverMode = true;
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
//Unschedule actions
this->unscheduleAllSelectors();
//Delete Puck
CCPoint puckPosition = _puck->getPosition();
_puck->removeAllChildrenWithCleanup(true);
_puck->removeFromParentAndCleanup(true);
_puckShadow->removeAllChildrenWithCleanup(true);
_puckShadow->removeFromParentAndCleanup(true);
_world->DestroyBody(_body);
delete(_puck);
delete(_puckShadow);
//RecreatePuck
this->initPuck(winSize, ccp(512, 200));
//Impulse
_body->SetLinearVelocity(b2Vec2(0, 0));
/** ERROR IS CAUSED BY THIS LINE */
this->schedule(schedule_selector(AirScene::fire));
}
void AirScene::disableScreensaver(cocos2d::CCPoint touchPosition)
{
}
#pragma endregion Public
#pragma region Getters & Setters
void AirScene::setDelegate(AirSceneDelegate *delegate)
{
_delegate = delegate;
}
#pragma endregion Getters & Setters
Fixed.
The version of cocos 2d which is used for windows 8 template is an old version, so the good way to schedule a selector is this one :
CCScheduler::sharedScheduler()->scheduleSelector(schedule_selector(AirScene::fire), this, 0, false);

member variables of a class has been changed, in each loop ( cocos2dx )

I 've declared some classes in my cocos2dx program, and I 've set some values for their member variables, but after each time the program loops ( the main loop of the CCDirector ), all of their values have been removed!
I want it class to save its next scene that should be replaced or pushed when the scene should be replaced (as you can see in the code, after 2 second, the introPage class calls a function "IntoPageDone" and the scene should be replaced )and I 've set a variable named nextScene for the next Scene, but after each loop, its value changes into NULL.
plz help me with this problem, and also is there some better ways to handling the scenes and changes of them??
tnx a lot!
StartingPage is a class which is inherited from CCLayerColor.
Here is CoCoGui.h file:
#ifndef _COCOGUI_H_
#define _COCOGUI_H_
#include "StartingPage.h"
#include "IntroPage.h"
using namespace cocos2d;
class CoCoGui : public CCLayerColor{
public:
CoCoGui();
virtual ~CoCoGui(void);
virtual bool init();
static CCScene* scene();
CREATE_FUNC(CoCoGui);
private:
IntroPage * introPage;
StartingPage * startingPage;
void onEnterTransitionDidFinish();
};
#endif /* COCOGUI_H */
And this is CoCoGui.cpp file:
#include "CoCoGui.h"
#include <iostream>
using namespace std;
CCScene* CoCoGui::scene(){
CCScene *scene = CCScene::create();
CoCoGui *layer = CoCoGui::create();
scene->addChild(layer);
return scene;
}
CoCoGui::CoCoGui ( )
{
this->startingPage = new StartingPage ( );
this->introPage = new IntroPage ( );
}
CoCoGui::~CoCoGui(void)
{
delete introPage;
delete startingPage;
}
bool CoCoGui::init ( ){
if ( !CCLayerColor::initWithColor ( ccc4 (100,100,100,255) ) ){
return false;
}
return true;
}
void CoCoGui::onEnterTransitionDidFinish ( ){
this->introPage->setNextScene ( StartingPage::scene( ) );
CCScene * scene = NULL;
scene = IntroPage::scene();
CCTransitionFade * trans = CCTransitionFade::create( 0.4f, scene , ccBLACK);
CCDirector::sharedDirector()->pushScene(trans);
cout << "step" << endl;
}
This is introPage.h file:
#ifndef INTROPAGE_H_
#define INTROPAGE_H_
#include "cocos2d.h"
#include "StartingPage.h"
using namespace cocos2d;
class IntroPage: public CCLayerColor {
public:
IntroPage( CCScene * nextScene );
IntroPage ( );
virtual ~IntroPage();
static CCScene* scene();
bool init();
void intoPageDone();
CREATE_FUNC(IntroPage);
CC_SYNTHESIZE_READONLY(CCLabelTTF*, _label, Label);
CCScene * getNextScene( );
void setNextScene ( CCScene * nScene );
private:
CCScene * nextScene;
};
#endif /* INTROPAGE_H_ */
And also IntroPage.cpp:
#include "IntroPage.h"
IntroPage::IntroPage( CCScene * nextScene ) {
this->introPageDone = false;
this->setNextScene ( nextScene );
}
IntroPage::IntroPage( ){
}
IntroPage::~IntroPage() {
}
void IntroPage::setNextScene ( CCScene * nScene ){
this->nextScene = nScene;
}
CCScene* IntroPage::scene(){
CCScene *scene = CCScene::create();
IntroPage *layer = IntroPage::create();
scene->addChild(layer);
return scene;
}
bool IntroPage::init() {
if (CCLayerColor::initWithColor (ccc4(0, 0, 0, 255) )) {
this->runAction(
CCSequence::actions(CCDelayTime::actionWithDuration(2),
CCCallFunc::actionWithTarget(this,
callfunc_selector(IntroPage::intoPageDone)),
NULL));
return true;
}
return false;
}
void IntroPage::intoPageDone() {
this->introPageDone = true;
CCDirector::sharedDirector( )->popScene( );
CCDirector::sharedDirector( )->pushScene( this->nextScene );
}
Finally I found the answer!
We should use static variable, so they won't destructed after main loop