How do you make your background move?

Hi, I want to make my bg scroll down and be replaced by a 2nd bg that I've positioned directly above the 1st. I've got a draggable object (my player) that I want to trigger the scrolling movement when it hits the top of the screen. I figure I need to write some sort of code like

1
2
3
4
5
6
7
8
9
10
local function wrap (event)
 
if player.y < 50 then
player.y = 50
--add some sort of "move bg's and player 480 pixels down" command--
end
 
end
 
Runtime:addEventListener("enterFrame", wrap)

I guess you mean scrolling form top to bottom. If that's true, you may want your 2nd bg "minus" 480 instead of plus.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
--first place your bg2 480 pix above bg1
bg2.y = bg1.y - 480
 
if player.y < 50 then
        player.y = 50
        -- you may want to do something with the eventlistener for the player's here
        player.y = player.y + 380
        bg1.y = bg1.y + 480
        bg2.y = bg2.y + 480
end
 
-- OR make it move smoothly 
 
if player.y < 50 then
        player.y = 50
        -- you may want to do something with the eventlistener for the player's here
        transition.to(player, {time = 395, delay = 0, y = player.y + 380})
        transition.to(bg1,    {time = 500, delay = 0, y = bg1.y + 480})
        transition.to(bg2,    {time = 500, delay = 0, y = bg2.y + 480})
end

Thanks Dan300, this looks like what I'm after. I'll give it a try.

Cheers,
Steven

@Dan300 Your code worked great, thank-you. More impressively, you're spot on with your notes, turns out I do indeed want to do something with the eventlistener for the player where you indicated. As you forsaw, it looks funny to have the player zipping back and forth while the backgrounds are transitioning (not to mention the player snapping to the user's finger once the transition is done). I need to take control away from the user while this transition takes place. So now the question is: how do you temporarily cancel an event? I tried inserting

1
event.phase = "cancelled"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if "moved" == event.phase then
 
        if player.y < 50 then
                -- add the listener back after 0.5 sec
                timer.performWithDelay(500, function(event) player:addEventListener( "touch", NameOfYourListener ) end, 1 )
                -- remove the listener for now
                player:removeEventListener( "touch", NameOfYourListener )
                
                -- add scrolling movement code here
                
                
                return
        end
        
elseif "ended" == event.phase or "cancelled" == event.phase then
end

@Dan300 Worked perfectly. You're a pro.

Much thanks,
Steven

views:1641 update:2011/9/27 8:54:05
corona forums © 2003-2011