Hi
I’m writing a program which spawns objects, moves them down the screen
and deletes them when they get to the bottom.
I want to change the speed of all the spawned objects at once. My current code only changes the speed of newly spawned objects.
How can I get the objects to check their speed against a variable each frame and adjust it, so they all speed up/slow down at the same time?
Heres my code:
[code]
physics = require(“physics”)
physics.start()
physics.setGravity(0,0)
_H = display.contentHeight;
_W = display.contentWidth;
mRand = math.random;
terrainspeed = 200
tsmod = 0
craterRate = 1000
local hitdet = display.newRect(0, 0, _W, 5)
hitdet.x = _W / 2
hitdet.y = _H - 5
hitdet:setFillColor(0, 0, 255)
physics.addBody(hitdet, “static”, {density = 1.0, friction = 0, bounce = 0, isSensor =true})
hitdet.myName = “hitDet”
function tsmodifier() --picks a random value by which to modify terrainspeed
tsmod = mRand(0, 50)
end
function spawnCrater() – spawns a crater, checks its y position and moves it if necessary.
crater = display.newCircle (mRand(0, _W), _H / _H, mRand(30, 100))
physics.addBody( crater, { friction=0, bounce=0, radius = crater.radius} )
crater.isSensor = true
crater.myName = “Crater”
function craterspeed(event)
crater:setLinearVelocity( 0, terrainspeed + tsmod)
end
Runtime:addEventListener(“enterFrame”, craterspeed)
end
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
print( self.myName … ": collision began with " … event.other.myName )
local function removecrater()
event.other:removeSelf()
end
Timer4 = timer.performWithDelay(1,removecrater, 1)
elseif ( event.phase == “ended” ) then
print( self.myName … ": collision ended with " … event.other.myName )
end
end
hitdet.collision = onLocalCollision
hitdet:addEventListener( “collision”, hitdet )
Timer1 = timer.performWithDelay(craterRate, spawnCrater, 0)
Timer3 = timer.performWithDelay(5000,tsmodifier, 0)
``
Thanks [import]uid: 67933 topic_id: 13452 reply_id: 313452[/import]