how to change variable value when each level is spawned

Ok, perhaps the title is not quite correct.

I am re visiting a game I shelved months back, it was shelved because it was an exercise in meta tables and OOP. However this is irrelevant to my question :slight_smile:

what I need to happen:

I have a wave of aliens, when they are all destroyed another wave is spawned. I want the wave to spawn lower down the screen each time up to a maximum of 5 times.

So I need to change the variable value of the spawning aliens each time they are respawned.

I have the game working perfect , at the moment I am just re-running the main game after each wave is destroyed.

Here is some code(stripped down to relevant parts)

I need to change the

[lua] levelDrop = 70 [/lua] value

to 140 then 210,280,350 (increments of 70) after each wave is destroyed please.

So when gameover() is called it will begin with the updated levelDrop value.

MAIN.LUA

[lua]


– CREATE ENEMY –

local levelDrop = 70

function createEnemy(x, y, row)
 
  for j = 1, 5 do
    for i = 1, 5 do
      allEnemys[#allEnemys + 1] = enemys:new()
      allEnemys[#allEnemys]:init(i * 60, j * 70 + levelDrop, j)
      allEnemys[#allEnemys]:start()
    end
  end
end


                    – GAMEOVER –

function gameOver()
    Runtime:removeEventListener(“enterFrame”, onEnterFrame)
    Runtime:removeEventListener(“enterFrame”, movePlayer)

    display.remove(leftWall)
    leftWall = nil
    display.remove(rightWall)
    rightWall = nil
    display.remove(ceilng)
    ceiling = nil
    display.remove(floorWall)
    floorWall = nil
    display.remove(player)
    player = nil
    display.remove(aBullet)
    aBullet = nil

    enemyCount = 0

       for i = 1,#allEnemys do
         timer.cancel(allEnemys[i].clock)
         Runtime:removeEventListener( “enterFrame”, allEnemys[i] )
         display.remove(allEnemys[i].image)
         allEnemys[i].image=nil
       end
         allEnemys=nil
         allEnemys = {}

    layers:removeSelf()
    layers = nil

    enableFire = true

    print(" Hey gameover")
    cleanupBlocks()

    timer.performWithDelay(4000,runGame,1)
end

ENEMY.LUA

         elseif enemyCount == 2
                      then
                    self.rightHit = 0
                    self.leftHit = 0
                    self.movement = 0
                    self.movement = 10
                           print(“less than 2”)

        elseif enemyCount == 1
         then
       
                  gameOver()

        end
        end

Inside gameOver() just put this line:

[lua]

levelDrop = levelDrop + 70

[/lua]

Unless I misunderstood the question, that should do it for you.

 Jay

PS - In your code shown above I don’t see the runGame function, but make sure the levelDrop variable is defined outside of that function.

@JAWhye,

talk about wood for trees !

works a treat your a diamond.

much appreciated.

ok, back again.

I am just trying to stop the var value increasing past say for example 280.

I am saying this:

if levelDrop == 280 then

levelDrop = 280

end

however nothing is happening, any explanation please?

placing in my enemy creation code.

ha,

sorted it

if levelDrop > 210 then

levelDrop = 210

end

simple,just acts as a limiter so it wont spawn them at a higher px than that.

Also didnt forget to reset the value to zero when the player dies.

excellent

Inside gameOver() just put this line:

[lua]

levelDrop = levelDrop + 70

[/lua]

Unless I misunderstood the question, that should do it for you.

 Jay

PS - In your code shown above I don’t see the runGame function, but make sure the levelDrop variable is defined outside of that function.

@JAWhye,

talk about wood for trees !

works a treat your a diamond.

much appreciated.

ok, back again.

I am just trying to stop the var value increasing past say for example 280.

I am saying this:

if levelDrop == 280 then

levelDrop = 280

end

however nothing is happening, any explanation please?

placing in my enemy creation code.

ha,

sorted it

if levelDrop > 210 then

levelDrop = 210

end

simple,just acts as a limiter so it wont spawn them at a higher px than that.

Also didnt forget to reset the value to zero when the player dies.

excellent