cocos2d-x学习(1)-------捕鱼达人的界面实现

时间:2023-02-08 10:50:25

首先做一些说明:

(1)开发工具:

vc2008      cocos2d-2.0-x-2.0.4

(2)实现的效果图(气泡的实际效果,比我做的图片要好许多的,这个要把代码运行起来就知道了)

cocos2d-x学习(1)-------捕鱼达人的界面实现


(3)用到的知识点:

1.CCSprite:   创建图片

2.CCMenuItemImage    CCMenu:创建目录

3.schedule: 定时器

4.CocosDenshion::SimpleAudioEngine::sharedEngine():播放音乐

5.CocosDenshion::SimpleAudioEngine::sharedEngine():播放音效

6.CCLog:Log:日志

7.CCRANDOM_0_1():随机数

8.CCMoveTo:移动动画

9.runAction:播放动画方法

10.开启触摸功能:

//set tounch funtion enable
this->setTouchEnabled(true);

void HelloWorld::registerWithTouchDispatcher()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority + 1, true);
}

bool HelloWorld::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
    CCPoint touchLocation = touch->getLocation();
   // updateSize(touchLocation);
	CCLog("ccTouchBegan:touchLocation: touchLocation.x=%.2f ,touchLocation.y:=%.2f",touchLocation.x,touchLocation.y);
    return true;
}

void HelloWorld::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
    CCPoint touchLocation = touch->getLocation();
    //updateSize(touchLocation);
	CCLog("ccTouchMoved:touchLocation: touchLocation.x=%.2f ,touchLocation.y:=%.2f",touchLocation.x,touchLocation.y);
}

void HelloWorld::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
    CCPoint touchLocation = touch->getLocation();
    //updateSize(touchLocation);
	CCLog("ccTouchEnded:touchLocation: touchLocation.x=%.2f ,touchLocation.y=%.2f",touchLocation.x,touchLocation.y);
}


(4)关键代码:

1.HelloWorldScene.h

class HelloWorld : public cocos2d::CCLayer
{
public:

	float locaitonH;
	float maxH;
	float maxW;

    // 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();

	virtual void onEnter();
    
    // a selector callback

	void startGame(CCObject* pSender);
	void selectGame(CCObject* pSender);

	void helpGame(CCObject* pSender);
	void settingGame(CCObject* pSender);

	void updateBubble(float dt);

	//Touch function
	virtual void registerWithTouchDispatcher();

	virtual bool ccTouchBegan(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);
    virtual void ccTouchMoved(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);
    virtual void ccTouchEnded(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);

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

2.HelloWorldScene.cpp

#include "HelloWorldScene.h"

using namespace cocos2d;

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


void HelloWorld::onEnter() 
{ 
    CCLayer::onEnter(); 
} 

// 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...
        //////////////////////////////////////////////////////////////////////////

		//set tounch funtion enable
		this->setTouchEnabled(true);

		// Get window size and place the label upper. 
        CCSize size = CCDirector::sharedDirector()->getWinSize();
		//MaxH = size.height;

        // 1. Add bg image.
        CCSprite* pSpriteBg = CCSprite::create("bg.png");
        CC_BREAK_IF(! pSpriteBg);

        // Place the sprite on the center of the screen
        pSpriteBg->setPosition(ccp(size.width/2, size.height/2));

        // Add the sprite to HelloWorld layer as a child layer.
        this->addChild(pSpriteBg, 0);

		//CCSprite* bubble = CCSprite::create("water.png");
		//bubble->setPosition(ccp(size.width/2,size.height/6));
		//bubble->setScale(0.5);
		//this->addChild(bubble,2);

		locaitonH = size.height/6;
		maxH = size.height;
		maxW = size.width;

        // 2. Add title image.
		CCSprite *spritetile=CCSprite::create("title.png");
		CCSize s=spritetile->getContentSize();
		spritetile->setPosition(ccp(size.width/2,size.height-s.height/2));
		//add to 1 order
		this->addChild(spritetile,1);

        // 3. Add select and start Menu
		CCMenuItemImage *start=CCMenuItemImage::create("start.png",//normal image
			"start.png",//pressed image
			this,
			menu_selector(HelloWorld::startGame));
		CCSize startSize=start->getContentSize();
		start->setPosition(ccp(0,size.height/2));


		CCMenuItemImage *select=CCMenuItemImage::create("select.png",
			"select.png",
			this,
			menu_selector(HelloWorld::selectGame));
		CCSize selectSize=select->getContentSize();
		select->setPosition(ccp(0,size.height/2-startSize.height));

		CCMenu *menu=CCMenu::create(start,select,NULL);//创建菜单,并将两个菜单项传入		
		menu->setPosition(ccp(size.width/2,0));//设置菜单的位置,若不设置则为屏幕的正中间
		addChild(menu,1);


		// 4. Add set and help Menu
		CCMenuItemImage *help=CCMenuItemImage::create("help.png",//normal image
			"help.png",//pressed image
			this,
			menu_selector(HelloWorld::helpGame));
		CCSize helpSize=help->getContentSize();
		help->setPosition(ccp(0,size.height/4));


		CCMenuItemImage *setting=CCMenuItemImage::create("setting.png",
			"setting.png",
			this,
			menu_selector(HelloWorld::settingGame));

		CCSize settingSize=setting->getContentSize();
		setting->setPosition(ccp(0,size.height/4 -helpSize.height));

		CCMenu *menu_2=CCMenu::create(help,setting,NULL);//创建菜单,并将两个菜单项传入		
		menu_2->setPosition(ccp(size.width/6,0));//设置菜单的位置,若不设置则为屏幕的正中间
		addChild(menu_2,1);
      
		schedule( schedule_selector(HelloWorld::updateBubble), 3); 

		//play music
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background-music.mp3", true);

		//play effect
		//CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE);  

        bRet = true;
    } while (0);

    return bRet;
}

void HelloWorld::startGame(CCObject* pSender)
{
	CCLog("startGame");
}

void HelloWorld::selectGame(CCObject* pSender)
{
	CCLog("selectGame");
}

void HelloWorld::helpGame(CCObject* pSender)
{
	CCLog("helpGame");
}

void HelloWorld::settingGame(CCObject* pSender)
{
	CCLog("settingGame");
}

void HelloWorld::updateBubble(float dt)
{
	CCSprite* bubble = CCSprite::create("water.png");	

	bubble->setScale(0.25);

	int random_x =CCRANDOM_0_1()*200;
	int random_y =CCRANDOM_0_1()*10;

	bubble->setPosition(ccp(maxW/8+random_x,0));

	CCMoveTo* moveTo = 	CCMoveTo::create(2.5, ccp(maxW/8+random_x+random_y,maxH+10));
	bubble->runAction(moveTo);

	this->addChild(bubble,2);

	CCLog("updateBubble");   
}

void HelloWorld::registerWithTouchDispatcher()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority + 1, true);
}

bool HelloWorld::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
    CCPoint touchLocation = touch->getLocation();
   // updateSize(touchLocation);
	CCLog("ccTouchBegan:touchLocation: touchLocation.x=%.2f ,touchLocation.y:=%.2f",touchLocation.x,touchLocation.y);
    return true;
}

void HelloWorld::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
    CCPoint touchLocation = touch->getLocation();
    //updateSize(touchLocation);
	CCLog("ccTouchMoved:touchLocation: touchLocation.x=%.2f ,touchLocation.y:=%.2f",touchLocation.x,touchLocation.y);
}

void HelloWorld::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
    CCPoint touchLocation = touch->getLocation();
    //updateSize(touchLocation);
	CCLog("ccTouchEnded:touchLocation: touchLocation.x=%.2f ,touchLocation.y=%.2f",touchLocation.x,touchLocation.y);
}

(5)源码下载地址:

http://download.csdn.net/detail/hfreeman2008/5483837


参考资料:

1.【Cocos2d-x游戏引擎开发笔记(3)】在屏幕上渲染菜单并添加消息响应