Scrolling background

Hi there, I’m facing a realy strage problem with scrolling a particular object in my game…

the other objects are scrolling perfectly. The weird thing is when I’m coping - pasting the code that works just fine it still doesnt work. Is there a Runtime - enterFrame functions limit I dont know about?

Scrolling handler:

local function scrollBackground( self, event ) &nbsp; &nbsp; if( self.x \< -( \_W ) ) then &nbsp; &nbsp; &nbsp; &nbsp; self.x = \_W; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; self.x = self.x - self.speed; &nbsp; &nbsp; end end

branch2 is the problematic object… 

&nbsp; &nbsp; branch1 = display.newImageRect( "images/branch.png", \_W, \_H \* 0.1 ); &nbsp; &nbsp; branch1.anchorX, branch1.anchorY = 0, \_H; &nbsp; &nbsp; branch1.x, branch1.y = 0, \_CY; &nbsp; &nbsp; branch1.speed = 3 &nbsp; &nbsp; branch1.enterFrame = scrollBackground; &nbsp; &nbsp; branch2 = display.newImageRect( "images/branch.png", \_W, \_H \* 0.1 ); &nbsp; &nbsp; branch2.anchorX, branch2.anchorY = 0, \_H; &nbsp; &nbsp; branch2.x, branch2.y = \_W, \_CY; &nbsp; &nbsp; branch2.speed = 3 &nbsp; &nbsp; branch2.enterFrame = scrollBackground;

&nbsp; &nbsp; &nbsp; &nbsp; Runtime:addEventListener( "enterFrame", branch1 ); &nbsp; &nbsp; &nbsp; &nbsp; Runtime:addEventListener( "etnerFrame", branch2 );

Again, the other objects in the game are scrolling just as they suppost with the same scrolling function…

I’d be happy if someone could test this in his code…

Cannot spot the issue if somone does please let me know

Thanks,

Itay

I have used rects instead images.

Try (simple method)

local \_W, \_CY = display.contentWidth, display.contentCenterY branch1 = display.newRect(0, 0, 50, 200); branch1.anchorX, branch1.anchorY = 0, 1; branch1:setFillColor(0.1, 0.7, 0.3) branch1.x, branch1.y = 0, \_CY; branch1.speed = 3 branch2 = display.newRect(0, 0, 50, 100); branch2.anchorX, branch2.anchorY = 0, 1; branch2.x, branch2.y = \_W, \_CY; branch2.speed = 3 local function scrollBackground( object ) if( object.x \< -\_W ) then &nbsp; &nbsp; object.x = \_W; else &nbsp; &nbsp; object.x = object.x - object.speed; end end local function enterFrame(event)&nbsp; &nbsp; &nbsp; scrollBackground(branch1) &nbsp; &nbsp; scrollBackground(branch2) end Runtime:addEventListener( "enterFrame", enterFrame );

Try (more complex one)

app.lua 

local \_M = {} -- Call f next frame function \_M.nextFrame(f) timer.performWithDelay(1, f) end -- For internal use function \_M.enterFrame() for i = 1, #\_M.enterFrameFunctions do \_M.enterFrameFunctions[i]() end end -- Call f each frame function \_M.eachFrame(f) if not \_M.enterFrameFunctions then \_M.enterFrameFunctions = {} Runtime:addEventListener('enterFrame', \_M.enterFrame) end table.insert(\_M.enterFrameFunctions, f) return f end -- Stop calling f function \_M.eachFrameRemove(f) if not f or not \_M.enterFrameFunctions then return end local ind = table.indexOf(\_M.enterFrameFunctions, f) if ind then table.remove(\_M.enterFrameFunctions, ind) if #\_M.enterFrameFunctions == 0 then Runtime:removeEventListener('enterFrame', \_M.enterFrame) \_M.enterFrameFunctions = nil end end end -- Stop everything function \_M.eachFrameRemoveAll() Runtime:removeEventListener('enterFrame', \_M.enterFrame) \_M.enterFrameFunctions = nil end return \_M

local app = require('lib.app') local \_W, \_CY = display.contentWidth, display.contentCenterY branch1 = display.newRect(0, 0, 100, 300); branch1:setFillColor(0.2, 0.3, 0.4) branch1.anchorX, branch1.anchorY = 0, 1; branch1.x, branch1.y = 0, \_CY; branch1.speed = 3 branch2 = display.newRect(0, 0, 50, 100); branch1:setFillColor(0.1, 0.7, 0.3) branch2.anchorX, branch2.anchorY = 0, 1; branch2.x, branch2.y = 0, \_CY; branch2.speed = 3 function branch1:startScrollBackground() &nbsp; &nbsp; &nbsp; self.fId = app.eachFrame(function() &nbsp; &nbsp; &nbsp; &nbsp; if ( self.x \< -\_W &nbsp;) then &nbsp; &nbsp; &nbsp; &nbsp;self.x = \_W; &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; &nbsp;self.x = self.x - self.speed; &nbsp; &nbsp;end &nbsp; &nbsp; end) end function branch1:finalize() &nbsp; &nbsp; &nbsp; app.eachFrameRemove(self.fId) end &nbsp; branch1:addEventListener('finalize') branch1:startScrollBackground() &nbsp; function branch2:startScrollBackground() &nbsp; &nbsp; &nbsp; self.fId = app.eachFrame(function() &nbsp; &nbsp; &nbsp; &nbsp; if ( self.x \< -\_W &nbsp;) then &nbsp; &nbsp; &nbsp; &nbsp;self.x = \_W; &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; &nbsp;self.x = self.x - self.speed; &nbsp; &nbsp;end &nbsp; &nbsp; end) end function branch2:finalize() &nbsp; &nbsp; &nbsp; app.eachFrameRemove(self.fId) end&nbsp; branch2:addEventListener('finalize') branch2:startScrollBackground()&nbsp;

Last code is based on tip from spiralcodestudio.com.

Unless you set 

display.setDefault( "isAnchorClamped", false )

the anchor points are limited to the range between 0.0 and 1.0 Read more on Tutorial: Extending Anchor Points.

You don’t need use semicolon at end of the line. You have to do this if you write more than one instruction in the same line.

Maybe this link ‘What is faster - many small enterframe listeners’ or only one but large’ would be helpful.

It was a typo mistake… “etnerFrame” instead of “enterFrame”. realy stupid of me haha

Thanks anyway!

Itay

Ok :) 

I have used rects instead images.

Try (simple method)

local \_W, \_CY = display.contentWidth, display.contentCenterY branch1 = display.newRect(0, 0, 50, 200); branch1.anchorX, branch1.anchorY = 0, 1; branch1:setFillColor(0.1, 0.7, 0.3) branch1.x, branch1.y = 0, \_CY; branch1.speed = 3 branch2 = display.newRect(0, 0, 50, 100); branch2.anchorX, branch2.anchorY = 0, 1; branch2.x, branch2.y = \_W, \_CY; branch2.speed = 3 local function scrollBackground( object ) if( object.x \< -\_W ) then &nbsp; &nbsp; object.x = \_W; else &nbsp; &nbsp; object.x = object.x - object.speed; end end local function enterFrame(event)&nbsp; &nbsp; &nbsp; scrollBackground(branch1) &nbsp; &nbsp; scrollBackground(branch2) end Runtime:addEventListener( "enterFrame", enterFrame );

Try (more complex one)

app.lua 

local \_M = {} -- Call f next frame function \_M.nextFrame(f) timer.performWithDelay(1, f) end -- For internal use function \_M.enterFrame() for i = 1, #\_M.enterFrameFunctions do \_M.enterFrameFunctions[i]() end end -- Call f each frame function \_M.eachFrame(f) if not \_M.enterFrameFunctions then \_M.enterFrameFunctions = {} Runtime:addEventListener('enterFrame', \_M.enterFrame) end table.insert(\_M.enterFrameFunctions, f) return f end -- Stop calling f function \_M.eachFrameRemove(f) if not f or not \_M.enterFrameFunctions then return end local ind = table.indexOf(\_M.enterFrameFunctions, f) if ind then table.remove(\_M.enterFrameFunctions, ind) if #\_M.enterFrameFunctions == 0 then Runtime:removeEventListener('enterFrame', \_M.enterFrame) \_M.enterFrameFunctions = nil end end end -- Stop everything function \_M.eachFrameRemoveAll() Runtime:removeEventListener('enterFrame', \_M.enterFrame) \_M.enterFrameFunctions = nil end return \_M

local app = require('lib.app') local \_W, \_CY = display.contentWidth, display.contentCenterY branch1 = display.newRect(0, 0, 100, 300); branch1:setFillColor(0.2, 0.3, 0.4) branch1.anchorX, branch1.anchorY = 0, 1; branch1.x, branch1.y = 0, \_CY; branch1.speed = 3 branch2 = display.newRect(0, 0, 50, 100); branch1:setFillColor(0.1, 0.7, 0.3) branch2.anchorX, branch2.anchorY = 0, 1; branch2.x, branch2.y = 0, \_CY; branch2.speed = 3 function branch1:startScrollBackground() &nbsp; &nbsp; &nbsp; self.fId = app.eachFrame(function() &nbsp; &nbsp; &nbsp; &nbsp; if ( self.x \< -\_W &nbsp;) then &nbsp; &nbsp; &nbsp; &nbsp;self.x = \_W; &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; &nbsp;self.x = self.x - self.speed; &nbsp; &nbsp;end &nbsp; &nbsp; end) end function branch1:finalize() &nbsp; &nbsp; &nbsp; app.eachFrameRemove(self.fId) end &nbsp; branch1:addEventListener('finalize') branch1:startScrollBackground() &nbsp; function branch2:startScrollBackground() &nbsp; &nbsp; &nbsp; self.fId = app.eachFrame(function() &nbsp; &nbsp; &nbsp; &nbsp; if ( self.x \< -\_W &nbsp;) then &nbsp; &nbsp; &nbsp; &nbsp;self.x = \_W; &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; &nbsp;self.x = self.x - self.speed; &nbsp; &nbsp;end &nbsp; &nbsp; end) end function branch2:finalize() &nbsp; &nbsp; &nbsp; app.eachFrameRemove(self.fId) end&nbsp; branch2:addEventListener('finalize') branch2:startScrollBackground()&nbsp;

Last code is based on tip from spiralcodestudio.com.

Unless you set 

display.setDefault( "isAnchorClamped", false )

the anchor points are limited to the range between 0.0 and 1.0 Read more on Tutorial: Extending Anchor Points.

You don’t need use semicolon at end of the line. You have to do this if you write more than one instruction in the same line.

Maybe this link ‘What is faster - many small enterframe listeners’ or only one but large’ would be helpful.

It was a typo mistake… “etnerFrame” instead of “enterFrame”. realy stupid of me haha

Thanks anyway!

Itay

Ok :)