专栏首页五毛程序员五毛的cocos2d-x学习笔记05-场景与场景动画,动作

五毛的cocos2d-x学习笔记05-场景与场景动画,动作

场景切换函数:

  • Director->getInstance()->replaceScene(Scene*);
  • Director->getInstance()->runWithScene(Scene*);

两者的区别在于,如果有Scene存在,使用第一个;第一次运行场景,用第二个。

下面是一个切换场景的关键代码。单击文本跳转到下一个场景。要切换到的场景ImageScene继承自Layer,定义了3个成员函数init,createScene,CREATE_FUNC。

ImageScene.h如下:

 1 #ifndef _IMAGESCENE_H_
 2 #define _IMAGESCENE_H_
 3 
 4 #include<iostream>
 5 #include<cocos2d.h>
 6 USING_NS_CC;
 7 
 8 class ImageScene :public Layer{
 9 public:
10     virtual bool init();
11     static Scene* createScene();
12     CREATE_FUNC(ImageScene);
13 };
14 #endif

ImageScene.cpp如下:

 1 #include"ImageScene.h"
 2 
 3 Scene* ImageScene::createScene(){
 4     Scene *scene = Scene::create();
 5     ImageScene *layer = ImageScene::create();
 6     scene->addChild(layer);
 7 
 8     return scene;
 9 }
10 bool ImageScene::init(){
11     if (!Layer::init()){
12         return false;
13     }
14 
15     Size size = Director::getInstance()->getVisibleSize();
16 
17     Sprite *s = Sprite::create("HelloWorld.png");
18     s->setPosition(size.width/2, size.height/2);
19     addChild(s);
20 
21     return true;
22 }

关键代码:

 1 bool HelloWorld::init()
 2 {
 3     //////////////////////////////
 4     // 1. super init first
 5     if ( !Layer::init() )
 6     {
 7         return false;
 8     }
 9     Size visibleSize = Director::getInstance()->getVisibleSize();
10     Point origin = Director::getInstance()->getVisibleOrigin();
11 
12     LabelTTF *label = LabelTTF::create("Show Next Scene", "Courier", 36);
13     addChild(label);
14 
15     label->setPosition(visibleSize.width/2, visibleSize.height/2);
16 
17     EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
18 
19     listener->onTouchBegan = [label](Touch *t, Event *e){
20         if (label->getBoundingBox().containsPoint(t->getLocation())){
21             Director::getInstance()->replaceScene(ImageScene::createScene());
22             return true;
23         }
24         return false;
25     };
26     Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, label);
27     return true;
28 }

运行效果:

场景切换效果能让场景的切换不显得突兀。场景切换效果主要是TransitionScene的子类实现的。

修改关键代码的第21行 

Director::getInstance()->replaceScene(TransitionFade::create(3.0f, ImageScene::createScene()));

切换效果:

Action动作

类的主要继承关系图

关键代码:

  • label->runAction(MoveTo::create(1.0f, Point(100, 100)));
  • label->runAction(MoveTo::create(1.0f, Point(-100, -100))->reverse());//MoveTo::reserve()在cocos2d-x3.5之后被屏蔽了,因为没有意义
  • label->runAction(MoveBy::create(1.0f, Point(-10, -10)));
  • label->runAction(MoveBy::create(1.0f, Point(10, 10))->reverse());//效果和上面一句相同,reverse是反转执行。

效果是不同的,运行效果:

详细代码如下:

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    
    LabelTTF *label = LabelTTF::create("Hello, Cocos", "Courier", 30);
    label->setPosition(visibleSize.width / 2, visibleSize.height / 2);
    addChild(label);
    
    EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = [label](Touch* t,Event* e){
        if (label->getBoundingBox().containsPoint(t->getLocation())){
            label->runAction(MoveTo::create(1.0f, Point(100, 100)));
            //label->runAction(MoveBy::create(1.0f, Point(-10,-10)));
        }
        return false;
    };
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, label);
    return true;
}

Repeat/RepeatForever:

//在1秒内完成180度旋转,重复3次。

label->runAction(Repeat::create(RotateBy::create(1.0f, 180), 3));

//在1秒内完成180度旋转,一直重复。 label->runAction(RepeatForever::create(RotateBy::create(1.0f, 180)));

运行结果:

动作混合:Spawn类

//label会一边移动一边旋转

//在末尾写一个NULL,标识数组的结束。 label->runAction(Spawn::create(MoveBy::create(1, Point(100, 100)), RotateBy::create(1, 360), NULL));

运行效果:

动作序列:Sequence类

//label先移动,然后再旋转

//在末尾写一个NULL, 标识数组的结束

label->runAction(Sequence::create(MoveBy::create(1, Point(100, 100), RotateBy::create(1, 360)), NULL));

运行效果:

动作的监听:

CallFunc类继承自FiniteTimeAction,所以可以用这个类来实现监听效果。

//label先移动,然后再旋转,旋转结束之后,弹出一个弹框。

label->runAction(Sequence::create(
                MoveBy::create(1, Point(100, 100)), 
                RotateBy::create(1, 360), 
                CallFunc::create([](){
                MessageBox("Action Complete", "Complete");
            }), NULL));

运行结果:

附上本节所有源码:

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    
    LabelTTF *label = LabelTTF::create("Hello, Cocos", "Courier", 30);
    label->setPosition(visibleSize.width / 2, visibleSize.height / 2);
    addChild(label);
    
    EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = [label](Touch* t,Event* e){
        if (label->getBoundingBox().containsPoint(t->getLocation())){
            //label->runAction(MoveTo::create(1.0f, Point(100, 100)));
            //label->runAction(MoveBy::create(1.0f, Point(-10,-10)));
            //label->runAction(MoveBy::create(1.0f, Point(10, 10))->reverse());//效果和上面一句相同
            //label->runAction(Repeat::create(RotateBy::create(1.0f, 180), 3));
            //label->runAction(RepeatForever::create(RotateBy::create(1.0f, 180)));
            //在末尾写一个NULL,标识数组的结束。
            //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);
    return true;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • wamp+thinkphp环境配置

    用户1737026
  • thymeltesys-基于Spring Boot Oauth2的扫码登录框架

    用户1737026
  • 五毛的cocos2d-x学习笔记08-动画

    用户1737026
  • leetcode542. 01 Matrix

    Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each ...

    眯眯眼的猫头鹰
  • python 图像处理:一福变五福

    快过年了,各种互联网产品都出来撒红包。某宝一年一度的集五福(shua hou)活动更是成为每年的必备活动。虽然到最后每人大概也就分个两块钱,但作为一个全民话题,...

    Crossin先生
  • Leetcode 542:01 矩阵 01 Matrix

    Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each ...

    爱写bug
  • .NET常用操作小知识

     var v = StringTruncat("广东省深圳市西乡镇宝安区", 10, "...");

    跟着阿笨一起玩NET
  • 力扣73——矩阵置零

    给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。

    健程之道
  • Leetcode 221. Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square conta...

    triplebee
  • 1004 四子连棋 未完成

    1004 四子连棋  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果 题目描述 Descriptio...

    attack

扫码关注云+社区

领取腾讯云代金券