前端之家收集整理的这篇文章主要介绍了
Cocos2d-x_创建多个滚动层(每层速度不同),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
local scheduler = cc.Director:getInstance():getScheduler()
local winSize = cc.Director:getInstance():getWinSize()
--创建循环地图
local function createLoopMap(name,tag)
local width = 1536
local m_bg1 = cc.Sprite:create(name)
m_bg1:setAnchorPoint(cc.p(0,0))
m_bg1:setPosition(cc.p(0,0))
m_bg1:setTag(111)
local m_bg2 = cc.Sprite:create(name)
m_bg2:setAnchorPoint(cc.p(0,0))
m_bg2:setPosition(cc.p(winSize.width,0))
m_bg2:setTag(222)
local node = cc.Node:create()
node:addChild(m_bg1)
node:addChild(m_bg2)
node:setTag(tag)
return node
end
local m_bg1 = createLoopMap("bg1.jpg",1)
self.m_bg1 = m_bg1
local m_bg2 = createLoopMap("crop.png",2)
self.m_bg2 = m_bg2
self.panel.layer:addChild(m_bg1)
self.panel.layer:addChild(m_bg2)
local function updateFunc(dt)
for i = 1,2 do
local node = self.panel.layer:getChildByTag(i)
local bg1 = node:getChildByTag(111)
local bg2 = node:getChildByTag(222)
local posX1 = bg1:getPositionX()
local posX2 = bg2:getPositionX()
local speed = 0
if i == 1 then
speed = 5.0
elseif i == 2 then
speed = 10.0
end
posX1 = posX1 - speed
posX2 = posX2 - speed
local width = winSize.width
if posX1 < -width then
posX2 = 0
posX1 = width
end
if posX2 < -width then
posX1 = 0
posX2 = width
end
bg1:setPositionX(posX1)
bg2:setPositionX(posX2)
end
end
if self.updateHandler == nil then
self.updateHandler = scheduler:scheduleScriptFunc(updateFunc,false)
end
原文链接:https://www.f2er.com/cocos2dx/341656.html