#include "SimpleAudioEngine.h"//引入头文件bool HelloWorld::init(){ if(!Layer::init()){ return false; } CocosDenShion::SimpleAudioEngine::getInstance() ->playBackgroundMusic("music.mp3",true);//播放音乐,重复播放 return true;}
<span >playBackgroundMusic适合播放持续时间较长一点的音乐,播放打斗声的时候使用</span>
playEffect("pa.wav");
2.可拉伸的图片
Size visibleSize = Director::getInstance()->getVisibleSize();Scale9Sprite* scale9Sprite = Scale9Sprite::create("HelloWorld.png");scale9Sprite->setContentSize(visibleSize);scale9Sprite->setposition(Point(visibleSize.wIDth/2,visibleSize.height/2));this->addChild(scale9Sprite,0);
导入扩展库,
在解决方案,添加现有项目:D:\worktools\cocostests\HongGG\cocos2d\extensions\proj.win32\libExtensions.vcxproj
在项目右键,属性->通用属性->添加新引用,将libExtensions添加
然后配置属性->c/c++->常规->附加包含目录,编辑,加上一句$(EngineRoot)
最后就是运行了(背景图片是用点九弄的,总感觉怪怪的),
记得加上头文件哦
#include "cocos-ext.h"using namespace cocos2d::extension;
木头的版本是V1.4.0.1的,所以我使用的也是一样的。
使用UI Editor
新建工程
①将文本框拖动到画布中,输入HongGG,
②将按钮拖动到画布中,将resource的图片拖动到特性,资源里
③将图片拖动到画布中,将resource的图片拖动到特性,文件里
④导出文件,选中导出全部画布,导出使用大图,裁剪
⑤接下来就是在代码中展示出来了
将导出的复制到项目的resources目录下
导库
D:\worktools\cocostests\HongGG\cocos2d\cocos\editor-support\cocostudio\proj.win32\libCocosstudio.vcxproj
D:\worktools\cocostests\HongGG\cocos2d\cocos\ui\proj.win32\libGUI.vcxproj
引入头文件
#include "editor-support//cocostudio/CCSGUIReader.h"#include "ui/CocosGUI.h"using namespace cocos2d::ui;using namespace cocostudio;
在代码中加入
auto xUI = cocostudio::GUIReader::getInstance()->WidgetFromJsonfile("HongGGUI_1.ExportJson");//记载进uixUI->setposition(Point(visibleSize.wIDth/2,visibleSize.height/2));this->addChild(xUI);button* button1 = (button*)Helper::seekWidgetByname(xUI,"button1");//找到名字是button1的按钮image1 = (ImageVIEw*)Helper::seekWidgetByname(xUI,"Image1");//找到名字是Image1的图片button1->addtouchEventListener(this,toucheventselector(HelloWorld::onClick));//给按钮设置监听
按钮的监听(点击按钮设置图片可见或不可见,效果图在上面的上面)
voID HelloWorld::onClick(Ref* ref,touchEventType type){ switch (type) { case Widget::touchEventType::BEGAN: break; case Widget::touchEventType::MOVED: break; case Widget::touchEventType::ENDED: if (image1->isVisible()) { image1->setVisible(false); }else { image1->setVisible(true); } break; default: break; }}
@H_403_202@4.制作血量条
添加一个图片,在常规里有个颜色混合,设置为red
创建个进度条控件,将进度条控件拖到图片的上方,作为图片的子控件,当然,也可以不这样的
将进度设置成50,这里说一句,这里设置的50未必能成功,反正我的是失败了
/* 加载UI */ auto UI = cocostudio::GUIReader::getInstance()->WidgetFromJsonfile("MoreUI_1.ExportJson"); UI->setposition(Point(100,100)); this->addChild(UI); /* 获取控件对象 */ m_hpbar = (Loadingbar*)Helper::seekWidgetByname(UI,"hpbar"); /* 添加点击监听 */ m_hpbar->addtouchEventListener(this,toucheventselector(HelloWorld::onClick));
移动监听
voID HelloWorld::onClick(Ref*,touchEventType type){ switch (type) { case touchEventType::touch_EVENT_MOVED: m_hpbar->setPercent(m_hpbar->getPercent() - 1); log("%f",m_hpbar->getPercent()); break; } }总结
以上是内存溢出为你收集整理的【Cocos2d-x3.0学习笔记 04】进一步的学习全部内容,希望文章能够帮你解决【Cocos2d-x3.0学习笔记 04】进一步的学习所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)