Object generation

I have a game where a character runs down a slope, so I have made a table to create the objects for the slope. However,I was wondering whether it would be possible to generate the slope as the character goes along, rather than generating it all at the beginning. Also, would it be possible to remove the objects once the character has run past them? The code I am currently using is:

local slope = {}
slopeshape = { -240,-100, 240,100, -240,100 }
local function MakeBottom()
 for s = 1,30 do
 slope[s] = display.newImage("slope.png")
 physics.addBody(slope[s], "static", {friction=0.01, density = 1, bounce = 0.1, shape=slopeshape})
 slope[s]:setReferencePoint(display.TopLeftReferencePoint)
 slope[s].x = (s\*468) - 480
 slope[s].y = (s\*195) - 100
 slope[s].material = "ground"
 moveGroup:insert(slope[s])
 end
end
MakeBottom()

[import]uid: 116264 topic_id: 24349 reply_id: 324349[/import]

Try this to create the slopes on the way:

local slopeshape = { -50,-10, 50,10 }  
local slopeLoc = 150;  
local slopeLat = 150;  
  
function spawnSlopes( event )  
 if(character.x + 500 \> slopeLoc)then  
 local slope = display.newRect( 0, 0, 0, 0)  
 slope.x = character.x + 600  
 slope.y = slopeLat  
 slope.myName = "slope"  
 slopeLoc = slope.x  
 slopeLat = slope.y + 20  
 physics.addBody(slope, "static", {friction=0.01, density = 1, bounce = 0.1, shape=slopeshape} )  
 end  
end  
  
Runtime:addEventListener( "enterFrame", spawnSlopes )  

[import]uid: 77199 topic_id: 24349 reply_id: 98386[/import]

Ive tried that but no slopes are formed when I start, and the man just falls forever [import]uid: 116264 topic_id: 24349 reply_id: 98474[/import]

Don’t worry, ive just edited my code and managed to get the right effect. thanks for your help [import]uid: 116264 topic_id: 24349 reply_id: 98543[/import]

The code I gave works except it creates the first slope at the x position of about 600 away from you. I forgot to add a random starting slope for the beginning. Just create a random starting slope for the first 600 and then also change the y of the spawnSlopes by however much you need and it should work perfectly. Let me know if you need help [import]uid: 77199 topic_id: 24349 reply_id: 98544[/import]