How can I change the direction of a scrolling background?

Hi everyone, I figured I’d give Corona a shot today to see if it made life any easier. I’m slowly making decent progress and had an issue. I’m trying to make a scrolling background sprite and it works but not in the direction I want. Would any of you kind folk know what I’m doing wrong in order to get the direction to change?

Please be kind looking at my code as it’s my first attempt using Lua. I may even change the sprite size and tile it more if there’s a performance issue on the device but for now this works other than the direction I would like.

Thanks!

Here’s the code:

[code]
require “sprite”
display.setStatusBar( display.HiddenStatusBar )
– Background
– When one of the background images slides offscreen, we move it to the bottom of the next one.
local background = display.newImage( “Background.png” )
background:setReferencePoint( display.CenterLeftReferencePoint )
background.x = 0
background.y = 240

local background2 = display.newImage( “Background.png” )
background2:setReferencePoint( display.CenterLeftReferencePoint )
background2.x = 0
background2.y = -240
– A per-frame event to move the elements
local tPrevious = system.getTimer()
local function move(event)
local tDelta = event.time - tPrevious
tPrevious = event.time

local yOffset = ( 0.1 * tDelta )

background.y = background.y - yOffset
background2.y = background2.y - yOffset

if (background.y + background.stageHeight) < 240 then
background:translate( 0, 480 * 2)
end
if (background2.y + background2.stageHeight) < 240 then
background2:translate( 0, 480 * 2)
end
end

– Start everything moving
Runtime:addEventListener( “enterFrame”, move );[/code] [import]uid: 21827 topic_id: 5215 reply_id: 305215[/import]

Ok well I changed the - yOffset to + yOffset and that changes the direction but kills the infinite scrolling somehow. [import]uid: 21827 topic_id: 5215 reply_id: 17395[/import]

can u zip the file with the image and put it somehere to download

you want a continious sliding background?

u are on the right track

c [import]uid: 24 topic_id: 5215 reply_id: 17444[/import]

http://www3.sympatico.ca/tlavier/linkedphotos/project.zip

Thanks Carlos. As you can see, it scrolls infinitely from bottom to top but I’d like to have it scroll from top to bottom. I can make it work but then it breaks the infinite scrolling. [import]uid: 21827 topic_id: 5215 reply_id: 17487[/import]

I’m stuck in the same boat. Anyone with an answer for this? VidKid72 is absolutely correct, when you change the value to a - it scrolls correctly, but it’s no longer infinite. I tried it myself with single background, two backgrounds, etc… [import]uid: 10379 topic_id: 5215 reply_id: 23466[/import]

hi this one nice, but how can i change the horizontal directions? can u give one example on this? [import]uid: 40515 topic_id: 5215 reply_id: 28067[/import]

Hello Vidkid72.

You’ve forgot to change the condition for the direction change.

It works with

[lua]if (background.y + background.contentHeight) > 1200 then[/lua]

and

[lua]if (background2.y + background2.contentHeight) > 1200 then[/lua] [import]uid: 42078 topic_id: 5215 reply_id: 28148[/import]

Thanks Sidasa but it still doesn’t work with what you suggested. 2 screens slide by and the scrolling stops. Believe me, I’ve tried many conditions but can’t get it to scroll down.

Carlos never got back to this thread either. I’m almost going to say there’s a bug somewhere within Corona to prevent this code from functioning. Almost. :wink:

I’ve certainly given up on Corona and returned to XCode. [import]uid: 21827 topic_id: 5215 reply_id: 28159[/import]

Hi.

i tried it myself and it works.

here the whole code:

[lua]–
– Abstract: JungleScene sample project
– Demonstrates sprite sheet animations, with different frame rates

– Version: 1.0

– Sample code is MIT licensed, see http://developer.anscamobile.com/code/license
– Copyright © 2010 ANSCA Inc. All Rights Reserved.

– Plants are from http://blender-archi.tuxfamily.org/Greenhouse
– and are subject to creative commons license, http://creativecommons.org/licenses/by/3.0/
– Make the Default.png last a little longer

– Hide the iPhone status bar
require “sprite”
display.setStatusBar( display.HiddenStatusBar )
– Background
– When one of the background images slides offscreen, we move it to the bottom of the next one.
local background = display.newImage( “Background.png” )
background:setReferencePoint( display.CenterLeftReferencePoint )
background.x = 0
background.y = 240

local background2 = display.newImage( “Background.png” )
background2:setReferencePoint( display.CenterLeftReferencePoint )
background2.x = 0
background2.y = -240
– A per-frame event to move the elements
local tPrevious = system.getTimer()
local function move(event)
local tDelta = event.time - tPrevious
tPrevious = event.time

local yOffset = ( 0.2 * tDelta )

background.y = background.y + yOffset
background2.y = background2.y + yOffset

if (background.y + background.contentHeight) > 1200 then
print( "1: " … background.y … “/”… background.contentHeight )
background:translate(0, - 480 * 2)

end
if (background2.y + background2.contentHeight) > 1200 then
print( "2: " … background2.y … “/”… background2.contentHeight )
background2:translate(0, - 480 * 2)

end
end

– Start everything moving
Runtime:addEventListener( “enterFrame”, move );[/lua] [import]uid: 42078 topic_id: 5215 reply_id: 28160[/import]

Thanks Sidasa, that certainly works with the additional code and not just the conditions like you first posted. :wink:

I really appreciate you taking the time to look at this. Now that it works I might try Corona again. [import]uid: 21827 topic_id: 5215 reply_id: 28169[/import]