cocos2d-x学习(9)----大鱼吃小鱼(一)

        这个一个大鱼吃小鱼的游戏Demo,我们来按照游戏来解说代码。

       一进游戏,我们可以看到游戏的menu界面:



详细代码:

HelloWorldScene.h

class HelloWorld : public cocos2d::CCLayer
{
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();
 
	//a selector callback
	//NewGame 回调函数
	virtual void menuNewGameCallback(CCObject* pSender);

	// a selector callback
	//Option 回调函数
	virtual void menuOptionCallback(CCObject* pSender);

	//About 回调函数
	virtual void menuAboutCallback(CCObject* pSender);

    // a selector callback
	//Quit 回调函数
    void menuCloseCallback(CCObject* pSender);

    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
};

HelloWorldScene.cpp

CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::create();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        //
        // super init first
        //

        CC_BREAK_IF(! CCLayer::init());
		
        //
        // add your codes below...
        //

		std::string titleStr = "Angry Fish";		
		//CCLabelTTF* pLable = CCLabelTTF::labelWithString(titleStr.c_str(),"Thonburi",30);
		CCLabelTTF* pLable = CCLabelTTF::create(titleStr.c_str(),"Thonburi",30);
		CC_BREAK_IF(!pLable);
		pLable->setColor(ccRED);
		CCSize winSize = CCDirector::sharedDirector()->getWinSize();
		pLable->setPosition(ccp(winSize.width/2,winSize.height*0.8f));
		this->addChild(pLable,1);


		//CCSprite* pSprite = CCSprite::spriteWithFile("bg01.jpg");
		CCSprite* pSprite = CCSprite::spriteWithFile("bg01.png");
		CC_BREAK_IF(!pSprite);
		//pSprite->setScaleX(winSize.width/pSprite->getContentSizeInPixels().width);
		pSprite->setScaleX(winSize.width/pSprite->getContentSize().width);
		//pSprite->setScaleY(winSize.height/pSprite->getContentSizeInPixels().height);
		pSprite->setScaleY(winSize.height/pSprite->getContentSize().height);
		pSprite->setPosition(ccp(winSize.width/2,winSize.height/2));
		this->addChild(pSprite,0);


		//游戏目录
		CCMenuItemFont::setFontName("Thonburi");
		CCMenuItemFont::setFontSize(25);
		std::string menuItemStr="NewGame";
		//CCMenuItemFont* newGame = CCMenuItemFont::itemFromString(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuNewGameCallback));
		CCMenuItemFont* newGame = CCMenuItemFont::create(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuNewGameCallback));
		menuItemStr = "Option";
		//CCMenuItemFont* option = CCMenuItemFont::itemFromString(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuOptionCallback));
		CCMenuItemFont* option = CCMenuItemFont::create(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuOptionCallback));
		menuItemStr = "About";
		//CCMenuItemFont* about = CCMenuItemFont::itemFromString(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuAboutCallback));
		CCMenuItemFont* about = CCMenuItemFont::create(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuAboutCallback));
		menuItemStr = "Quit";
		//CCMenuItemFont* quit = CCMenuItemFont::itemFromString(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuCloseCallback));
		CCMenuItemFont* quit = CCMenuItemFont::create(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuCloseCallback));
		
		CCMenu* menu = CCMenu::menuWithItems(newGame,option,about,quit,NULL);
		menu->setPosition(ccp(winSize.width/2,winSize.height/2));
		menu->alignItemsVertically();
		addChild(menu,1);

        bRet = true;
    } while (0);

    return bRet;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCDirector::sharedDirector()->end();
}

void HelloWorld::menuOptionCallback(CCObject* pSender)
{
	CCScene* scene = SettingScene::scene();
	CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::transitionWithDuration(1.2f,scene));

}


void HelloWorld::menuNewGameCallback(CCObject* pSender)
{
	CCScene* scene = GameScene::scene();
	CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::transitionWithDuration(1.2f,scene));
}

void HelloWorld::menuAboutCallback(CCObject* pSender)
{
	//CCScene* scene = AboutScene::scene();
	CCScene* scene = AboutScene::scene();
	CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::transitionWithDuration(1.2f,scene));
}


关于界面:



详细代码:

AboutScene.h

class AboutScene : public cocos2d::CCLayer
{
public:

	virtual bool init();
	static cocos2d::CCScene* scene();
	void backCallback(cocos2d::CCObject* pSender);
    // implement the "static node()" method manually
    CREATE_FUNC(AboutScene);
};

AboutScene.cpp

cocos2d::CCScene* AboutScene::scene()
{
	CCScene * scene = NULL;
	do 
	{
		// 'scene' is an autorelease object
		scene = CCScene::create();
		CC_BREAK_IF(! scene);

		// 'layer' is an autorelease object
		AboutScene* layer = AboutScene::create();
		CC_BREAK_IF(! layer);

		// add layer as a child to scene
		scene->addChild(layer);
	} while (0);

	// return the scene
	return scene;
}


bool AboutScene::init()
{
	bool bRet = false;
	do 
	{
		//init background
		CCSize winSize = CCDirector::sharedDirector()->getWinSize();

		CCSprite* pSprite = CCSprite::spriteWithFile("bg01.png");
		pSprite->setScaleX(winSize.width/pSprite->getContentSize().width);
		pSprite->setScaleY(winSize.height/pSprite->getContentSize().height);
		pSprite->setPosition(ccp(winSize.width/2,winSize.height/2));
		this->addChild(pSprite,0);

		std::string titleStr = "Angry Fish\n\n Write by Cocos2d-x\n\n Author:freeman";
		CCLabelTTF* pLable = CCLabelTTF::labelWithString(titleStr.c_str(),"Thonburi",30);
		CC_BREAK_IF(!pLable);
		pLable->setColor(ccBLUE);
		
		//pLable->setAnchorPoint(ccp(0,1));
		pLable->setPosition(ccp(winSize.width/2,winSize.height/2));
		this->addChild(pLable,1);


		std::string str;
		str="Back";
		//CCMenuItemFont* backMenu = CCMenuItemFont::itemFromString(str.c_str(),this,menu_selector(AboutScene::backCallback));
		CCMenuItemFont* backMenu = CCMenuItemFont::create(str.c_str(),this,menu_selector(AboutScene::backCallback));
		backMenu->setAnchorPoint(ccp(1,0));
		backMenu->setPosition(ccp(winSize.width,0));

		CCMenu* mn = CCMenu::menuWithItems(backMenu,NULL);
		mn->setPosition(ccp(0,0));
		this->addChild(mn,1);

		bRet = true;

	} while (0);
	return(bRet);
}


void AboutScene::backCallback(cocos2d::CCObject* pSender)
{
	CCScene * scene=HelloWorld::scene();
	CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::transitionWithDuration(1.2f,scene));
}

选项界面:



详细代码:

global.h

extern bool g_isMusic;
extern bool g_isSound;

global.cpp

bool g_isSound = true;

bool g_isMusic = true;

SettingScene.h

class SettingScene :public cocos2d::CCLayer
{
public:
	virtual bool init();

	static cocos2d::CCScene* scene();

	void backCallback(cocos2d::CCObject* pSender);
	void soundCallback( cocos2d::CCObject *pSender );
	void musicCallback( cocos2d::CCObject *pSender );
	   
	//LAYER_NODE_FUNC(SettingScene);
	CREATE_FUNC(SettingScene);
};

SettingScene.cpp

cocos2d::CCScene* SettingScene::scene()
{
	CCScene * scene = NULL;
	do 
	{
		// 'scene' is an autorelease object
		scene = CCScene::create();
		CC_BREAK_IF(! scene);

		// 'layer' is an autorelease object
		SettingScene *layer = SettingScene::create();
		CC_BREAK_IF(! layer);

		// add layer as a child to scene
		scene->addChild(layer);
	} while (0);

	// return the scene
	return scene;
}


bool SettingScene::init()
{
	bool bRet = false;
	do 
	{
		CCSize winSize = CCDirector::sharedDirector()->getWinSize();

		CCSprite* pSprite = CCSprite::spriteWithFile("bg01.png");
		pSprite->setScaleX(winSize.width/pSprite->getContentSize().width);
		pSprite->setScaleY(winSize.height/pSprite->getContentSize().height);
		pSprite->setPosition(ccp(winSize.width/2,winSize.height/2));
		this->addChild(pSprite,0);



		CCMenuItemFont::setFontName("Thonburi");
		CCMenuItemFont::setFontSize(30);

		std::string str;
		str = "Music";
		//CCMenuItemFont *music = CCMenuItemFont::itemFromString(str.c_str(),NULL,NULL);
		CCMenuItemFont *music = CCMenuItemFont::create(str.c_str(),NULL,NULL);
		//music->setIsEnabled(false);
		music->setEnabled(false);

		str = "Open";
		//CCMenuItemFont *openMusic = CCMenuItemFont::itemFromString(str.c_str());
		CCMenuItemFont *openMusic = CCMenuItemFont::create(str.c_str());
		str = "Close";
		//CCMenuItemFont *closeMusic = CCMenuItemFont::itemFromString(str.c_str());
		CCMenuItemFont *closeMusic = CCMenuItemFont::create(str.c_str());
		CCMenuItemToggle *musicItem = CCMenuItemToggle::itemWithTarget(this,menu_selector(SettingScene::musicCallback),openMusic,closeMusic,NULL);
		musicItem->setSelectedIndex(g_isMusic?0:1);

		str = "Sound";
		//CCMenuItemFont *sound = CCMenuItemFont::itemFromString(str.c_str(),NULL,NULL);
		CCMenuItemFont *sound = CCMenuItemFont::create(str.c_str(),NULL,NULL);
		//sound->setIsEnabled(false);
		sound->setEnabled(false);

		str = "Open";
		//CCMenuItemFont *openSound = CCMenuItemFont::itemFromString(str.c_str());
		CCMenuItemFont *openSound = CCMenuItemFont::create(str.c_str());
		str = "Close";
		//CCMenuItemFont *closeSound = CCMenuItemFont::itemFromString(str.c_str());
		CCMenuItemFont *closeSound = CCMenuItemFont::create(str.c_str());

		CCMenuItemToggle *soundItem = CCMenuItemToggle::itemWithTarget(this,menu_selector(SettingScene::soundCallback),openSound,closeSound,NULL);
		soundItem->setSelectedIndex(g_isSound?0:1);

		str="Back";
		//CCMenuItemFont* back = CCMenuItemFont::itemFromString(str.c_str(),this,menu_selector(SettingScene::backCallback));
		CCMenuItemFont* back = CCMenuItemFont::create(str.c_str(),this,menu_selector(SettingScene::backCallback));

		CCMenu* menu = CCMenu::menuWithItems(music,musicItem,sound,soundItem,back,NULL);
		this->addChild(menu);
		menu->alignItemsInColumns(2,2,1);

		bRet = true;

	} while (0);
	return(bRet);
}

void SettingScene::backCallback(cocos2d::CCObject* pSender)
{
	CCScene* scene = HelloWorld::scene();
	CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::transitionWithDuration(1.2f,scene));
}

void SettingScene::soundCallback( cocos2d::CCObject *pSender )
{
	g_isSound=!g_isSound;
}

void SettingScene::musicCallback( cocos2d::CCObject *pSender )
{
	g_isMusic=!g_isMusic;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hfreeman2008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值