您的位置:首页 > 移动开发 > Cocos引擎

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){

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: