How do you manipulate spawned objects?

One last thing, now that I’ve inserted this code into my project, I now need to trigger the moveCamera function with a conditional statement rather than with the timer:

local function wrap (event)if player.y < 50 thenplayer.y = 50 endendRuntime:addEventListener("enterFrame", wrap)[/code]Basically, I want the moveCamera function to fire when my "player" reaches the top of the screen.I have a feeling this should be easy, but I just can't seem to figure out how or where I should change the code. I posted this question earlier in a new thread so as not to trouble u with newbie questions, but haven't recieved any responses. Any suggestions would be much appreciated.Thank you,Steven [import]uid: 79394 topic_id: 15294 reply_id: 56584[/import]

Oh… sorry I din’t see your messages… but good to know that you got help… so that was what you were trying to achieve… I was just wondering what it is supposed to be with traffic ? was it some cyclone or something ? LOL

he camera move when the player reach the top its better to give the condition inside the player movement code. how do you move the player ? is it on button press or when u click the screen. any way u can do the checking in the code where you move the player.
just call the function like below
[lua]if player.y < 50 then
player.y = 50
moveCamera()
end[/lua]

and remove the timer which calls moveCamera.

hope this helps.
[import]uid: 71210 topic_id: 15294 reply_id: 56588[/import]

Hi Renjith,

I’d like to show you my current code and let you take a look at where I’ve currently got everything placed. If I add the conditional to the player movement function, the moveCamera function hasn’t been defined yet, and it’s local to the main() function further down. I know I can rearrange the chunks to get things working, but I don’t know what the best way of organizing things is:

local physics = require("physics")physics.start()physics.setGravity(0, 0)--physics.setDrawMode("hybrid")physics.setDrawMode("normal") local background=display.newImage("Street (first version).png")background:setReferencePoint(display.TopLeftReferencePoint)background.x=0background.y=0local background2=display.newImage("Street (first version).png")background2:setReferencePoint(display.TopLeftReferencePoint)background2.x=0background2.y=-1024local background3=display.newImage("Street (first version).png")background3:setReferencePoint(display.TopLeftReferencePoint)background3.x=0background3.y=-1024local loqsprite = require('loq_sprite')local playerFactory = loqsprite.newFactory('playerSheet')local spriteFactory = loqsprite.newFactory('sheet')local angelFactory = loqsprite.newFactory('angelSheet')local player = playerFactory:newSpriteGroup()atrace(xinspect(player:getSpriteNames()))local prevx = 0local prevy = 0local dragDirectionlocal string_dir = ''local flaglocal flag2local flag3local flag4local moved = 0local counttracker = 0player.x = 385player.y = 970player:addPhysics(physics,'kinematic',{})physics.addBody(player,"kinematic")player:play('Player static back view')-- text to display directionlocal directionTXT = display.newText("Direction: ",130,25,nil,25)directionTXT:setTextColor( 255,0,0 )--Player movement functionlocal function onTouch( event ) local player = event.target local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = player.parent parent:insert( player ) display.getCurrentStage():setFocus( player ) -- Spurious events can be sent to the target, e.g. the user presses -- elsewhere on the screen and then moves the finger over the target. -- To prevent this, we add this flag. Only when it's true will "move" -- events be sent to the target. player.isFocus = true -- Store initial position player.x0 = event.x - player.x player.y0 = event.y - player.y prevx = event.x prevy = event.y elseif player.isFocus then if "moved" == phase then -- Make object move (we subtract t.x0,t.y0 so that moves are -- relative to initial grab point, rather than object "snapping"). player.x = event.x - player.x0 player.y = event.y - player.y0 local sqrt = math.sqrt local dx = prevx - event.x local dy = prevy - event.y local distance = sqrt(dx * dx + dy * dy); if distance>10 then flag="off" local angle = math.atan2(dy,dx) * 57.2957795 if angle>=45*-1 and angle<=45 then string_dir="Left" if dragDirection ~= "left" then player:play('Player run side view left') dragDirection = "left" end elseif angle>45 and angle<135 then string_dir="Up" if dragDirection ~= "up" then player:play('Player run back view') dragDirection = "up" end elseif angle>135*-1 and angle<45*-1 then string_dir="Down" if dragDirection ~= "down" then player:play('Player run front view') dragDirection = "down" end elseif angle>=135 or angle<=135*-1 then string_dir="Right" if dragDirection ~= "right" then player:play('Player run side view right') dragDirection = "right" end end prevx=event.x prevy=event.y directionTXT.text ="Direction : "..string_dir moved = moved + 1 counttracker = 0 end elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) if string_dir == "Left" then player:play('Player static left side view') elseif string_dir == "Up" then player:play('Player static back view') elseif string_dir == "Down" then player:play('Player static front view') elseif string_dir == "Right" then player:play('Player static right side view') end dragDirection = "stopped" flag = "on" end -- Important to return true. This tells the system that the event -- should not be propagated to listeners of any objects underneath. return true endendlocal function movementTracker(event) if moved == 0 then if flag ~= "on" then if counttracker == 3 then print("not moving and last movement was towards "..string_dir) if string_dir == "Left" then player:play('Player static left side view') elseif string_dir == "Up" then player:play('Player static back view') elseif string_dir == "Down" then player:play('Player static front view') elseif string_dir == "Right" then player:play('Player static right side view') end dragDirection = "stopped" flag = "on" end end end counttracker = counttracker + 1 moved = 0endlocal function wrap (event)if player.x < 25 thenplayer.x = 25endif player.x > 743 thenplayer.x = 743endif player.y < 50 thenplayer.y = 50--1st transition of background and player down one screen's length if flag2~="on" then flag = "on" timer.performWithDelay(1000,function(event) player:addEventListener("touch",onTouch) end, 1) function playerStopFunction() player:play('Player static back view') end timer.performWithDelay(1000,playerStopFunction,1) player:removeEventListener( "touch", onTouch ) display.getCurrentStage():setFocus( nil ) player.isFocus = false player:play('Player run back view') transition.to(player, {time = 1000, delay = 0, y = player.y + 920}) transition.to(background, {time = 1000, delay = 0, y = background.y + 1024}) transition.to(background2, {time = 1000, delay = 0, y = background2.y + 1024}) flag2="on" --2nd transition of background and player down one more screen's length elseif flag3~="on" then flag = "on" timer.performWithDelay(1000,function(event) player:addEventListener("touch",onTouch) end, 1) function playerStopFunction() player:play('Player static back view') end timer.performWithDelay(1000,playerStopFunction,1) player:removeEventListener( "touch", onTouch ) display.getCurrentStage():setFocus( nil ) player.isFocus = false player:play('Player run back view') transition.to(player, {time = 1000, delay = 0, y = player.y + 920}) transition.to(background2, {time = 1000, delay = 0, y = background2.y + 1024}) transition.to(background3, {time = 1000, delay = 0, y = background3.y + 1024}) flag3="on" end endif player.y > 1010 thenplayer.y = 1010endend---[[--Create display group and spawn scooterslocal function main() --forward declaration of the table, so it is visible. local spawnedGroup = display.newGroup() local transitions = {} local timers = {} local function scooterSpawn (event) local scooter = spriteFactory:newSpriteGroup('Scooter 1 moving right') scooter:addPhysics(physics,'dynamic',{}) scooter.x = -100 scooter.y = math.random(120,924) physics.addBody(scooter,"dynamic",{isSensor = true}) scooter.isSensor = true transitions[#transitions + 1] = transition.to(scooter, {time = math.random(1400,1500), delay = 0, x = scooter.x + 968, onComplete=function() scooter :removeSelf() end}) spawnedGroup:insert(scooter) end local cleanUp = function() --cancel and nil all the transitions for i=1,#transitions do if transitions[i] then transition.cancel(transitions[i]) transitions[i] = nil end end --cancel and nil all the timers for i=1,#timers do if timers[i] then timer.cancel(timers[i]) timers[i] = nil end end display.remove(spawnedGroup) end local moveCamera = function() transitions[#transition + 1] = transition.to(spawnedGroup, {time = 1000, y = spawnedGroup.y + 1200, onComplete = cleanUp}) end timers[#timers + 1] = timer.performWithDelay(math.random(20,35),scooterSpawn,0) endmain()--]] local function flag4reset(event)flag4="off"end --Collision function local function onPlayerCollision() if flag4~="on" then player.isVisible = false local angel = angelFactory:newSpriteGroup('Player Angel copy 2') angel.x = player.x angel.y = player.y transition.to(angel, {time = 1000, delay = 0, y = player.y - 920, onComplete=function() angel :removeSelf() end}) timer.performWithDelay(1000,function() player.isVisible = true end,1) timer.performWithDelay(1000,function() player.y = 970 end,1) media.playEventSound( "Punch.wav" ) print("collided with square") flag4="on" timer.performWithDelay(1000,flag4reset,1) endendplayer:addEventListener("collision",onPlayerCollision)Runtime:addEventListener("enterFrame", wrap)player:addEventListener( "touch", onTouch )timer.performWithDelay(1, movementTracker,0)[/code]I'm about to add in the conditional a little too confused at this point to take any action, so I thought I'd run it by you first:) I Any suggestions would be much appreciated!Thanks,Steven [import]uid: 79394 topic_id: 15294 reply_id: 56595[/import]

Am working on it… u are using quite a lot of images and sprites. had to do some work to get it in working condition.
now I got it working but the traffic is crazy I cannot reach the other side… :slight_smile:

[import]uid: 71210 topic_id: 15294 reply_id: 56602[/import]

try this
[lua]local physics = require(“physics”)
physics.start()
physics.setGravity(0, 0)
–physics.setDrawMode(“hybrid”)
physics.setDrawMode(“normal”)

–forward declaration for functions
–now the order of function doesn’t matter
local onTouch,movementTracker,wrap,cleanUp,moveCamera,flag4reset,onPlayerCollision,scooterSpawn
local background=display.newImage(“Street (first version).png”)
background:setReferencePoint(display.TopLeftReferencePoint)
background.x=0
background.y=0
local background2=display.newImage(“Street (first version).png”)
background2:setReferencePoint(display.TopLeftReferencePoint)
background2.x=0
background2.y=-1024
local background3=display.newImage(“Street (first version).png”)
background3:setReferencePoint(display.TopLeftReferencePoint)
background3.x=0
background3.y=-1024

local loqsprite = require(‘loq_sprite’)
local playerFactory = loqsprite.newFactory(‘playerSheet’)
local spriteFactory = loqsprite.newFactory(‘sheet’)
local angelFactory = loqsprite.newFactory(‘angelSheet’)
local player = playerFactory:newSpriteGroup()

atrace(xinspect(player:getSpriteNames()))

local prevx = 0
local prevy = 0

local dragDirection
local string_dir = ‘’
local flag
local flag2
local flag3
local flag4
local moved = 0
local counttracker = 0
player.x = 385
player.y = 970
player:addPhysics(physics,‘kinematic’,{})
physics.addBody(player,“kinematic”)
player:play(‘Player static back view’)

– text to display direction
local directionTXT = display.newText("Direction: ",130,25,nil,25)
directionTXT:setTextColor( 255,0,0 )

–Player movement function
function onTouch( event )
local player = event.target
local phase = event.phase
if “began” == phase then
– Make target the top-most object
local parent = player.parent
parent:insert( player )
display.getCurrentStage():setFocus( player )

– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
player.isFocus = true

– Store initial position
player.x0 = event.x - player.x
player.y0 = event.y - player.y
prevx = event.x
prevy = event.y
elseif player.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
player.x = event.x - player.x0
player.y = event.y - player.y0

local sqrt = math.sqrt
local dx = prevx - event.x
local dy = prevy - event.y
local distance = sqrt(dx * dx + dy * dy);

if distance>10 then
flag=“off”
local angle = math.atan2(dy,dx) * 57.2957795
if angle>=45*-1 and angle<=45 then
string_dir=“Left”
if dragDirection ~= “left” then
player:play(‘Player run side view left’)
dragDirection = “left”
end
elseif angle>45 and angle<135 then
string_dir=“Up”
if dragDirection ~= “up” then
player:play(‘Player run back view’)
dragDirection = “up”
end
elseif angle>135*-1 and angle<45*-1 then
string_dir=“Down”
if dragDirection ~= “down” then
player:play(‘Player run front view’)
dragDirection = “down”
end
elseif angle>=135 or angle<=135*-1 then
string_dir=“Right”
if dragDirection ~= “right” then
player:play(‘Player run side view right’)
dragDirection = “right”
end
end
prevx=event.x
prevy=event.y
directionTXT.text ="Direction : "…string_dir
moved = moved + 1
counttracker = 0
end

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
if string_dir == “Left” then
player:play(‘Player static left side view’)
elseif string_dir == “Up” then
player:play(‘Player static back view’)
elseif string_dir == “Down” then
player:play(‘Player static front view’)
elseif string_dir == “Right” then
player:play(‘Player static right side view’)
end
dragDirection = “stopped”
flag = “on”
end
– Important to return true. This tells the system that the event
– should not be propagated to listeners of any objects underneath.
return true
end

end

function movementTracker(event)
if moved == 0 then
if flag ~= “on” then
if counttracker == 3 then
print("not moving and last movement was towards "…string_dir)
if string_dir == “Left” then
player:play(‘Player static left side view’)
elseif string_dir == “Up” then
player:play(‘Player static back view’)
elseif string_dir == “Down” then
player:play(‘Player static front view’)
elseif string_dir == “Right” then
player:play(‘Player static right side view’)
end
dragDirection = “stopped”
flag = “on”
end
end
end
counttracker = counttracker + 1
moved = 0
end
function wrap (event)

if player.x < 25 then
player.x = 25
end

if player.x > 743 then
player.x = 743
end

if player.y < 50 then
player.y = 50
–to move the scooter group
moveCamera()
–you should remove the collisiion listener when moving else it may collide with object and your player will become Angel :slight_smile:
player:removeEventListener( “collision”, onPlayerCollision )

–1st transition of background and player down one screen’s length
if flag2~=“on” then
flag = “on”
timer.performWithDelay(1000,function(event) player:addEventListener(“touch”,onTouch) end, 1)
function playerStopFunction()
player:play(‘Player static back view’)
end
timer.performWithDelay(1000,playerStopFunction,1)
player:removeEventListener( “touch”, onTouch )
display.getCurrentStage():setFocus( nil )
player.isFocus = false
player:play(‘Player run back view’)
transition.to(player, {time = 1000, delay = 0, y = player.y + 920})
transition.to(background, {time = 1000, delay = 0, y = background.y + 1024})
transition.to(background2, {time = 1000, delay = 0, y = background2.y + 1024})
flag2=“on”
–2nd transition of background and player down one more screen’s length
elseif flag3~=“on” then
flag = “on”
timer.performWithDelay(1000,function(event) player:addEventListener(“touch”,onTouch) end, 1)
function playerStopFunction()
player:play(‘Player static back view’)
end
timer.performWithDelay(1000,playerStopFunction,1)
player:removeEventListener( “touch”, onTouch )
display.getCurrentStage():setFocus( nil )
player.isFocus = false
player:play(‘Player run back view’)
transition.to(player, {time = 1000, delay = 0, y = player.y + 920})
transition.to(background2, {time = 1000, delay = 0, y = background2.y + 1024})
transition.to(background3, {time = 1000, delay = 0, y = background3.y + 1024})
flag3=“on”
end
end

if player.y > 1010 then
player.y = 1010

end
end

–forward declaration of the table, so it is visible.
local spawnedGroup = display.newGroup()
local transitions = {}
local timers = {}

function scooterSpawn (event)
local scooter = spriteFactory:newSpriteGroup(‘Scooter 1 moving right’)
scooter:addPhysics(physics,‘dynamic’,{})
scooter.x = -100
scooter.y = math.random(120,924)
physics.addBody(scooter,“dynamic”,{isSensor = true})
scooter.isSensor = true
transitions[#transitions + 1] = transition.to(scooter, {time = math.random(1400,1500), delay = 0, x = scooter.x + 968, onComplete=function() scooter :removeSelf() end})
spawnedGroup:insert(scooter)
end

cleanUp = function()
–cancel and nil all the transitions
for i=1,#transitions do
if transitions[i] then
transition.cancel(transitions[i])
transitions[i] = nil
end
end
–cancel and nil all the timers
for i=1,#timers do
if timers[i] then
timer.cancel(timers[i])
timers[i] = nil
end
end
display.remove(spawnedGroup)
end

moveCamera = function()
transitions[#transition + 1] = transition.to(spawnedGroup, {time = 1000, y = spawnedGroup.y + 1200, onComplete = cleanUp})
end

timers[#timers + 1] = timer.performWithDelay(math.random(20,35),scooterSpawn,0)

function flag4reset(event)
flag4=“off”
end

–Collision function
function onPlayerCollision()
if flag4~=“on” then
player.isVisible = false
local angel = angelFactory:newSpriteGroup(‘Player Angel copy 2’)
angel.x = player.x
angel.y = player.y
transition.to(angel, {time = 1000, delay = 0, y = player.y - 920, onComplete=function() angel :removeSelf() end})
timer.performWithDelay(1000,function() player.isVisible = true end,1)
timer.performWithDelay(1000,function() player.y = 970 end,1)
media.playEventSound( “Punch.wav” )
print(“collided with square”)
flag4=“on”
timer.performWithDelay(1000,flag4reset,1)
end
end

player:addEventListener(“collision”,onPlayerCollision)
Runtime:addEventListener(“enterFrame”, wrap)
player:addEventListener( “touch”, onTouch )
timer.performWithDelay(1, movementTracker,0)[/lua] [import]uid: 71210 topic_id: 15294 reply_id: 56611[/import]

I haven’t tested the above code as i don;t have the required graphics…
but below is the skinned version of it and it works fine…
[lua]local physics = require(“physics”)
physics.start()
physics.setGravity(0, 0)

local player = display.newCircle(20,20,40)

local prevx = 0
local prevy = 0

local dragDirection
local string_dir = ‘’
local flag
local flag2
local flag3
local flag4
local moved = 0
local counttracker = 0
player.x = 385
player.y = 970
physics.addBody(player,“kinematic”)

– text to display direction
local directionTXT = display.newText("Direction: ",130,25,nil,25)
directionTXT:setTextColor( 255,0,0 )

–forward declaration of the table, so it is visible.
local spawnedGroup = display.newGroup()
local transitions = {}
local timers = {}

local function scooterSpawn (event)
local scooter = display.newRect(20,20,20,20)
scooter.x = -100
scooter.y = math.random(120,924)
physics.addBody(scooter,“dynamic”,{isSensor = true})
scooter.isSensor = true
transitions[#transitions + 1] = transition.to(scooter, {time = math.random(1400,1500), delay = 0, x = scooter.x + 968, onComplete=function() scooter :removeSelf() end})
spawnedGroup:insert(scooter)
end

local cleanUp = function()
–cancel and nil all the transitions
for i=1,#transitions do
if transitions[i] then
transition.cancel(transitions[i])
transitions[i] = nil
end
end
–cancel and nil all the timers
for i=1,#timers do
if timers[i] then
timer.cancel(timers[i])
timers[i] = nil
end
end
display.remove(spawnedGroup)
end

local moveCamera = function()
transitions[#transition + 1] = transition.to(spawnedGroup, {time = 1000, y = spawnedGroup.y + 1200, onComplete = cleanUp})
end

timers[#timers + 1] = timer.performWithDelay(math.random(100,135),scooterSpawn,0)

–Player movement function
local function onTouch( event )
local player = event.target
local phase = event.phase
if “began” == phase then
– Make target the top-most object
local parent = player.parent
parent:insert( player )
display.getCurrentStage():setFocus( player )
player.isFocus = true
– Store initial position
player.x0 = event.x - player.x
player.y0 = event.y - player.y
elseif player.isFocus then
if “moved” == phase then
player.x = event.x - player.x0
player.y = event.y - player.y0
end
end
return true
end

–Collision function
local function onPlayerCollision()
if flag4~=“on” then
player.isVisible = false
–local angel = angelFactory:newSpriteGroup(‘Player Angel copy 2’)
local angel = display.newRect(20,20,30,30)
angel.x = player.x
angel.y = player.y
transition.to(angel, {time = 1000, delay = 0, y = player.y - 920, onComplete=function() angel :removeSelf() end})
timer.performWithDelay(1000,function() player.isVisible = true end,1)
timer.performWithDelay(1000,function() player.y = 970 end,1)
– media.playEventSound( “Punch.wav” )
print(“collided with square”)
flag4=“on”
timer.performWithDelay(1000,flag4reset,1)
end
end
local function wrap (event)
if player.x < 25 then
player.x = 25
end

if player.x > 743 then
player.x = 743
end

if player.y < 50 then
player.y = 50
moveCamera()
transition.to(player, {time = 1000, delay = 0, y = player.y + 920})
player:removeEventListener( “touch”, onTouch )
player:removeEventListener(“collision”,onPlayerCollision)
end

if player.y > 1010 then
player.y = 1010
end
end

local function flag4reset(event)
flag4=“off”
end

player:addEventListener(“collision”,onPlayerCollision)
Runtime:addEventListener(“enterFrame”, wrap)
player:addEventListener( “touch”, onTouch )[/lua] [import]uid: 71210 topic_id: 15294 reply_id: 56613[/import]

Yup, that’s working perfectly alright:) And yes, the traffic IS outrageous, just like the real thing, lol. I’m going to modify my code as per your example and see how it fares… [import]uid: 79394 topic_id: 15294 reply_id: 56624[/import]