cocos2d-x-3.0 alpha1与C++11练习九:飞镖忍者,添加发射飞镖数

来源:未知 责任编辑:责任编辑 发表时间:2014-01-25 11:31 点击:
1,创建UI p>使用cocostudio,创建UI,分别添加一个LabelAtlas和一个LabelBMFont。导出项目,存为

p> 

p>cocos2d3sampleui_playScene.ExportJson

p> 

p>并与相关资源文件一同添加进xcode的Resource目录下。这里有一点可能需要注意,但是在添加资源时,如果代码中未使用目录,则在添加时,则选择“Create folder reference for any added folders”,这样将在项目中创建蓝色的组,并非真正的子目录。

p> 

p> 

p> 

p>2,编码实现

p>在HelloWorldScene.h中添加相关变量声明,

p> 

p> 

p>//发射的数目

p>int numShootProjectile;

p> 

p>UILabelAtlas *shootNumLabelAtlas;

p>UILabelBMFont *scoreLabelbMFont;

p>  

p> 

p>变量名称与在UIEditor中设置的控件名称保持一致,以便于编码。

p> 

p>在HelloWorldScene.cpp中init中,return之前,添加如下代码:

p> 

p>UILayer *layer = UILayer::create();

p>auto widget = dynamic_cast<UIWidget *>(cocostudio::GUIReader::shareReader()->widgetFromJsonFile("cocos2d3sampleui_playScene.ExportJson"));

p>widget->setSize(Size(480, 320));

p>layer->addWidget(widget);

p>this->addChild(layer);

p> 

p>this->shootNumLabelAtlas = dynamic_cast<UILabelAtlas *>(widget->getChildByName("shootNumLabelAtlas"));

p>this->shootNumLabelAtlas->setStringValue("0");

p>this->shootNumLabelAtlas->setPosition(Point(50, winSize.height-50));

p>this->scoreLabelbMFont = dynamic_cast<UILabelBMFont *>(widget->getChildByName("scoreLabelbMFont"));

p>this->scoreLabelbMFont->setPosition(Point(winSize.width-100, winSize.height-50));

p>this->scoreLabelbMFont->setText("Score:0");

p>numShootProjectile=0;

p>  

p> 

p>两个控件被加载以后,在垂直方向上自动放在了居中的位置,不知为何?分别取名称取其控件,保存引用至变量中。需要需要指出,在c++中声明的变量,一定要初始化,例如:

p> 

p> 

p>numShootProjectile=0

p>  

p> 

p>如果不作初始化,变量可能是任意一个数值。

p> 

p>在onTouchesEnded中,当发射飞镖之后,数目加1,并且设置文本框数字。代码如下:

p> 

p> 

p>numShootProjectile++;

p>char numShootStr[20];

p>sprintf(numShootStr,"%d",numShootProjectile);

p>this->shootNumLabelAtlas->setStringValue(numShootStr);

p>  

p> 

p>在c++中,将string转换为int,可以使用std::atoi,但是反过来,itoa却并不是所有编译器均支持并提供的。

p> 

p>This function(itoa) is not defined in ANSI-C and is not part of C++, but is supported by some compilers.

p> 

p>在update中,添加更新得分的代码:

p> 

p>     char scoreStr[20];

p>        sprintf(scoreStr,"Score:%d",targetDestroyed);

p>        this->scoreLabelbMFont->setText(scoreStr);

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • cocos2d-x学习笔记(19)--label 、label atlas
    • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
    • Cocos2d-x学习(一):HelloWorld
    • cocos2dx在xcode下开发,编译到android上(2)
    • cocos2d 设置屏幕默认方向
    • cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoi
    • Cocos2d-x 2.0 之 Actions “三板斧” 之一
    • cocos2d-x学习笔记(18)--游戏打包(windows平台)
    • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1