第三阶段:常用功能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 );
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 );
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中
"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 );
Sprite *s = Sprite::create( "HelloWorld.png" );
s->setPosition(size.width/ 2 );
addChild(s);
true
;
}
2.Cocos2d-x场景切换效果
Director::getInstance()->replaceScene(TransitionFadeBL::create(
1
,ImageScene::createScene()));
- TransitionCrossFade.create(t,scene)//交叉消失两个场景使用cc.RenderTexture对象。
- TransitionFad.create(t,scene,color)@H_626_301@//淡出即将离任的场景,然后消失在传入的场景。
- TransitionFadeBL.create(t,scene)@H_626_301@//向左下波浪退出
- TransitionFadeDown.create(t,0); background-color:inherit">//向下百叶窗式换场景
- TransitionFadeUp.create(t,0); background-color:inherit">//向上百叶窗式换场景
-
- TransitionJumpZoom.create(t,0); background-color:inherit">//跳跃式替换,场景缩小,再加载进来
- TransitionMoveInB.create(t,0); background-color:inherit">//创建一个在底部,覆盖当前场景
- TransitionMoveInR.create(t,0); background-color:inherit">//创建一个在右边,覆盖当前场景
- TransitionMoveInT.create(t,0); background-color:inherit">//创建一个在左边,覆盖当前场景
- TransitionPageTurn.create(t,backwards)@H_626_301@//前翻页式场景替换
- TransitionRadialCW.create(t,0); background-color:inherit">//逆时针切入
- TransitionRotoZoom.create(t,0); background-color:inherit">//转换角度替换
- TransitionFlipAngular.create(t,o)@H_626_301@//按一定角度左翻
- TransitionFlipX.create(t,0); background-color:inherit">//X轴左边翻换
- TransitionFlipY.create(t,0); background-color:inherit">//Y轴左边翻换
- TransitionZoomFlipAngular.create(t,0); background-color:inherit">//带有缩放效果,有角度的转左翻
- TransitionZoomFlipX.create(t,0); background-color:inherit">//带有缩放效果,在X轴左翻
- TransitionZoomFlipY.create(t,0); background-color:inherit">//带有缩放效果,左Y轴左翻
- TransitionShrinkGrow.create(t,0); background-color:inherit">//交叉着替换场景
- TransitionSlideInB.create(t,0); background-color:inherit">//场景有底部进入
3.Cocos2d-x动作
instant
/'ɪnst(ə)nt/
片刻,顷刻,刹那/
立即的
Size visibleSize = Director::getInstance()->getVisibleSize();
auto label = LabelTTF::create( "jikexueyuan" ,216)">30 );
label->setPosition(visibleSize.width/ 2 );
addChild(label);
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [label](Touch *t,162)">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,100)),RotateBy::create(1,360),NULL));
// label->runAction(Sequence::create(MoveBy::create(1,NULL));
label->runAction(Sequence::create(
MoveBy::create( 100 ,216)">100 )),
RotateBy::create( 360 ),
CallFunc::create([](){
MessageBox( "Action complete" ,27)">"complete" );
}),162)">NULL ));
}
false ;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,label);
auto label = LabelTTF::create( "jikexueyuan" ,216)">30 );
label->setPosition(visibleSize.width/ 2 );
addChild(label);
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [label](Touch *t,162)">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,100)),RotateBy::create(1,360),NULL));
// label->runAction(Sequence::create(MoveBy::create(1,NULL));
label->runAction(Sequence::create(
MoveBy::create( 100 ,216)">100 )),
RotateBy::create( 360 ),
CallFunc::create([](){
MessageBox( "Action complete" ,27)">"complete" );
}),162)">NULL ));
}
false ;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,label);
// label->runAction(Repeat::create(RotateBy::create(1,180),3));
// label->runAction(RepeatForever::create(RotateBy::create(1,180)))
true ;
4.Cocos2d-x动作反转
label->runAction(MoveBy::create(1,-50))->reverse());
5.Cocos2d-x动作重复
6.Cocos2d-x动作混合
同时执行:
8.Cocos2d-x动作侦听
label->runAction(Sequence::create(
MoveBy::create( "complete" );
MoveBy::create( "complete" );
}),NULL));
9.Cocos2d-x动画
auto
cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile( "anim.plist" );
Vector<SpriteFrame*> vec;
char name[ 15 ];
memset(name,216)">0 ,216)">15 );
for ( int i= 0 ; i< 20 ; i++) {
sprintf(name,27)">"anim%04d" ,i);
vec.pushBack(cache->getSpriteFrameByName(name));
cache->addSpriteFramesWithFile( "anim.plist" );
Vector<SpriteFrame*> vec;
char name[ 15 ];
memset(name,216)">0 ,216)">15 );
for ( int i= 0 ; i< 20 ; i++) {
sprintf(name,27)">"anim%04d" ,i);
vec.pushBack(cache->getSpriteFrameByName(name));
}
//每秒十帧
Animation *animation = Animation::createWithSpriteFrames(vec,216)">0.1f
);
Animate *animate = Animate::create(animation);
auto sprite = Sprite::create();
addChild(sprite);
sprite->setPosition( 200 ,216)">200 );
Animate *animate = Animate::create(animation);
auto sprite = Sprite::create();
addChild(sprite);
sprite->setPosition( 200 ,216)">200 );
sprite->runAction(RepeatForever::create(animate));
原文链接:https://www.f2er.com/cocos2dx/345732.html