第三阶段:常用功能2
1.Cocos2d-x场景切换
Size visibleSize = Director::getInstance()->getVisibleSize();
Sprite *bg = Sprite::create( "bg.jpg" );
bg->setPosition(visibleSize.width/ 2 , visibleSize.height/ 2 );
addChild(bg);
LabelTTF *label = LabelTTF::create( "Show Next Scene" , "Courier" , 36 );
addChild(label);
label->setPosition(visibleSize.width/ 2 , visibleSize.height/ 2 );
EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [label, this ](Touch* t,Event *e){
if (label->getBoundingBox().containsPoint(t->getLocation())) {
Director::getInstance()->replaceScene(TransitionFadeBL::create( 1 , ImageScene::createScene()));
}
return false ;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, label);
Sprite *bg = Sprite::create( "bg.jpg" );
bg->setPosition(visibleSize.width/ 2 , visibleSize.height/ 2 );
addChild(bg);
LabelTTF *label = LabelTTF::create( "Show Next Scene" , "Courier" , 36 );
addChild(label);
label->setPosition(visibleSize.width/ 2 , visibleSize.height/ 2 );
EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [label, this ](Touch* t,Event *e){
if (label->getBoundingBox().containsPoint(t->getLocation())) {
Director::getInstance()->replaceScene(TransitionFadeBL::create( 1 , ImageScene::createScene()));
}
return false ;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, label);
return true;
在ImageScene.h中
#include
<iostream>
#include <cocos2d.h>
#include <cocos2d.h>
USING_NS_CC;
class
ImageScene:
public
Layer {
public
:
static Scene* createScene();
virtual
bool
init();
CREATE_FUNC(ImageScene);
};
CREATE_FUNC(ImageScene);
};
在ImageScene.cpp中
#include
"ImageScene.h"
Scene* ImageScene::createScene(){
Scene* scene = Scene::create();
ImageScene * layer = ImageScene::create();
ImageScene * layer = ImageScene::create();
scene->addChild(layer);
return
scene;
}
bool
ImageScene
::init(){
Size size = Director::getInstance()->getVisibleSize();
Sprite *s = Sprite::create( "HelloWorld.png" );
s->setPosition(size.width/ 2 , size.height/ 2 );
Sprite *s = Sprite::create( "HelloWorld.png" );
s->setPosition(size.width/ 2 , size.height/ 2 );
addChild(s);
return
true
;
}
2.Cocos2d-x场景切换效果
Director::getInstance()->replaceScene(TransitionFadeBL::create(
1
, ImageScene::createScene()));
- TransitionCrossFade.create(t,scene) // 交叉消失两个场景使用cc.RenderTexture对象。
- TransitionFad.create(t,scene,color) // 淡出即将离任的场景,然后消失在传入的场景。
- TransitionFadeBL.create(t, scene) // 向左下波浪退出
- TransitionFadeDown.create(t, scene) // 向下百叶窗式换场景
- TransitionFadeUp.create(t, scene) // 向上百叶窗式换场景
-
- TransitionJumpZoom.create(t, scene) // 跳跃式替换,场景缩小,再加载进来
- TransitionMoveInB.create(t,scene) // 创建一个在底部,覆盖当前场景
- TransitionMoveInR.create(t,scene) // 创建一个在右边,覆盖当前场景
- TransitionMoveInT.create(t,scene) // 创建一个在左边,覆盖当前场景
- TransitionPageTurn.create(t,scene, backwards) // 前翻页式场景替换
- TransitionRadialCW.create(t,scene) // 逆时针切入
- TransitionRotoZoom.create(t,scene) // 转换角度替换
- TransitionFlipAngular.create(t,scene, o) // 按一定角度左翻
- TransitionFlipX.create(t, scene,o) // X轴左边翻换
- TransitionFlipY.create(t,scene, o) // Y轴左边翻换
- TransitionZoomFlipAngular.create(t,scene, o) // 带有缩放效果,有角度的转左翻
- TransitionZoomFlipX.create(t,scene, o) // 带有缩放效果,在X轴左翻
- TransitionZoomFlipY.create(t,scene, o) // 带有缩放效果,左Y轴左翻
- TransitionShrinkGrow.create(t,scene) //交叉着替换场景
- TransitionSlideInB.create(t,scene) //场景有底部进入
3.Cocos2d-x动作
instant
/'ɪnst(ə)nt/
片刻, 顷刻, 刹那/
立即的
Size visibleSize = Director::getInstance()->getVisibleSize();
auto label = LabelTTF::create( "jikexueyuan" , "Courier" , 30 );
label->setPosition(visibleSize.width/ 2 , visibleSize.height/ 2 );
addChild(label);
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [label](Touch *t,Event *e){
if (label->getBoundingBox().containsPoint(t->getLocation())) {
// label->runAction(MoveTo::create(1, Point(100, 100)));
// label->runAction(MoveBy::create(1, Point(-50, -50))->reverse());
// label->runAction(Spawn::create(MoveBy::create(1, Point(100, 100)), RotateBy::create(1, 360),NULL));
// label->runAction(Sequence::create(MoveBy::create(1, Point(100, 100)),RotateBy::create(1, 360), NULL));
label->runAction(Sequence::create(
MoveBy::create( 1 , Point( 100 , 100 )),
RotateBy::create( 1 , 360 ),
CallFunc::create([](){
MessageBox( "Action complete" , "complete" );
}), NULL ));
}
return false ;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, label);
auto label = LabelTTF::create( "jikexueyuan" , "Courier" , 30 );
label->setPosition(visibleSize.width/ 2 , visibleSize.height/ 2 );
addChild(label);
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [label](Touch *t,Event *e){
if (label->getBoundingBox().containsPoint(t->getLocation())) {
// label->runAction(MoveTo::create(1, Point(100, 100)));
// label->runAction(MoveBy::create(1, Point(-50, -50))->reverse());
// label->runAction(Spawn::create(MoveBy::create(1, Point(100, 100)), RotateBy::create(1, 360),NULL));
// label->runAction(Sequence::create(MoveBy::create(1, Point(100, 100)),RotateBy::create(1, 360), NULL));
label->runAction(Sequence::create(
MoveBy::create( 1 , Point( 100 , 100 )),
RotateBy::create( 1 , 360 ),
CallFunc::create([](){
MessageBox( "Action complete" , "complete" );
}), NULL ));
}
return false ;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, label);
// label->runAction(Repeat::create(RotateBy::create(1, 180), 3));
// label->runAction(RepeatForever::create(RotateBy::create(1, 180)))
return true ;
4.Cocos2d-x动作反转
label->runAction(MoveBy::create(1, Point(-50, -50))->reverse());
5.Cocos2d-x动作重复
// label->runAction(Repeat::create(RotateBy::create(1, 180), 3));重复动作三次
// label->runAction(RepeatForever::create(RotateBy::create(1, 180)));永远重复下去
6.Cocos2d-x动作混合
同时执行:
// label->runAction(Spawn::create(MoveBy::create(1, Point(100, 100)), RotateBy::create(1, 360),NULL));
7.Cocos2d-x动作序列
顺序执行:
// label->runAction(Sequence::create(MoveBy::create(1, Point(100, 100)),RotateBy::create(1, 360), NULL));
8.Cocos2d-x动作侦听
RotateBy之后执行
CallFunc:
label->runAction(Sequence::create(
MoveBy::create( 1 , Point( 100 , 100 )),
RotateBy::create( 1 , 360 ),
CallFunc::create([](){
MessageBox( "Action complete" , "complete" );
MoveBy::create( 1 , Point( 100 , 100 )),
RotateBy::create( 1 , 360 ),
CallFunc::create([](){
MessageBox( "Action complete" , "complete" );
}),NULL));
9.Cocos2d-x动画
auto
cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile( "anim.plist" );
Vector<SpriteFrame*> vec;
char name[ 15 ];
memset(name, 0 , 15 );
for ( int i= 0 ; i< 20 ; i++) {
sprintf(name, "anim%04d" ,i);
vec.pushBack(cache->getSpriteFrameByName(name));
cache->addSpriteFramesWithFile( "anim.plist" );
Vector<SpriteFrame*> vec;
char name[ 15 ];
memset(name, 0 , 15 );
for ( int i= 0 ; i< 20 ; i++) {
sprintf(name, "anim%04d" ,i);
vec.pushBack(cache->getSpriteFrameByName(name));
}
//每秒十帧
Animation *animation = Animation::createWithSpriteFrames(vec,
0.1f
);
Animate *animate = Animate::create(animation);
auto sprite = Sprite::create();
addChild(sprite);
sprite->setPosition( 200 , 200 );
Animate *animate = Animate::create(animation);
auto sprite = Sprite::create();
addChild(sprite);
sprite->setPosition( 200 , 200 );
sprite->runAction(RepeatForever::create(animate));