Cocos2d-x 常用语句
2015-11-24 15:16
302 查看
版本:cocos2d-x 3.6
音乐播放
#include
"SimpleAudioEngine.h"
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("秋日思语.mp3",true);
CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(“打斗声.mp3",true);
九妹图片显示---可自动调整图片保持不失真
#include
"cocos-ext.h"
using
namespace
cocos2d::extension;
Scale9Sprite
*nineGirl =
Scale9Sprite ::create("firstbg1.jpg");
nineGirl->setContentSize(Size(size.width,
size.height));
nineGirl->setPosition(Point(size.width/2,size.height/2));
Scale9Sprite
*nineGirl =
Scale9Sprite ::create("firstbg1.jpg");
nineGirl->setContentSize(Size(size.width,
size.height));
nineGirl->setPosition(Point(size.width/2,size.height/2));
this->addChild(nineGirl);
qqaimation =
Sprite::create("sprite.png");
qqaimation->setPosition(Point(10,10));
this->addChild(qqaimation);
常用动作
// MoveTo *moveTo = MoveTo::create(3, Point(size.width/2,size.height/2));
// qqaimation->runAction(moveTo);
//再微调移动
x y移动
// MoveBy *moveby = MoveBy::create(1, Point(0,100));
// qqaimation->runAction(moveby);
//缩放
时间
x y倍数
// ScaleTo *scaleto = ScaleTo::create(2, 0.4, 1.0);
//闪烁
Blink *blink =
Blink::create(3.0f,
3);
qqaimation->runAction(blink);
//曲线移动
ccBezierConfig bezier;
bezier.controlPoint_1
=
Point(240,10);
bezier.controlPoint_2
=
Point(480,300);
bezier.endPosition
=
Point(480,400);
BezierTo * bezierto =
BezierTo::create(5,
bezier);
// qqaimation->runAction(bezierto);
//跳
JumpBy *jumpby =
JumpBy::create(3.0f,
Point(50,1),
100,
1);
//RepeatForever *repeatf = RepeatForever::create(jumpby);//永久重复
Repeat *repeatA =
Repeat::create(jumpby,
3);
//qqaimation->runAction(repeatA);
//旋转
RotateBy *rotateby =
RotateBy::create(3,
220,
10);
Action *actions =
Spawn::create(bezierto,repeatA,rotateby,
NULL);//三个动作一起播放
qqaimation->runAction(actions);
//监听动作结束
//监听最后一个动作结束
auto callback = [&](){
animationfinish(qqaimation);//此处必须要是全局变量
};
CallFunc *callFunc =
CallFunc::create(callback);
Action *actions2 =
Sequence::create(repeatA,callFunc,
NULL);//监听某个动作结束
qqaimation->runAction(actions2);
//监听屏幕动作
auto listener =
EventListenerTouchOneByOne::create();
listener->onTouchBegan
= [](Touch
*touch,Event
*event){
Point
pos = touch->getLocationInView();//2D坐标—左上角为原点
Point pos2 = Director::getInstance()->convertToGL(pos);//获取单机坐标基于cocos2dx---笛卡尔坐标
log("touchbegan x=%f y=%f",pos2.x,pos2.y);
return
true;
};
listener->onTouchMoved
= [](Touch
*touch ,
Event *event){
log("touchMove");
};
listener->setSwallowTouches(true);//覆盖其它对象获取手势
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,
this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),
sprite);//赋值这个监听到这个精灵身上
//连续一套动作
m_sprite->runAction(createAnimate1());//精灵执行这套动作
//执行函数
Animate*
SecondScene::createAnimate1(){
//加载图片到缓存池—使用工具TexturePacher打包图片
SpriteFrameCache * frameCache =
SpriteFrameCache::getInstance();
frameCache->addSpriteFramesWithFile("boys.plist",
"boys.png");
int iFrameNum =15;
SpriteFrame *frame =
NULL;
Vector<SpriteFrame
*>frameVec;
for (int
i=
1; i <=iFrameNum; i++) {
//frame = SpriteFrame::create(StringUtils::format("run%d.png",i), Rect(0, 0, 130, 130));
frame= frameCache->getSpriteFrameByName(StringUtils::format("run%d.png",i));
frameVec.pushBack(frame);
}
Animation* animation =
Animation::createWithSpriteFrames(frameVec);
animation->setLoops(-1);//-1
表示不断重复
其它数字表示重复次数
animation->setDelayPerUnit(0.1f);//每张图片间隔显示时间
Animate *action =
Animate::create(animation);
log("llllll");
return action;
}
//Cocostudio使用常用语句
//导入cocostudio创建的ui界面
#include
"cocostudio/CocoStudio.h"
//#include
"ui/CocosGUI.h"
//获取控件
#include
"cocostudio/ActionTimeline/CSLoader.h"
#include
"CocosGUI.h"
//加载
auto
rootNode =
CSLoader::createNode("HelloUI.csb");
addChild(rootNode);
//获取控件
auto button =
dynamic_cast<Button*>(rootNode->getChildByName("xiaoruoBtn"));
//获取控件响应
struct
callBackFunctor{
void
operator() (Ref*sender)const{
Button
*btn = (Button
*)sender;
log("ddd");
}
};
Widget::ccWidgetClickCallback
callback =
callBackFunctor();
button->addClickEventListener(callback);
//第二种方式同ios
button->addTouchEventListener(this,
toucheventselector(_SELECTOR));
void InGame::ButtentouchEvent(Object *pSender, TouchEventType type)
{
};
/第三种方式
finishibtn->addTouchEventListener(CC_CALLBACK_2(TowerEditorScene::btnclick,
this));
void
TowerEditorScene::btnclick(cocos2d::Ref
*object,
Widget::TouchEventType
type){
}
音乐播放
#include
"SimpleAudioEngine.h"
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("秋日思语.mp3",true);
CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(“打斗声.mp3",true);
九妹图片显示---可自动调整图片保持不失真
#include
"cocos-ext.h"
using
namespace
cocos2d::extension;
Scale9Sprite
*nineGirl =
Scale9Sprite ::create("firstbg1.jpg");
nineGirl->setContentSize(Size(size.width,
size.height));
nineGirl->setPosition(Point(size.width/2,size.height/2));
Scale9Sprite
*nineGirl =
Scale9Sprite ::create("firstbg1.jpg");
nineGirl->setContentSize(Size(size.width,
size.height));
nineGirl->setPosition(Point(size.width/2,size.height/2));
this->addChild(nineGirl);
qqaimation =
Sprite::create("sprite.png");
qqaimation->setPosition(Point(10,10));
this->addChild(qqaimation);
常用动作
// MoveTo *moveTo = MoveTo::create(3, Point(size.width/2,size.height/2));
// qqaimation->runAction(moveTo);
//再微调移动
x y移动
// MoveBy *moveby = MoveBy::create(1, Point(0,100));
// qqaimation->runAction(moveby);
//缩放
时间
x y倍数
// ScaleTo *scaleto = ScaleTo::create(2, 0.4, 1.0);
//闪烁
Blink *blink =
Blink::create(3.0f,
3);
qqaimation->runAction(blink);
//曲线移动
ccBezierConfig bezier;
bezier.controlPoint_1
=
Point(240,10);
bezier.controlPoint_2
=
Point(480,300);
bezier.endPosition
=
Point(480,400);
BezierTo * bezierto =
BezierTo::create(5,
bezier);
// qqaimation->runAction(bezierto);
//跳
JumpBy *jumpby =
JumpBy::create(3.0f,
Point(50,1),
100,
1);
//RepeatForever *repeatf = RepeatForever::create(jumpby);//永久重复
Repeat *repeatA =
Repeat::create(jumpby,
3);
//qqaimation->runAction(repeatA);
//旋转
RotateBy *rotateby =
RotateBy::create(3,
220,
10);
Action *actions =
Spawn::create(bezierto,repeatA,rotateby,
NULL);//三个动作一起播放
qqaimation->runAction(actions);
//监听动作结束
//监听最后一个动作结束
auto callback = [&](){
animationfinish(qqaimation);//此处必须要是全局变量
};
CallFunc *callFunc =
CallFunc::create(callback);
Action *actions2 =
Sequence::create(repeatA,callFunc,
NULL);//监听某个动作结束
qqaimation->runAction(actions2);
//监听屏幕动作
auto listener =
EventListenerTouchOneByOne::create();
listener->onTouchBegan
= [](Touch
*touch,Event
*event){
Point
pos = touch->getLocationInView();//2D坐标—左上角为原点
Point pos2 = Director::getInstance()->convertToGL(pos);//获取单机坐标基于cocos2dx---笛卡尔坐标
log("touchbegan x=%f y=%f",pos2.x,pos2.y);
return
true;
};
listener->onTouchMoved
= [](Touch
*touch ,
Event *event){
log("touchMove");
};
listener->setSwallowTouches(true);//覆盖其它对象获取手势
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,
this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),
sprite);//赋值这个监听到这个精灵身上
//连续一套动作
m_sprite->runAction(createAnimate1());//精灵执行这套动作
//执行函数
Animate*
SecondScene::createAnimate1(){
//加载图片到缓存池—使用工具TexturePacher打包图片
SpriteFrameCache * frameCache =
SpriteFrameCache::getInstance();
frameCache->addSpriteFramesWithFile("boys.plist",
"boys.png");
int iFrameNum =15;
SpriteFrame *frame =
NULL;
Vector<SpriteFrame
*>frameVec;
for (int
i=
1; i <=iFrameNum; i++) {
//frame = SpriteFrame::create(StringUtils::format("run%d.png",i), Rect(0, 0, 130, 130));
frame= frameCache->getSpriteFrameByName(StringUtils::format("run%d.png",i));
frameVec.pushBack(frame);
}
Animation* animation =
Animation::createWithSpriteFrames(frameVec);
animation->setLoops(-1);//-1
表示不断重复
其它数字表示重复次数
animation->setDelayPerUnit(0.1f);//每张图片间隔显示时间
Animate *action =
Animate::create(animation);
log("llllll");
return action;
}
//Cocostudio使用常用语句
//导入cocostudio创建的ui界面
#include
"cocostudio/CocoStudio.h"
//#include
"ui/CocosGUI.h"
//获取控件
#include
"cocostudio/ActionTimeline/CSLoader.h"
#include
"CocosGUI.h"
//加载
auto
rootNode =
CSLoader::createNode("HelloUI.csb");
addChild(rootNode);
//获取控件
auto button =
dynamic_cast<Button*>(rootNode->getChildByName("xiaoruoBtn"));
//获取控件响应
struct
callBackFunctor{
void
operator() (Ref*sender)const{
Button
*btn = (Button
*)sender;
log("ddd");
}
};
Widget::ccWidgetClickCallback
callback =
callBackFunctor();
button->addClickEventListener(callback);
//第二种方式同ios
button->addTouchEventListener(this,
toucheventselector(_SELECTOR));
void InGame::ButtentouchEvent(Object *pSender, TouchEventType type)
{
};
/第三种方式
finishibtn->addTouchEventListener(CC_CALLBACK_2(TowerEditorScene::btnclick,
this));
void
TowerEditorScene::btnclick(cocos2d::Ref
*object,
Widget::TouchEventType
type){
}
相关文章推荐
- 使用COCOS2D-JS开发你的第一个游戏
- cocos2dx 动作Sequence和Spawn包含RepeatForever的方法解决
- cocos2d-x开发学习笔记(一)
- cocos2d-x lua调用c++函数
- xcode 编译cocos2d项目 出现 No Header or Directory *.pch
- cocos2d-x 屏幕适配
- Android版Cocos2d-X应用的AnySDK集成Admob管理库教程
- AndroidStudio搭建cocos2d
- cocos2d-x 调度器(scheduler)
- cocos2d开发响应android手机home和back的处理优化
- Windows Cocos-2dx-3.8.1 lua + Visual Studio环境配置
- cocos2dx v3.x box2d DrawDebugData 后置问题
- 让Cocos Studio 2.3.2制作UI界面中控件支持运行3d动作特效的间接途径
- 命令行编译cocos2d-x android
- cocos2d-x大型地图实现(续)
- 单例模式
- cocos2d-x大型地图的实现
- cocos2dx android 音效崩溃
- [自动化-脚本]002.cocos2dx-lua lua代码windows加密批处理
- 麦子学院cocos2d听课截图