Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Parallax Background. — Gideros Forum

Parallax Background.

adnaanadnaan Member
edited January 2012 in Code snippets
--[[ This is a rough code I put together for a parallax background. Personally I think it can be done better(as you can see I am drawing the same texture twice!!), but not sure how. Maybe someone here will figure it out]]
 
--Setup--
local bg1 = Bitmap.new(Texture.new("background_a.png"),true)
local bg2 = Bitmap.new(Texture.new("background_a.png"),true)
local bg3 = Bitmap.new(Texture.new("background_b.png"),true)
local bg4 = Bitmap.new(Texture.new("background_b.png"),true)
 
local bgFarY=0
local bgNearY=0
 
--OnEnterFrame--
function drawParallaxBackGround()
    bgFarY = bgFarY -1
	bgNearY = bgNearY -4
 
	local newFarY = bg1:getHeight() -(-bgFarY)
	if newFarY <=0 then
		bgFarY = 0
		bg1:setY(bgFarY)
	else
		bg1:setY(bgFarY)
		bg2:setY(newFarY)
	end
 
	local newNearY = bg3:getHeight() -(-bgNearY)
	if newNearY <=0 then
		bgNearY = 0
		bg3:setY(bgNearY)
	else
		bg3:setY(bgNearY)
		bg4:setY(newNearY)
	end
 
 
end
 
 
--Event--
stage:addEventListener(Event.ENTER_FRAME, drawParallaxBackGround)
 
--Stage--
 
stage:addChild(bg1)
stage:addChild(bg2)
stage:addChild(bg3)
stage:addChild(bg4)

Likes: atilim

+1 -1 (+1 / -0 )Share on Facebook

Comments

Sign In or Register to comment.