How Do I Make The Game Harder Without Need To Change Scene?

Hi guys, im really inspired from android game NinJump and im looking to make similar mechanism to my game. I want my game to be harder through the game as long as the player hasn’t died yet (items spawn faster and different various items only appear when we survive the first few minutes).

What i have is like this atm:

[code]function spawn( objectType, x, y )
if gameIsActive == true then
bottle = display.newImage(“bottle1.png”)
bottle.name = “bottle”
bottle.x = math.random(320)
bottle.y = -100
bottle.rotation = 10
physics.addBody(bottle, { density=2.0, friction=0.5, bounce=0.3})

if bottle.y > display.contentHeight then
print(“failed”)
end
end
end
timer.performWithDelay(2000,spawn,60000)[/code]

basically with this, the bottle should “spawn” every 2 secs. Now I honestly don’t know how to make the bottle spawn faster if let’s say the game has been played for few minutes and player still survive…I also would like to create new item (for example: banana) but it shouldn’t start from the very first game but only if the player has survived the first few minutes as well…basically just like the NinJump game where you dont see dynamite when your score is still very low…

how do I set up that kind of mechanism? [import]uid: 114765 topic_id: 20268 reply_id: 320268[/import]

Hey,

Theres a fair few ways to go about doing that, one way would to just keep track of the elasped time, then edit variables accordingly. Another way is shown below… I just knocked this up, so its probably a horrendous way of doing it, but it should give you some ideas at the very least.

[code]
local staticTimerFunc --Forward declaration of the timer function…
local bottleSpawns = 0 --Variable to keep track of bottle spawns…

function spawn( objectType, x, y )
if gameIsActive == true then
–Increase the variable each time one spawns…
bottleSpawns = bottleSpawns + 1

bottle = display.newImage(“bottle1.png”)
bottle.name = “bottle”
bottle.x = math.random(320)
bottle.y = -100
bottle.rotation = 10
physics.addBody(bottle, { density=2.0, friction=0.5, bounce=0.3})

if bottle.y > display.contentHeight then
print(“failed”)
end

–Check to see if enough bottles have spawned to start spawning “Bananas” aswell…
if bottleSpawns > 60 then
–Could also add 50/50 chance to call this function here too…
createBanana() --Call your new spawn function.
end

–Call the timer function again to start a new spawn sequence…
staticTimerFunc()
end
end

–The time for the first timer below is now slowly subtracted by the amount of bottles spawned*10… Therefore lowering the time between spawns each time.
local function changingTime() local variableTimer = timer.performWithDelay(2000-(bottleSpawns*10),spawn,1); end
function staticTimerFunc() local staticTimer = timer.performWithDelay(50,changingTime,1); end
–Calls this function initially to start everything off.
staticTimerFunc()
[/code] [import]uid: 69826 topic_id: 20268 reply_id: 79205[/import]

hi mate,

thank you so much.

it really works, thank you. However, im curious about something

local function changingTime() local variableTimer = timer.performWithDelay(2000-(bottleSpawns\*10),spawn,1); end function staticTimerFunc() local staticTimer = timer.performWithDelay(50,changingTime,1); end

I know it’s the code that makes the bottle spawn faster but I want to know why did you put “1” after spawn? The reason why I put “60000” there (earlier) was because so the bottle would keep spawning for a long time. How could it work with just “1” as the number there? [import]uid: 114765 topic_id: 20268 reply_id: 79210[/import]

Math!
Lets say using your example using timer delay…

and you want to have 20 levels of play… Each level gets a little bit faster.

local level = 1  
local maxLevel = 20  
local elapsedTime = 0  
  
function spawn( objectType, x, y )  
 elapsedTime = elapsedTime + 1  
 if elapsedTime \> (maxLevel - level) then  
 if gameIsActive == true then  
 bottle = display.newImage("bottle1.png")  
 bottle.name = "bottle"  
 bottle.x = math.random(320)  
 bottle.y = -100  
 bottle.rotation = 10  
 physics.addBody(bottle, { density=2.0, friction=0.5, bounce=0.3})  
   
 if bottle.y \> display.contentHeight then  
 print("failed")  
 end  
 end  
 elapsedTime = 0  
 end  
end  
timer.performWithDelay(100,spawn,1200000)  

Note I’m now calling spawn every 10th (1/20th of the time)of a second , so to create the same number of objects, we have to multiply the number of times we are going to spawn by 20 to keep this running for the same total duration.

So on the first pass, elapsedTime will be 1 which is less or equal to than (20-1) or 19, so it just returns. Every 100ms, this will get called and elapasedTime increments. Once it hits 20 (20 * 100ms = 2 seconds or your level 1 speed) your object will spawn.

Then at some point when you decided its time to up the difficulty, just increment level (and make sure it doesn’t get over maxLevel). Lets say its now level 2. The objects should spawn every 1.9 seconds. Level 3, 1.8 seconds, etc.

You may have to fiddle with it (and of course this is untested, but…)

[import]uid: 19626 topic_id: 20268 reply_id: 79211[/import]

The reason they are both ‘1’ is because in your spawn function, i have it calling staticTimerFunc() again. Therefore its essentially a never ending loop of spawning the item.

staticTimerFunc() calls changingTime() which calles spawn(), that in turn calls staticTimerFunc() again.

Robs way is also good, and probably better than me using timers again and again lol. [import]uid: 69826 topic_id: 20268 reply_id: 79217[/import]

If it were me I wouldn’t use a timer. Lots of overhead…

I’d setup an Runtime:addEventListener(“enterFrame”,spawn) gameLoop myself and knowing I’m getting called 30 times per second (or 60 depending on your setup), I’d use similar logic to spawn after X number of frames.

[import]uid: 19626 topic_id: 20268 reply_id: 79220[/import]

hi guys, thanks for the explanation. I will check these codes later tonight and will comment here back if i need anything else. Thanks! [import]uid: 114765 topic_id: 20268 reply_id: 79339[/import]