Gap when drag scrolling background

Hi all,

I have a drag scroll background composed of two images that wraps when reaching each side. The problem is a vertical gap that appears between each image even though the images get correctly positioned. The gap is variable based on the speed of flick and grows if keep dragging back and forth. I replaced the images with rectangles for ease of testing. Any ideas on how I can fix?

I am using a stripped down version of the code found on the Game Development forum @ http://bit.ly/nJdDK1.

Thanks for taking a look!

[lua]display.setStatusBar(display.HiddenStatusBar) – Hide status bar

local bg1 = display.newRect(0, 0, 1024, 768)
local bg2 = display.newRect(-1024, 0, 1024, 768)
bg1:setFillColor(40, 91, 142)
bg2:setFillColor(0, 153, 102)
bg1:setReferencePoint(display.CenterLeftReferencePoint)
bg2:setReferencePoint(display.CenterLeftReferencePoint)

– Set up variables
local previousX = 0
local scrollVelocity = 0
local scrollDirection
local scrollFriction = 5
local xMove = 0

local function flickScroll() – used to add on velocity after touch stopped
bg1.x = bg1.x + scrollVelocity – make the move
bg2.x = bg2.x + scrollVelocity

– moving left
if (scrollDirection==“left”) then
scrollVelocity = scrollVelocity + scrollFriction
if (scrollVelocity > 0) then
scrollVelocity = 0
Runtime:removeEventListener(“enterFrame”, flickScroll)
end
end

– moving right
if (scrollDirection==“right”) then
scrollVelocity = scrollVelocity - scrollFriction
if (scrollVelocity < 0) then
scrollVelocity = 0
Runtime:removeEventListener(“enterFrame”, flickScroll)
end
end
end

local function makeScroll(event)
local target = event.target
local phase = event.phase
local time = event.time
if (event.phase == “began”) then
display.getCurrentStage():setFocus(event.target) – lock the focus onto the scrolling
startTime = event.time – record the start time of this drag
scrollVelocity = 0
end
if (event.phase == “moved”) then
if (previousX == 0) then
previousX = event.x
end
xMove = event.x - previousX
previousX = event.x
if (xMove < 0) then
scrollDirection = “left”
else
scrollDirection = “right”
end
print("xMove: " … xMove)
print("bg1.x: " … bg1.x)
print("bg2.x: " … bg2.x)
print("scrollDirection: " … scrollDirection)
print("scrollVelocity: " … scrollVelocity)
print("previousX: " … previousX)
print("event.x: " … event.x)

– make the background move
bg1.x = bg1.x + xMove
bg2.x = bg2.x + xMove

– Reposition images once they fall off stage
if bg1.x > 1024 then bg1.x = -1024 end
if bg2.x > 1024 then bg2.x = -1024 end
if bg1.x < -1024 then bg1.x = 1024 end
if bg2.x < -1024 then bg2.x = 1024 end
end
if (event.phase == “ended”) then
scrollVelocity = xMove
Runtime:addEventListener(“enterFrame”, flickScroll)
xMove = 0
previousX = 0
end
end
Runtime:addEventListener(“touch”, makeScroll)[/lua]
[import]uid: 12397 topic_id: 12411 reply_id: 312411[/import]

Anybody?!?

Am I in the correct forum?

Am I asking a stupid question? [import]uid: 12397 topic_id: 12411 reply_id: 45573[/import]

Hey,

Ah, well, it would have been better to post in Developer Support - New Users is more for the really basic stuff :slight_smile:

It might be worth just having a play with your code to adjust some things and see if it helps; for example if you set your friction at 10 the gap gets smaller.

Maybe try a function when touch is released that checks the images are where they should be in relation to one another? [import]uid: 52491 topic_id: 12411 reply_id: 45601[/import]

Hi Peach,

Thanks for taking a look. Changing the friction does not appear to affect the size of the gap when dragging quickly back and forth or when flicking in once direction quickly. Also, your final suggestion of firing a function to check position on touch release doesn’t sound like it would help, as the gap appears while dragging is moving as well as after dragging has ended. Am I missing something?

The print statement for xMove reveals a value that is alway even. Is this a clue?

I am basically looking to add drag scroll capabilities to a wrapping background. Would I be better off trying to modify the List View 1 in the sample code?

Thanks again, I do appreciate your time. [import]uid: 12397 topic_id: 12411 reply_id: 45746[/import]

here is the solution but as i am in hurry as it is a weekend and need to go home i had just copied some code from ur example of velocity but it is working properly except velocity is much higher
try this code
[lua]display.setStatusBar(display.HiddenStatusBar) – Hide status bar

local bg1 = display.newRect(0, 0, 1024, 768)
local bg2 = display.newRect(-1024, 0, 1024, 768)
bg1:setFillColor(40, 91, 142)
bg2:setFillColor(0, 153, 102)

– Set up variables
local previousX = 0
local scrollVelocity = 0
local scrollDirection
local scrollFriction = 5
local xMove = 0

local function flickScroll() – used to add on velocity after touch stopped
bg1.x = bg1.x + scrollVelocity – make the move
bg2.x = bg2.x + scrollVelocity
if bg1.x >= 1024 + 512 then
bg1.x = bg2.x - 1024
x1 = e.x - bg1.x
end
if bg2.x >= 1024 + 512 then
bg2.x = bg1.x - 1024
x2 = e.x - bg2.x
end
if bg1.x <= -512 then
bg1.x = bg2.x + 1024
x1 = e.x - bg1.x
end
if bg2.x <= -512 then
bg2.x = bg1.x + 1024
x2 = e.x - bg2.x
end
– moving left
if (scrollDirection==“left”) then
scrollVelocity = scrollVelocity + scrollFriction
if (scrollVelocity > 0) then
scrollVelocity = 0
Runtime:removeEventListener(“enterFrame”, flickScroll)
end
end

– moving right
if (scrollDirection==“right”) then
scrollVelocity = scrollVelocity - scrollFriction
if (scrollVelocity < 0) then
scrollVelocity = 0
Runtime:removeEventListener(“enterFrame”, flickScroll)
end
end
end
local function makeScroll(e)
if “began” == e.phase then
x1 = e.x - bg1.x
x2 = e.x - bg2.x
scrollVelocity = 0
elseif “moved” == e.phase then
if (previousX == 0) then
previousX = e.x
end
scrollVelocity = e.x - previousX
bg1.x = e.x - x1
bg2.x = e.x - x2
if bg1.x >= 1024 + 512 then
bg1.x = bg2.x - 1024
x1 = e.x - bg1.x
end
if bg2.x >= 1024 + 512 then
bg2.x = bg1.x - 1024
x2 = e.x - bg2.x
end
if bg1.x <= -512 then
bg1.x = bg2.x + 1024
x1 = e.x - bg1.x
end
if bg2.x <= -512 then
bg2.x = bg1.x + 1024
x2 = e.x - bg2.x
end
elseif “ended” == e.phase then
– Runtime:addEventListener(“enterFrame”, flickScroll)
xMove = 0
previousX = 0

end
end
Runtime:addEventListener(“touch”, makeScroll)[/lua]
note tested in landscape mode with width = 1024 and height = 768

let me know if anything else required will be available on monday
happy weekend to both of you also
:slight_smile: [import]uid: 12482 topic_id: 12411 reply_id: 45770[/import]

alright its Monday and here’s the full solution

[lua]display.setStatusBar(display.HiddenStatusBar) – Hide status bar

local bg1 = display.newRect(0, 0, 1024, 768)
local bg2 = display.newRect(-1024, 0, 1024, 768)
bg1:setFillColor(40, 91, 142)
bg2:setFillColor(0, 153, 102)

– Set up variables
local previousX = 0
local scrollVelocity = 0

local function flickScroll() – used to add on velocity after touch stopped
bg1.x = bg1.x + scrollVelocity – make the move
bg2.x = bg2.x + scrollVelocity
if bg1.x >= 1024 + 512 then
bg1.x = bg2.x - 1024
end
if bg2.x >= 1024 + 512 then
bg2.x = bg1.x - 1024
end
if bg1.x <= -512 then
bg1.x = bg2.x + 1024
end
if bg2.x <= -512 then
bg2.x = bg1.x + 1024
end
scrollVelocity = scrollVelocity * 0.95

if scrollVelocity < 0.1 and scrollVelocity > -0.1 then
scrollVelocity = 0
end
end

local function makeScroll(e)
if “began” == e.phase then
x1 = e.x - bg1.x
x2 = e.x - bg2.x
scrollVelocity = 0
elseif “moved” == e.phase then
if (previousX == 0) then
previousX = e.x
end
scrollVelocity = e.x - previousX

bg1.x = e.x - x1
bg2.x = e.x - x2
if bg1.x >= 1024 + 512 then
bg1.x = bg2.x - 1024
x1 = e.x - bg1.x
end
if bg2.x >= 1024 + 512 then
bg2.x = bg1.x - 1024
x2 = e.x - bg2.x
end
if bg1.x <= -512 then
bg1.x = bg2.x + 1024
x1 = e.x - bg1.x
end
if bg2.x <= -512 then
bg2.x = bg1.x + 1024
x2 = e.x - bg2.x
end
elseif “ended” == e.phase then
Runtime:addEventListener(“enterFrame”, flickScroll)
previousX = 0

end
end
Runtime:addEventListener(“touch”, makeScroll)[/lua] [import]uid: 12482 topic_id: 12411 reply_id: 46001[/import]

@ hgvyas123,

Your code indeed solves the vertical gap issue… thank you VERY much! While I’ll have to get a good night sleep before I can really figure it all out, here are my initial thoughts.

After testing in the simulator and on an iPad, I’m thinking the rate of velocity is way too high given my scenario. I decreased the scrollVelocity multiplier (scrollVelocity = scrollVelocity * 0.75), which slowed the speed down, but caused the easing to appear unnatural.

Is there a better way to slow the scrolling down while retaining the easing?

I also notice that the screen dragging does not stay in sync with the cursor/finger, i.e. the screen zooms ahead of your drag position. You can see this by dragging on the screen where one rectangle meets another. How can I change this?

Finally, I noticed that you have many conditionals containing 1024 + 512. Is there a particular reason for this? Would it be faster to simply write 1536? I hope this was not too dumb of a question.

Thanks again for your AWESOME help. [import]uid: 12397 topic_id: 12411 reply_id: 46028[/import]

  1. there are lots of way if you think rate of velocity is too high

one is as you had given another change the line scrollVelocity = e.x - previousX in makeScroll function with this

scrollVelocity = (e.x - previousX)/5
2) i can’t understand about screen zooms ahead what’s that

  1. reason to use 1024 + 512 is just to simplicity i doubt it is hard to figure out why i had used 1536 really so for simplicity i had used 1024 + 512 u r welcome to use 1536
    :slight_smile: [import]uid: 12482 topic_id: 12411 reply_id: 46036[/import]

@hgvyas123,

Per your suggestion I played around with the scrollVelocity within the makeScroll function and ended up with scrollVelocity = (e.x - previousX)*0.1, which is just perfect for my needs.

Never mind the dumb question about the screen zooming ahead of the touch point… slowing down the velocity via the above code had the desired effect.

I inserted a print statement for scrollVelocity within the flickScroll function out of curiosity and noticed that it never stopped calculating, even though the screen was stationary. Adding a line to remove the flickScroll event listener after the scrollVelocity is zeroed out took care of that.

if scrollVelocity \< 0.1 and scrollVelocity \> -0.1 then  
 scrollVelocity = 0  
 Runtime:removeEventListener("enterFrame", flickScroll)  
end  

Final dumb question, I assume I don’t need to set the scroll focus or add a “return true”, because the entire screen is being scrolled, correct?

Thanks again for the elegant solution! [import]uid: 12397 topic_id: 12411 reply_id: 46134[/import]

Final dumb question, I assume I don’t need to set the scroll focus or add a “return true”, because the entire screen is being scrolled, correct?
yup 100% correct

and one more thing none of your question are dumb nor stupid
:slight_smile: [import]uid: 12482 topic_id: 12411 reply_id: 46173[/import]

@ hgvyas123,

Stumped Again…

I added two small rectangles to a display group, which I then looped through to enable scrolling. This appears to work, but I’m stuck on the following…

  1. Individually wrap the rectangles as they each scroll out of view
  2. Assign a different velocity to the display group (parallax effect)

When you get a chance, could you please test the following code and point me in the right direction? Here is what I have so far…

display.setStatusBar(display.HiddenStatusBar) -- Hide status bar  
   
layer1 = display.newGroup()  
layer2 = display.newGroup()  
local bg1 = display.newRect(0, 0, 1024, 768)  
local bg2 = display.newRect(-1024, 0, 1024, 768)  
local bg3 = display.newRect(550, 420, 100, 100)  
local bg4 = display.newRect(700, 250, 100, 100)  
bg1:setFillColor(40, 91, 142)  
bg2:setFillColor(0, 153, 102)  
bg3:setFillColor(30, 75, 50)  
bg4:setFillColor(200, 175, 250)  
layer1:insert(bg1)  
layer1:insert(bg2)  
layer2:insert(bg3)  
layer2:insert(bg4)  
   
-- Set up variables  
local previousX = 0  
local scrollVelocity = 0  
   
local function flickScroll() -- Additional velocity increase  
 bg1.x = bg1.x + scrollVelocity -- Start scroll  
 bg2.x = bg2.x + scrollVelocity  
 for i = 1, layer2.numChildren do layer2[i].x = layer2[i].x + scrollVelocity end  
 -- Wrap screen contents from one side to the other as they fall off the stage   
 if bg1.x \>= 1024 + 512 then   
 bg1.x = bg2.x - 1024  
 end  
 if bg2.x \>= 1024 + 512 then   
 bg2.x = bg1.x - 1024  
 end   
 if bg1.x \<= -512 then  
 bg1.x = bg2.x + 1024  
 end  
 if bg2.x \<= -512 then   
 bg2.x = bg1.x + 1024  
 end  
 for i = 1, layer2.numChildren do  
 if layer2[i].x \>= 1024 + 512 then  
 layer2[i].x = layer2[i].x - 1024  
 end  
 end  
 for i = 1, layer2.numChildren do  
 if layer2[i].x \<= -512 then  
 layer2[i].x = layer2[i].x + 1024   
 end  
 end  
 for i = 1, layer2.numChildren do print("layer2.x: "..layer2[i].x) end  
 scrollVelocity = scrollVelocity \* 0.95 -- Alter velocity via scrollVelocity @ makeScroll()   
 if scrollVelocity \< 0.1 and scrollVelocity \> -0.1 then  
 scrollVelocity = 0  
 Runtime:removeEventListener("enterFrame", flickScroll)   
 end  
end  
  
local function makeScroll(e)  
 if "began" == e.phase then  
 x1 = e.x - bg1.x  
 x2 = e.x - bg2.x  
 x3 = e.x - layer2.x   
 scrollVelocity = 0  
 elseif "moved" == e.phase then  
 if (previousX == 0) then  
 previousX = e.x  
 end   
 scrollVelocity = (e.x - previousX) \* 0.1 -- Increase multiplier to increase velocity  
 bg1.x = e.x - x1  
 bg2.x = e.x - x2  
 layer2.x = e.x - x3  
 -- Wrap screen contents from one side to the other as they fall off the stage   
 if bg1.x \>= 1024 + 512 then   
 bg1.x = bg2.x - 1024  
 x1 = e.x - bg1.x  
 end  
 if bg2.x \>= 1024 + 512 then   
 bg2.x = bg1.x - 1024  
 x2 = e.x - bg2.x  
 end   
 if bg1.x \<= -512 then  
 bg1.x = bg2.x + 1024  
 x1 = e.x - bg1.x   
 end  
 if bg2.x \<= -512 then   
 bg2.x = bg1.x + 1024  
 x2 = e.x - bg2.x  
 end  
 for i = 1, layer2.numChildren do  
 if layer2[i].x \>= 1024 + 512 then  
 layer2[i].x = layer2[i].x - 1024  
 x3 = e.x - layer2.x  
 end  
 end  
 for i = 1, layer2.numChildren do  
 if layer2[i].x \<= -512 then  
 layer2[i].x = layer2[i].x + 1024  
 x3 = e.x - layer2.x  
 end  
 end  
 elseif "ended" == e.phase then  
 Runtime:addEventListener("enterFrame", flickScroll)  
 previousX = 0   
 end  
end  
Runtime:addEventListener("touch", makeScroll)  

Any Thoughts? Thanks so much! [import]uid: 12397 topic_id: 12411 reply_id: 46382[/import]

are you looking for something like this ??

http://www.youtube.com/watch?v=kGndaktrLcw [import]uid: 12482 topic_id: 12411 reply_id: 46389[/import]

Yes similar, but without a main character (point of view instead). Also, the layers would wrap to the other side of the screen when falling out to view, to give the illusion of endless movement… just like your code already does.

Ultimately, I’m looking to have multiple display groups, each with different velocities, each with multiple spawned objects. Some of these objects will be small, while others will fill the entire screen (1024 x 768). I’d like to be able to drag scroll from landscape mode in either direction, with objects wrapping to the other side as they each fall out of view. Concept is a little closer to the horse animation sample @ http://developer.anscamobile.com/content/horse-animation, only ipad-sized and drag-scrollable.

I’d like to use your lean code as the base, but can’t figure out how to assign a separate velocity to the display group I added, nor how to wrap its children as they individually fall out of view. Any help you can offer would be greatly appreciated. [import]uid: 12397 topic_id: 12411 reply_id: 46451[/import]

[lua]display.setStatusBar(display.HiddenStatusBar) – Hide status bar
local bg1 = display.newRect(0, 0, 1024, 768)
local bg2 = display.newRect(-1024, 0, 1024, 768)
bg1:setFillColor(40, 91, 142)
bg2:setFillColor(0, 153, 102)
local bg3 = display.newRect(150,100,100,100)
local bg4 = display.newRect(370,200,100,100)
local bg5 = display.newRect(0,100,100,100)
local bg6 = display.newRect(924,200,100,100)
local localGroup = display.newGroup()
localGroup:insert(bg3)
localGroup:insert(bg4)
localGroup:insert(bg5)
localGroup:insert(bg6)
–localGroup:setReferencePoint(display.CenterReferencePoint);
– Set up variables
local previousX = 0
local scrollVelocity = 0

local function flickScroll() – used to add on velocity after touch stopped
bg1.x = bg1.x + scrollVelocity – make the move
bg2.x = bg2.x + scrollVelocity
if bg1.x >= 1024 + 512 then
bg1.x = bg2.x - 1024
end
if bg2.x >= 1024 + 512 then
bg2.x = bg1.x - 1024
end
if bg1.x <= -512 then
bg1.x = bg2.x + 1024
end
if bg2.x <= -512 then
bg2.x = bg1.x + 1024
end
scrollVelocity = scrollVelocity * 0.95
localGroup.x = localGroup.x + scrollVelocity * 0.2
if localGroup.x < - 1024 then
localGroup.x = 1024
end
if localGroup.x > 1024 then
localGroup.x = -1024
end
if scrollVelocity < 0.1 and scrollVelocity > -0.1 then
scrollVelocity = 0
end
end

local flag =false
local function makeScroll(e)
if “began” == e.phase then
x1 = e.x - bg1.x
x2 = e.x - bg2.x
x3 = e.x - localGroup.x
scrollVelocity = 0
print(localGroup.x)
elseif “moved” == e.phase then

scrollVelocity = e.x - previousX

bg1.x = e.x - x1
bg2.x = e.x - x2
print(scrollVelocity)
print(localGroup.x)
if flag then
localGroup.x = localGroup.x + scrollVelocity * 0.2
if localGroup.x < - 1024 then
localGroup.x = 1024
end
if localGroup.x > 1024 then
localGroup.x = -1024
end
end
flag = true
previousX = e.x
if bg1.x >= 1024 + 512 then
bg1.x = bg2.x - 1024
x1 = e.x - bg1.x
end
if bg2.x >= 1024 + 512 then
bg2.x = bg1.x - 1024
x2 = e.x - bg2.x
end
if bg1.x <= -512 then
bg1.x = bg2.x + 1024
x1 = e.x - bg1.x
end
if bg2.x <= -512 then
bg2.x = bg1.x + 1024
x2 = e.x - bg2.x
end
elseif “ended” == e.phase then
Runtime:addEventListener(“enterFrame”, flickScroll)
previousX = 0
flag = false
end
end
Runtime:addEventListener(“touch”, makeScroll)[/lua]
also note in previous examples theres a one mistake with
if (previousX == 0) then
previousX = e.x
end

previousX is never changing after 1st time that’s the reason why its a too fast [import]uid: 12482 topic_id: 12411 reply_id: 46578[/import]

@ hgvyas123,

Thanks so much, your solutions have really helped me in getting past the mental blocks I was experiencing. I was able to adapt your code to meet the needs of my current project. I’ll post the test code that I ended up with to illustrate what I was envisioning, but before I do I’d like to ask for your thoughts on the following:

  1. When dragging the screen (before release), all display objects scroll at uniform velocity, despite their assigned unique values. I tried copying the velocities assigned under the flickScroll function to makeScroll, under “began” == e.phase, but it did not address the issue. Any ideas?

  2. Should there be a Runtime:removeEventListener(“enterFrame”, flickScroll) included under the flickScroll function, for when the scrolling stops? Without it, a print statement on any object’s x value will continue on without stopping in the terminal, even after the scrolling appears to stop in the simulator.

  3. What is the purpose of the boolean (flag) included under the makeScroll function in your last example? Just remnants from testing, or am I missing something bigger?

  4. If you see any silliness in my logic, please let me know.

Best Regards!

[code]
display.setStatusBar(display.HiddenStatusBar) – Hide status bar

– set variables
local previousX = 0
local scrollVelocity = 0
local x1a
local x1b
local x2a
local x2b
local x3a
local x3b
local x4a
local x4b
local x5a
local x5b
local mRandKludge1 = math.randomseed(os.time()) --seed random number generator
local mRandKludge2 = math.random(0, 255) – ensure unique starting number
local mRand = math.random
local object
local objectCount = mRand(5, 20)
local n = 0
local i

– create display groups
local bgAll = display.newGroup() – all background images
local layer2 = display.newGroup() --spawned objects furthest away
local layer3 = display.newGroup() --spawned objects further away
local layer4 = display.newGroup() --spawned objects closest

– create shapes
local bg1a = display.newRect(0, 0, 1024, 120)
local bg1b = display.newRect(-1024, 0, 1024, 120)
local bg2a = display.newRect(0, 120, 1024, 130)
local bg2b = display.newRect(-1024, 120, 1024, 130)
local bg3a = display.newRect(0, 250, 1024, 200)
local bg3b = display.newRect(-1024, 250, 1024, 200)
local bg4a = display.newRect(0, 450, 1024, 200)
local bg4b = display.newRect(-1024, 450, 1024, 200)
local bg5a = display.newRect(0, 650, 1024, 118)
local bg5b = display.newRect(-1024, 650, 1024, 118)

– assign random colors to shapes
bg1a:setFillColor(mRand(0, 255), mRand(0, 255), mRand(0, 255))
bg1b:setFillColor(mRand(0, 255), mRand(0, 255), mRand(0, 255))
bg2a:setFillColor(mRand(0, 255), mRand(0, 255), mRand(0, 255))
bg2b:setFillColor(mRand(0, 255), mRand(0, 255), mRand(0, 255))
bg3a:setFillColor(mRand(0, 255), mRand(0, 255), mRand(0, 255))
bg3b:setFillColor(mRand(0, 255), mRand(0, 255), mRand(0, 255))
bg4a:setFillColor(mRand(0, 255), mRand(0, 255), mRand(0, 255))
bg4b:setFillColor(mRand(0, 255), mRand(0, 255), mRand(0, 255))
bg5a:setFillColor(mRand(0, 255), mRand(0, 255), mRand(0, 255))
bg5b:setFillColor(mRand(0, 255), mRand(0, 255), mRand(0, 255))

– insert shapes into groups
bgAll:insert(bg1a)
bgAll:insert(bg1b)
bgAll:insert(bg2a)
bgAll:insert(bg2b)
bgAll:insert(bg3a)
bgAll:insert(bg3b)
bgAll:insert(bg4a)
bgAll:insert(bg4b)
bgAll:insert(bg5a)
bgAll:insert(bg5b)

– create tables to hold calculated distances for spawned objects
local x2Var = {}
local x3Var = {}
local x4Var = {}

local function flickScroll() – tack on additional velocity to drag when flicked
scrollVelocity = scrollVelocity * 0.95 – base velocity
if scrollVelocity < 0.1 and scrollVelocity > -0.1 then
scrollVelocity = 0
Runtime:removeEventListener(“enterFrame”, flickScroll)
end
– calculate distance and velocity to scroll, then start
bg1a.x = bg1a.x + scrollVelocity * 0.05
bg1b.x = bg1b.x + scrollVelocity * 0.05
bg2a.x = bg2a.x + scrollVelocity * 0.2
bg2b.x = bg2b.x + scrollVelocity * 0.2
bg3a.x = bg3a.x + scrollVelocity * 0.4
bg3b.x = bg3b.x + scrollVelocity * 0.4
bg4a.x = bg4a.x + scrollVelocity * 0.6
bg4b.x = bg4b.x + scrollVelocity * 0.6
bg5a.x = bg5a.x + scrollVelocity * 0.8
bg5b.x = bg5b.x + scrollVelocity * 0.8
for i = 1, layer2.numChildren do layer2[i].x = layer2[i].x + scrollVelocity * 0.2 end
for i = 1, layer3.numChildren do layer3[i].x = layer3[i].x + scrollVelocity * 0.4 end
for i = 1, layer4.numChildren do layer4[i].x = layer4[i].x + scrollVelocity * 0.6 end

– wrap screen contents during flick
if bg1a.x >= 1536 then bg1a.x = bg1a.x - 2048 end
if bg1b.x >= 1536 then bg1b.x = bg1b.x - 2048 end
if bg1a.x <= -512 then bg1a.x = bg1a.x + 2048 end
if bg1b.x <= -512 then bg1b.x = bg1b.x + 2048 end
if bg2a.x >= 1536 then bg2a.x = bg2a.x - 2048 end
if bg2b.x >= 1536 then bg2b.x = bg2b.x - 2048 end
if bg2a.x <= -512 then bg2a.x = bg2a.x + 2048 end
if bg2b.x <= -512 then bg2b.x = bg2b.x + 2048 end
if bg3a.x >= 1536 then bg3a.x = bg3a.x - 2048 end
if bg3b.x >= 1536 then bg3b.x = bg3b.x - 2048 end
if bg3a.x <= -512 then bg3a.x = bg3a.x + 2048 end
if bg3b.x <= -512 then bg3b.x = bg3b.x + 2048 end
if bg4a.x >= 1536 then bg4a.x = bg4a.x - 2048 end
if bg4b.x >= 1536 then bg4b.x = bg4b.x - 2048 end
if bg4a.x <= -512 then bg4a.x = bg4a.x + 2048 end
if bg4b.x <= -512 then bg4b.x = bg4b.x + 2048 end
if bg5a.x >= 1536 then bg5a.x = bg5a.x - 2048 end
if bg5b.x >= 1536 then bg5b.x = bg5b.x - 2048 end
if bg5a.x <= -512 then bg5a.x = bg5a.x + 2048 end
if bg5b.x <= -512 then bg5b.x = bg5b.x + 2048 end
for i = 1, layer2.numChildren do
if layer2[i].x < 0 then layer2[i].x = layer2[i].x + 2048 end
if layer2[i].x > 1024 then layer2[i].x = layer2[i].x - 2048 end
end
for i = 1, layer3.numChildren do
if layer3[i].x < 0 then layer3[i].x = layer3[i].x + 2048 end
if layer3[i].x > 1024 then layer3[i].x = layer3[i].x - 2048 end
end
for i = 1, layer4.numChildren do
if layer4[i].x < 0 then layer4[i].x = layer4[i].x + 2048 end
if layer4[i].x > 1024 then layer4[i].x = layer4[i].x - 2048 end
end
end

local function makeScroll(e)
local target = e.target
local phase = e.phase
if “began” == e.phase then
scrollVelocity = 0
– calculate distance between initial touch and current position & assign value
x1a = e.x - bg1a.x
x1b = e.x - bg1b.x
x2a = e.x - bg2a.x
x2b = e.x - bg2b.x
x3a = e.x - bg3a.x
x3b = e.x - bg3b.x
x4a = e.x - bg4a.x
x4b = e.x - bg4b.x
x5a = e.x - bg5a.x
x5b = e.x - bg5b.x
for i = 1, layer2.numChildren do
x2Var[i] = e.x - layer2[i].x
end
for i = 1, layer3.numChildren do
x3Var[i] = e.x - layer3[i].x
end
for i = 1, layer4.numChildren do
x4Var[i] = e.x - layer4[i].x
end
elseif “moved” == e.phase then
scrollVelocity = e.x - previousX
– calculate distance
bg1a.x = e.x - x1a
bg1b.x = e.x - x1b
bg2a.x = e.x - x2a
bg2b.x = e.x - x2b
bg3a.x = e.x - x3a
bg3b.x = e.x - x3b
bg4a.x = e.x - x4a
bg4b.x = e.x - x4b
bg5a.x = e.x - x5a
bg5b.x = e.x - x5b
for i = 1, layer2.numChildren do
layer2[i].x = e.x - x2Var[i]
end
for i = 1, layer3.numChildren do
layer3[i].x = e.x - x3Var[i]
end
for i = 1, layer4.numChildren do
layer4[i].x = e.x - x4Var[i]
end
previousX = e.x

– wrap screen contents during drag
if bg1a.x >= 1536 then
bg1a.x = bg1a.x - 2048
x1a = e.x - bg1a.x
end
if bg1b.x >= 1536 then
bg1b.x = bg1b.x - 2048
x1b = e.x - bg1b.x
end
if bg1a.x <= -512 then
bg1a.x = bg1a.x + 2048
x1a = e.x - bg1a.x
end
if bg1b.x <= -512 then
bg1b.x = bg1b.x + 2048
x1b = e.x - bg1b.x
end
if bg2a.x >= 1536 then
bg2a.x = bg2a.x - 2048
x2a = e.x - bg2a.x
end
if bg2b.x >= 1536 then
bg2b.x = bg2b.x - 2048
x2b = e.x - bg2b.x
end
if bg2a.x <= -512 then
bg2a.x = bg2a.x + 2048
x2a = e.x - bg2a.x
end
if bg2b.x <= -512 then
bg2b.x = bg2b.x + 2048
x2b = e.x - bg2b.x
end
if bg3a.x >= 1536 then
bg3a.x = bg3a.x - 2048
x3a = e.x - bg3a.x
end
if bg3b.x >= 1536 then
bg3b.x = bg3b.x - 2048
x3b = e.x - bg3b.x
end
if bg3a.x <= -512 then
bg3a.x = bg3a.x + 2048
x3a = e.x - bg3a.x
end
if bg3b.x <= -512 then
bg3b.x = bg3b.x + 2048
x3b = e.x - bg3b.x
end
if bg4a.x >= 1536 then
bg4a.x = bg4a.x - 2048
x4a = e.x - bg4a.x
end
if bg4b.x >= 1536 then
bg4b.x = bg4b.x - 2048
x4b = e.x - bg4b.x
end
if bg4a.x <= -512 then
bg4a.x = bg4a.x + 2048
x4a = e.x - bg4a.x
end
if bg4b.x <= -512 then
bg4b.x = bg4b.x + 2048
x4b = e.x - bg4b.x
end
if bg5a.x >= 1536 then
bg5a.x = bg5a.x - 2048
x5a = e.x - bg5a.x
end
if bg5b.x >= 1536 then
bg5b.x = bg5b.x - 2048
x5b = e.x - bg5b.x
end
if bg5a.x <= -512 then
bg5a.x = bg5a.x + 2048
x5a = e.x - bg5a.x
end
if bg5b.x <= -512 then
bg5b.x = bg5b.x + 2048
x5b = e.x - bg5b.x
end
for i = 1, layer2.numChildren do
if layer2[i].x < 0 then layer2[i].x = layer2[i].x +2048 end
if layer2[i].x > 1024 then layer2[i].x = layer2[i].x - 2048 end
end
for i = 1, layer3.numChildren do
if layer3[i].x < 0 then layer3[i].x = layer3[i].x +2048 end
if layer3[i].x > 1024 then layer3[i].x = layer3[i].x - 2048 end
end
for i = 1, layer4.numChildren do
if layer4[i].x < 0 then layer4[i].x = layer4[i].x +2048 end
if layer4[i].x > 1024 then layer4[i].x = layer4[i].x - 2048 end
end
elseif “ended” == e.phase then
Runtime:addEventListener(“enterFrame”, flickScroll)
previousX = 0
end
end
Runtime:addEventListener(“touch”, makeScroll)

local function objectTap(e)
– tap actions to go here
end

local function objectAttributes()
object.x = mRand(-1024, 1024) – random horizontal positioning (both off/on screen)
object.y = mRand(150, 600) – random vertical positioning within bounds
if object.y >= 150 and object.y < 250 then – scale to help with impression of depth
object:scale(.35, .35)
layer2:insert(object)
elseif object.y >= 250 and object.y < 450 then
object:scale(.55, .55)
layer3:insert(object)
elseif object.y >= 450 and object.y < 600 then
layer4:insert(object)
end
object:setFillColor(mRand(0, 255), mRand(0, 255), mRand(0, 255))
end

local function objectSpawn()
object = display.newRect(0, 0, 100, 100)
object.id = n
n = n + 1
object:addEventListener(“tap”, objectTap)
objectAttributes()
end
for i = 1, objectCount do objectSpawn() end
[/code] [import]uid: 12397 topic_id: 12411 reply_id: 47125[/import]

lots of questions!!! :slight_smile:

ok answers

  1. can’t understand the question i think in my code that is working right

  2. Should there be a Runtime:removeEventListener(“enterFrame”, flickScroll) included yes it should be included i had just forgot to include that (every time !!! :frowning: )

  3. What is the purpose of the boolean (flag) included under the makeScroll

just to prevent this line

localGroup.x = localGroup.x + scrollVelocity * 0.2

from calling very first time as it is crating some problems if you can find solution for that you can remove it else it will work as a good fix for that you can chk the problem by setting flag = true every time ( you can also say it my laziness for not finding proper solution and making fix for that)

  1. really good
    :slight_smile:
    [import]uid: 12482 topic_id: 12411 reply_id: 47134[/import]

@hgvyas123, et al:

In my example, I ended up removing that line of code under makeScroll, as it did not appear to be needed. I did set the flag instances to true on your example to see the glitch you were seeing… weird, but a moot point now I guess.

I did indeed notice that your example drags correctly with the two different velocities, as opposed to mine… here’s a screenshot of the issue during drag (before release) to help illustrate.

http://dl.dropbox.com/u/20882589/Corona/drag_issue.jpg

Any thoughts? [import]uid: 12397 topic_id: 12411 reply_id: 47207[/import]

i think you had not noticed this line in “moved” phase
localGroup.x = localGroup.x + scrollVelocity * 0.2
:slight_smile: [import]uid: 12482 topic_id: 12411 reply_id: 47247[/import]

Thanks to your hint I was able to figure out that dragging issue. Now, I think I may have introduced a different glitch that intermittently appears before releasing the drag. There’s a jump in x position… maybe 1024 once every four or five drags, e.g. bg1a swaps places with bg1b. I figured it’s a wrapping issue and tried commenting out the respective code in both flickScroll() and makeScroll(), but that does not appear to fix the problem, so I’m guessing it may have something to do with the calculation of e.x.

I threw in some display.newTexts to help show the different x positions being updated. Any thoughts?

[code]
display.setStatusBar(display.HiddenStatusBar)

local previousX = 0
local scrollVelocity = 0

local bg1a = display.newRect(0, 0, 1024, 384)
local bg1b = display.newRect(-1024, 0, 1024, 384)
local bg2a = display.newRect(0, 384, 1024, 384)
local bg2b = display.newRect(-1024, 384, 1024, 384)
bg1a:setFillColor(55, 100, 250)
bg1b:setFillColor(95, 25, 155)
bg2a:setFillColor(233, 66, 90)
bg2b:setFillColor(259, 89, 89)

local function positionXDisplayUpdate()
bg1aText.text = “bg1a.x: “…string.format(”%.f”,bg1a.x)
bg1bText.text = “bg1b.x: “…string.format(”%.f”,bg1b.x)
bg2aText.text = “bg2a.x: “…string.format(”%.f”,bg2a.x)
bg2bText.text = “bg2b.x: “…string.format(”%.f”,bg2b.x)
bg1aText:setReferencePoint(display.CenterLeftReferencePoint)
bg1bText:setReferencePoint(display.CenterLeftReferencePoint)
bg2aText:setReferencePoint(display.CenterLeftReferencePoint)
bg2bText:setReferencePoint(display.CenterLeftReferencePoint)
bg1aText.x = 450
bg1bText.x = 450
bg2aText.x = 450
bg2bText.x = 450
end

local function positionXDisplay()
bg1aText = display.newText("bg1a.x: "…bg1a.x, 450, 150, “Helvetica”, 60)
bg1bText = display.newText("bg1b.x: "…bg1b.x, 450, 200, “Helvetica”, 60)
bg2aText = display.newText("bg2a.x: "…bg2a.x, 450, 500, “Helvetica”, 60)
bg2bText = display.newText("bg2b.x: "…bg2b.x, 450, 550, “Helvetica”, 60)
end
positionXDisplay()

local function flickScroll()
bg1a.x = bg1a.x + scrollVelocity
bg1b.x = bg1b.x + scrollVelocity
bg2a.x = bg2a.x + scrollVelocity
bg2b.x = bg2b.x + scrollVelocity
if bg1a.x >= 1536 then bg1a.x = bg1b.x - 1024 end
if bg1a.x <= -512 then bg1a.x = bg1b.x + 1024 end
if bg1b.x >= 1536 then bg1b.x = bg1a.x - 1024 end
if bg1b.x <= -512 then bg1b.x = bg1a.x + 1024 end
if bg2a.x >= 1536 then bg2a.x = bg2b.x - 1024 end
if bg2a.x <= -512 then bg2a.x = bg2b.x + 1024 end
if bg2b.x >= 1536 then bg2b.x = bg2a.x - 1024 end
if bg2b.x <= -512 then bg2b.x = bg2a.x + 1024 end
positionXDisplayUpdate()
scrollVelocity = scrollVelocity * 0.95
if scrollVelocity < 0.1 and scrollVelocity > -0.1 then
scrollVelocity = 0
Runtime:removeEventListener(“enterFrame”, flickScroll)
end
end

local flag = false
local function makeScroll(e)
if “began” == e.phase then
x1a = e.x - bg1a.x
x1b = e.x - bg1b.x
x2a = e.x - bg2a.x
x2b = e.x - bg2b.x
scrollVelocity = 0
elseif “moved” == e.phase then
scrollVelocity = e.x - previousX
if flag == true then
bg1a.x = bg1a.x + scrollVelocity * 0.05
bg1b.x = bg1b.x + scrollVelocity * 0.05
bg2a.x = bg2a.x + scrollVelocity * 0.8
bg2b.x = bg2b.x + scrollVelocity * 0.8
if bg1a.x >= 1536 then bg1a.x = bg1b.x - 1024 x1a = e.x - bg1a.x end
if bg1a.x <= -512 then bg1a.x = bg1b.x + 1024 x1a = e.x - bg1a.x end
if bg1b.x >= 1536 then bg1b.x = bg1a.x - 1024 x1b = e.x - bg1b.x end
if bg1b.x <= -512 then bg1b.x = bg1a.x + 1024 x1b = e.x - bg1b.x end
if bg2a.x >= 1536 then bg2a.x = bg2b.x - 1024 x2a = e.x - bg2a.x end
if bg2a.x <= -512 then bg2a.x = bg2b.x + 1024 x2a = e.x - bg2a.x end
if bg2b.x >= 1536 then bg2b.x = bg2a.x - 1024 x2b = e.x - bg2b.x end
if bg2b.x <= -512 then bg2b.x = bg2a.x + 1024 x2b = e.x - bg2b.x end
positionXDisplayUpdate()
end
flag = true
previousX = e.x
elseif “ended” == e.phase then
Runtime:addEventListener(“enterFrame”, flickScroll)
flag = false
previousX = 0
end
end
Runtime:addEventListener(“touch”, makeScroll)
[/code] [import]uid: 12397 topic_id: 12411 reply_id: 47409[/import]

seems like you are at wrong way now previously you are at right
replace ur code with below at proper place
[lua]bg1a.x = bg1a.x + scrollVelocity * 0.8
bg1b.x = bg1b.x + scrollVelocity * 0.8
bg2a.x = bg2a.x + scrollVelocity * 0.4
bg2b.x = bg2b.x + scrollVelocity * 0.4[/lua]
if you find it is hard then might be this can help i had not chked it but may be it is useful
http://developer.anscamobile.com/code/parallax-class
:slight_smile: [import]uid: 12482 topic_id: 12411 reply_id: 47414[/import]