what is the logic behind constantly moving levels which are random everytime you load the game. Examples of this would be from games like iCopter free on iphone. [import]uid: 23546 topic_id: 12622 reply_id: 312622[/import]
Games like that would normally have a timer going to spawn objects off screen. Then, I imagine, would use math.random() to generate a number and spawn an object based on that number. [import]uid: 52491 topic_id: 12622 reply_id: 46225[/import]
Yeah something like
function spawnFloor()
floor = display.newRect(0,0,40,40)
floor.x = math.random(100,400)
floor.y = math.random(300,600)
floor.xScale = math.random(1,5)
floor.yScale = math.random(1,5)
end
[import]uid: 24111 topic_id: 12622 reply_id: 46227[/import]
Thank you. Appreciate the help! [import]uid: 23546 topic_id: 12622 reply_id: 46324[/import]
I’m currently producing a game a bit like this. I used the following tutorial which helped wrap my head around the ideas involved.
http://mobile.tutsplus.com/tutorials/corona/creating-a-scrolling-background-with-corona-sdk/
hope that helps.
Dan [import]uid: 67933 topic_id: 12622 reply_id: 46335[/import]
Thanks spider, i tried using it but still no luck. If you don’t mind, could you please share your code regarding this? [import]uid: 23546 topic_id: 12622 reply_id: 46547[/import]
What is it you’re specifically trying to do? What will the level look like, how will the objects behave, and how will the player interact with them? Try breaking it all down into small chunks and solve each problem one at a time.
For the code below, what I’m doing is generating circles at the top of the screen. Each circle has a random position on the x axis, and a random radius. The circles spawn on a timer set to trigger ever 1000 milliseconds, (1 second). Once triggered the timer calls the function “spawnCrater”. Within the “spawnCrater” function is another timer, which calls the “movecrater” function and moves the circles by changing their y position by the value of “terrainspeed.”
“tsmod” is a variable I can use to increase or decrease the speed at which the circles fall. In this example code, I have linked it to a timer which randomly changes its value every 5 seconds and prints its value to the terminal, (just so I can see what it’s doing and make sure it works).
This code is extremely basic, and a work in progress to test some ideas, so don’t expect anything really polished or ready to use.
--hides iphone status bar
--display.setStatusBar(display.HiddenStatusBar)
physics = require("physics")
physics.start()
physics.setGravity(0,0)
--physics.setDrawMode( "hybrid" ) -- uncomment to see physics interactions.
--cache content dimensions and math.random for better performance
\_H = display.contentHeight;
\_W = display.contentWidth;
mRand = math.random;
terrainspeed = 5
maxCrater = 5
tsmod = 0
local centerX = display.contentWidth/2;
function tsmodifier() --picks a random value by which to modify terrainspeed
tsmod = mRand(0, 10)
print (tsmod)
end
function spawnCrater() -- spawns a crater, checks its y position and moves it if necessary.
if maxCrater \> 0 then
local crater = display.newCircle (mRand(0, \_W), \_H / \_H, mRand(30, 100))
maxCrater = maxCrater - 1
--print (maxCrater)
local function moveCrater() -- moves the crater
if crater.y \< \_H + crater.contentHeight then
crater.y = crater.y + terrainspeed + tsmod
end
if crater.y \>= \_H + crater.contentHeight then
maxCrater = maxCrater + 1
end
end
Timer2 = timer.performWithDelay(10,moveCrater, 0)
end
end
Timer1 = timer.performWithDelay(1000,spawnCrater, 0)
Timer3 = timer.performWithDelay(5000,tsmodifier, 0)
Is that the sort of thing you’re after? If you have any questions, I’ll try and explain it further.
Dan [import]uid: 67933 topic_id: 12622 reply_id: 46629[/import]
Thanks for the explanation. My project right now is to first build up to a game engine like icopter’s and then work from there. In the game the only problem so far is the random level generation on the bottom and the top. The method you are using is good for random objects/obstacles on the screen but not the level. I believe the only solution for that is to go with a the background loop. Create a long background in photoshop, cut it into 20 some different even pieces. Place those 20 in a table, and randomly call those in a loop. And after they pass the screen remove from the loop.
Thank you again, Appreciate the help. [import]uid: 23546 topic_id: 12622 reply_id: 46659[/import]