@ 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:
-
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?
-
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.
-
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?
-
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]