Hi,
Here is an update. things are kind of working. However there are anomalies that I cannot resolve. Is there a way of watching the variables?
You may put the following code in main.lua in the Jungle Scene example to check out the dragging in both directions. Any help is appreciated.
-dd
– Abstract: JungleScene sample project
require “sprite”
display.setStatusBar( display.HiddenStatusBar )
– The sky as background
local sky = display.newImage( “sky.jpg” )
– Create master display group (for global “camera” dragging effect)
local game = display.newGroup();
game.x = 0
local baseline = 280
– a bunch of foliage
local tree = {}
tree[1] = display.newImage( “Palm-arecaceae.png” )
tree[1].xScale = 0.7; tree[1].yScale = 0.7
tree[1]:setReferencePoint( display.BottomCenterReferencePoint )
tree[1].x = 40; tree[1].y = baseline
tree[1].dx = 0.1
game:insert( tree[1] )
tree[2] = display.newImage( “Greenhouse-Palm-jubaea01.png” )
tree[2].xScale = 0.6; tree[2].yScale = 0.6
tree[2]:setReferencePoint( display.BottomCenterReferencePoint )
tree[2].x = 120; tree[2].y = baseline
tree[2].dx = 0.2
game:insert( tree[2] )
tree[3] = display.newImage( “Greenhouse-Palm-cycas01.png” )
tree[3].xScale = 0.8; tree[3].yScale = 0.8
tree[3]:setReferencePoint( display.BottomCenterReferencePoint )
tree[3].x = 200; tree[3].y = baseline
tree[3].dx = 0.3
game:insert( tree[3] )
tree[4] = display.newImage( “Ginger.png” )
tree[4].xScale = 0.7; tree[4].yScale = 0.7
tree[4]:setReferencePoint( display.BottomCenterReferencePoint )
tree[4].x = baseline; tree[4].y = baseline
tree[4].dx = 0.4
game:insert( tree[4] )
tree[5] = display.newImage( “Greenhouse-Palm-acai01.png” )
tree[5].xScale = 0.8; tree[5].yScale = 0.8
tree[5]:setReferencePoint( display.BottomCenterReferencePoint )
tree[5].x = 300; tree[5].y = baseline
tree[5].dx = 0.5
game:insert( tree[5] )
tree[6] = display.newImage( “Dracena.png” )
tree[6].xScale = 0.4; tree[5].yScale = 0.4
tree[6]:setReferencePoint( display.BottomCenterReferencePoint )
tree[6].x = 320; tree[6].y = baseline
tree[6].dx = 0.6
game:insert( tree[6] )
tree[7] = display.newImage( “Banana.png” )
tree[7].xScale = 0.4; tree[7].yScale = 0.4
tree[7]:setReferencePoint( display.BottomCenterReferencePoint )
tree[7].x = 380; tree[7].y = baseline
tree[7].dx = 0.7
game:insert( tree[7] )
tree[8] = display.newImage( “Bamboo-rgba.png” )
tree[8].xScale = 0.8; tree[8].yScale = 0.8
tree[8]:setReferencePoint( display.BottomCenterReferencePoint )
tree[8].x = 420; tree[8].y = baseline
tree[8].dx = 0.8
game:insert( tree[8] )
– Grass
– This is doubled so we can slide it
– When one of the grass images slides offscreen, we move it all the way to the right of the next one.
local grass = display.newImage( “grass.png” )
grass:setReferencePoint( display.CenterLeftReferencePoint )
grass.x = 0
grass.y = baseline - 20
game:insert( grass )
local grass2 = display.newImage( “grass.png” )
grass2:setReferencePoint( display.CenterLeftReferencePoint )
grass2.x = 480
grass2.y = baseline - 20
game:insert( grass2 )
– solid ground, doesn’t need to move
local ground = display.newRect( 0, baseline, 480, 90 )
ground:setFillColor( 0x31, 0x5a, 0x18 )
– A basic function for dragging objects
local function startDrag( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
elseif t.isFocus then
if “moved” == phase then
game.x = event.x - t.x0
local tDelta = event.x - t.x0
local xOffset = ( 0.2 * tDelta )
grass.x = grass.x - xOffset
grass2.x = grass2.x - xOffset
if (grass.x + grass.stageWidth) < 0 then
grass:translate( 480 * 2, 0)
elseif
(grass.x + grass.stageWidth) > 480 then
grass:translate( -480 * 2, 0)
end
if (grass2.x + grass2.stageWidth) < 0 then
grass2:translate( 480 * 2, 0)
elseif
(grass2.x + grass.stageWidth) > 480 then
grass2:translate( -480 * 2, 0)
end
local i
for i = 1, #tree, 1 do
tree[i].x = tree[i].x - tree[i].dx * tDelta * 0.2
if (tree[i].x + tree[i].stageWidth) < 0 then
tree[i]:translate( 480 + tree[i].stageWidth * 2, 0 )
else
if (tree[i].x + tree[i].stageWidth) > 480 then
tree[i]:translate( -480 + tree[i].stageWidth * 2, 0 )
end
end
end
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
– Stop further propagation of touch event!
return true
end
local function moveCamera()
if (boulder.x > 80 and boulder.x < 1800) then
game.x = -boulder.x + 80
end
end
– Start the drag
game:addEventListener( “touch”, startDrag ) [import]uid: 1908 topic_id: 2842 reply_id: 8516[/import]