cocos2d-x游戏开发系列教程-坦克大战游戏启动界面的编写

用前面介绍的方法,创建一个cocos2d-x项目,可以看到新项目内容如下图:

我看查看 HelloWorldScene.cpp中的函数

bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->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
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));
    
	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);

    /
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
    
    // position the label on the center of the screen
    pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - pLabel->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(pLabel, 1);

    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);
    
    return true;
}
开始场景中主要动作都在这里。

第一步初始化父类


第二步添加一个“X”图片,点击之后退出程序
可一看到,创建菜单之后用addChild函数讲他添加到了场景了,但是比

CCScene* HelloWorld::scene()中的addChild多出一个参数zOrder,这个

参数是指的child的z轴顺序,也就是显示的先后顺序。


第三步创建了一个文本标签并添加到层中,显示内容是“HelloWorld”


第四部用“HelloWorld.png”创建了一个精灵并添加到第0层中。最后返回true,

表示初始化成功.


下面我们对它进行修改,来完成我们的坦克大战游戏。

1.在layer中调用:
setKeypadEnabled(true);
layer中重写以下两个方法:
virtual void keyBackClicked();
virtual void keyMenuClicked();


他们分别用来相应Android中的返回键和菜单键

void HelloWorld::keyBackClicked()//Android 返回键
{
CCLayer::keyBackClicked();
CCDirector::sharedDirector()->end();
}
void HelloWorld::keyMenuClicked()//Android 菜单键
{
CCLayer::keyMenuClicked();
}


2.创建一个“关闭”的菜单项

CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));

其中最后一个参数是回掉函数,点击这个菜单时会调用这个函数,具体实现为:

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
//关闭菜单被点击时调用
CCDirector::sharedDirector()->end();
}


3.获取可视区域的大小和原点

    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

后面将会用到。


4.设置关闭菜单位置到右下角,并用前面创建的关闭的菜单项创建一个菜单,

然后添加到层中

pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width / 2, 
origin.y + visibleSize.height - pCloseItem->getContentSize().height/2));
CCMenu *pMenu = CCMenu::create(pCloseItem, NULL);
this->addChild(pMenu, 1);

5.创建一个“play game”按钮来开始游戏,设置位置,然后设置缩放比例来适应屏幕,

然后添加菜单项到菜单中,加入层中
CCMenuItemImage *pItemPlay = CCMenuItemImage::create(
"playgameNormal.png", "playgameClose.png", this, menu_selector(HelloWorld::menuPlayGameCallback));
pItemPlay->setPosition(ccp(visibleSize.width / 2, visibleSize.height*1.0f / 4.0f));
pItemPlay->setScaleX(visibleSize.width / 600);
pItemPlay->setScaleY(visibleSize.height / 400);

CCMenu *pMenuPlay = CCMenu::create(pItemPlay, NULL);
pMenuPlay->setPosition(CCPointZero);
this->addChild(pMenuPlay, 1);

菜单的相应函数menuPlayGameCallback暂时什么也不做

void HelloWorld::menuPlayGameCallback(CCObject* pSender)
{
//开始菜单被点击时调用
return;
}


6.创建一个启动的背景界面
CCSprite *pSprite = CCSprite::create("ScenceStart.png");
pSprite->setPosition(ccp(visibleSize.width / 2, visibleSize.height / 2));
CCSize sz = pSprite->getContentSize();
pSprite->setScaleX(visibleSize.width / sz.width);
pSprite->setScaleY(visibleSize.height / sz.height);
this->addChild(pSprite, 0);


最后把资源文件拷到相应位置,编译项目,成功后运行程序,如下图:



完整的代码下载地址:

http://download.csdn.net/detail/yincheng01/6716573


转载于:https://www.cnblogs.com/new0801/p/6177364.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值