cocos2dx中用动作实现背景无限滚动
来源:互联网 发布:印度与中国 知乎 编辑:程序博客网 时间:2024/05/16 17:18
cocos2dx-3.1.5中用动作实现背景滚动,cocos2dx都可以使用的
用到的动作其实很简单
- MoveBy 因为有反动作
- TargetedAction 给指定目标1个动作
- Spawn 同时执行
- Sequence 动作按照顺序执行
- RepeatForever 一直重复执行
如果用定时器,会有画面抖动,因为每帧执行的时间不一样,而我们用动作实现背景滚动只需要关注移动的时间,不需要关注每帧偏移的像素是多少。
头文件
#pragma once#include "GameSceneManager.h"#define DEF_ROLL_SPEDD 8/*背景滚动*/class BackgroundLayer : public Layer{public: virtual bool init(); CREATE_FUNC(BackgroundLayer); Scale9Sprite* createBackGround(Vec2 pos); // 默认背景滚动图片 static int currBgImageIndex; // 滚动速度,值越小速度越快 static int rollTime; void startRollBg(); void stop(); bool isFastRoll = false;};
cpp文件
#include "BackgroundLayer.h"int BackgroundLayer::rollTime = DEF_ROLL_SPEDD;int BackgroundLayer::currBgImageIndex = 1;bool BackgroundLayer::init(){ if (!Layer::init()) { return false; } startRollBg(); return true;}Scale9Sprite* BackgroundLayer::createBackGround(Vec2 pos){ auto background = Scale9Sprite::create(StringUtils::format(BACKGROUND_IMG_NAME, currBgImageIndex)); background->setScale9Enabled(true); background->setContentSize(winSize); background->setPosition(pos); background->setAnchorPoint(Vec2::ZERO); this->addChild(background); return background;}void BackgroundLayer::startRollBg(){ // 创建2张背景图 Scale9Sprite* bg = createBackGround(Vec2::ZERO); Scale9Sprite* bg2 = createBackGround(Vec2(0, winSize.height)); // 滚动动作 auto mt = MoveBy::create(rollTime, Vec2(0, -winSize.height)); auto ta = TargetedAction::create(bg, mt); auto ta2 = TargetedAction::create(bg2, mt->clone()); // 同时执行 auto spawn = Spawn::createWithTwoActions(ta, ta2); // 重复执行,设定位置 auto rf = RepeatForever::create(Sequence::createWithTwoActions(spawn, CallFunc::create([=]() { //this->stopAllActions(); bg->setPositionY(0); bg2->setPositionY(winSize.height); //log("----"); }))); // 当前Layer执行这个Action this->runAction(rf);}void BackgroundLayer::stop(){ this->stopAllActions();}
阅读全文
0 0
- cocos2dx中用动作实现背景无限滚动
- cocos2dx实现背景的无限滚动
- cocos2dx 背景无限滚动
- cc随笔:cocos2dx 创建无限滚动的移动背景
- cocos2dx-无限循环动作
- 无限背景滚动
- 【Cocos2d-x】实现可上下左右无限滚动的背景
- cocos2d-x无限滚动背景
- cocos2d 背景滚动 移动 无限
- cocos: 无限循环滚动背景
- cocos2dx 实现重复滚动背景时遇到的一些问题
- UGUI 实现无限滚动
- cocos2dx 物品下落动作实现
- Unity实例.001无限滚动的背景
- 【Cocos2d-x】新手自学(九)滚动背景..实现无限卷轴效果(2.0.1版本)
- 【unity3d学习笔记】Shader-通过Shader实现2D游戏中无限滚动的背景Shader
- 滚动背景的实现
- AngularJs实现无限滚动加载
- 简单的多线程编程_同步与互斥问题
- lightoj 1028
- python中的pop函数和append函数
- 用VirtualBox建立一个ubuntu机群
- PAT 1017. Queueing at Bank (25) 屡次段错误,终于过了
- cocos2dx中用动作实现背景无限滚动
- POJ 2955 _Brackets (区间Dp)
- svn cleanup
- ajax返回中文乱码
- C++——USACO Section 2.1 题解
- pdb调试python代码
- MySql优化-04
- 微信小程序上传图片
- STL容器