首页 > 代码库 > Cocos2d-x3.2 Speed、Follow的使用

Cocos2d-x3.2 Speed、Follow的使用

//GameScene.h

#include "cocos2d.h"

class GameScene : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
    
    virtual bool init();
    
    void menuCallback(cocos2d::Ref* pSender);
    
    CREATE_FUNC(GameScene);
};




//GameScene.cpp

#include "GameScene.h"

USING_NS_CC;

cocos2d::Scene* GameScene::createScene()
{
    auto scene = Scene::create();   //创建一个场景
    auto layer = GameScene::create();   //创建一个图层
    scene->addChild(layer);
    return scene;
}

//初始化当前的图层
bool GameScene::init()
{
    if(!Layer::init())      //初始化父类
        return false;
    
    //获取屏幕大小
    Size visibleSize = Director::getInstance()->getVisibleSize();
    //auto size = Director::getInstance()->getWinSize();
    //添加一个图片精灵
    auto sprite = Sprite::create("OnePiece_1.png");
    sprite->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
    this->addChild(sprite);
    
    auto sprite1 = Sprite::create("Icon.png");
    sprite1->setPosition(Vec2(visibleSize.width*0.2, visibleSize.height/2));
    this->addChild(sprite1);
    
    auto moveTo = MoveTo::create(2, Vec2(visibleSize.width*0.8, visibleSize.height/2));
    //sprite1->runAction(moveTo);
    
    //速度变化
    auto speed = Speed::create(moveTo, 2);
    sprite1->runAction(speed);
    
    //背景跟随
    auto follow = Follow::create(sprite1);
    this->runAction(follow);
    
    return true;
}