Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Parallax in function — Gideros Forum

Parallax in function

JojojohnJojojohn Member
edited August 2013 in General questions
Everytime I put my function BackGround code in a function of a class the output was different. Can somebody help me? Please. Thank you!
Here is my code:

sample=gideros.class(Sprite)
function sample:init()
...
stage:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
...
end

local bg1 = Bitmap.new(Texture.new("background.jpg"),true)
local bg2 = Bitmap.new(Texture.new("background.jpg"),true)
local bgX=0

function sample:onEnterFrame()
if self.started then
function BackGround()
bgX = bgX - 1
local newBgX = bg1:getWidth() -(-bgX)
if newBgX <=0
then
bgX=0
bg1:setX(bgX)
else
bg1:setX(bgX)
bg2:setX(newBgX)
end
end
self:addEventListener(Event.ENTER_FRAME, BackGround)
self:addChild(bg1)
self:addChild(bg2)
end
end


*By the way the output here was about the background that goes to right then goes back to the left. Also the speed was increasing. Please I really need your help guys. Thank you!

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @Jojojohn most probably your problem is of mixing up scene events and stage events together. If you are using sceneManager you should only do everything for your current scene, without touching the stage.
  • JojojohnJojojohn Member
    edited August 2013
    @ar2rsawseen but I didn't used scenemanager, I only used addEventListener?
  • ar2rsawseenar2rsawseen Maintainer
    edited August 2013 Accepted Answer
    @Jojojohn your problem is that inside one onEnterFrame event you on each frame add another enterframe event. Thus it inreases the speed because bgX = bgX - 1 is called more and more times on each enter frame as you multiply them on each enter frame
    You need something more like this:
    sample=gideros.class(Sprite)
     
    local bg1 = Bitmap.new(Texture.new("background.jpg", true))
    local bg2 = Bitmap.new(Texture.new("background.jpg", true))
    local bgX=0
     
    function sample:init()
        ...
        self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
        ...
        self:addChild(bg1)
        self:addChild(bg2)
    end
     
    function sample:onEnterFrame()
        if self.started then
            bgX = bgX - 1
            local newBgX = bg1:getWidth() -(-bgX)
            if newBgX <=0 then
                bgX=0
                bg1:setX(bgX)
            else
                bg1:setX(bgX)
                bg2:setX(newBgX) 
            end
       end
    end
  • @ar2rsawseen thank you! :) I removed the function BackGround inside the function sample:onEnterFrame() and it works! :) Thank you so much. I realized my error. Thanks again.
Sign In or Register to comment.