How do i restart from the begining when the game ends

hi so i’m a newBie to corona development and i’m trying to develop a very simple game

there is 2 object (enemy)

and single player (player)

i’ve applied physics in the game

mianly enemy moves around and and player just have to not collide each other when it hapens 

this event start

function onCollision (event)
  happy:removeSelf()
end

so player one diappear i was wondering how can i get back the player again

please help ASAP

Thanks

This while seemingly a simple question is really complex because every game has different ways of wanting to do this.  Without knowing what your end goal is, it’s too broad of a question to answer.

In your case, I suspect the question you are really asking is how do you respawn your enemy?  Again, a simple concept but complex to answer because we don’t know enough about your code, like how you are creating your enemies, what should cause them to respawn, etc.

You probably should put your enemy creation code into a function that you can just call.  Once you have that, perhaps you could add a timer in your onCollision event that would call your enemyCreate function.  Perhaps you want it to respawn immediately.   Perhaps killing the enemy triggers a series of events like going to another scene showing a high score and then you go back and start completely over.

If you can share more thoughts…

Rob

ok i guess my question bit complex so i just want share the code ;

local physics = require “physics”
physics.start()
physics.setGravity(0,0)
–setting background image center
local centerX = display.contentCenterX
local centerY = display.contentCenterY

local background = display.newImage(“background.jpg”)
background.y = centerY
background.x = centerX

–playeroneface
local happy = display.newImage(“happygirl.png”)
happy.x = 100
happy.y = 100
physics.addBody(happy, “dynamic”)

local angry = display.newImage(“angrymen1.png”)
angry.x = 880
angry.y = 600
physics.addBody(angry, “static”)

local angry2 = display.newImage(“angrymen2.png”)
angry2.x = 830
angry2.y = 520
physics.addBody(angry2, “static”)

function touchScreen(event)
   if event.phase == “ended” then
    --print(event.x)
    transition.to(happy, {time=1000, x=event.x, y=event.y})
    end
end
Runtime:addEventListener(“touch”, touchScreen)

function moveAngry()
  transition.to(angry,{time=1000, x=math.random(80,880),y=math.random(60,580), onComplete=moveAngry})
  transition.to(angry2,{time=1000, x=math.random(80,880),y=math.random(60,580)})
end

moveAngry()

function onCollision (event)
  happy:removeSelf()

– here i want display text Restart and start gain
end
Runtime:addEventListener(“collision”, onCollision)

Move code like this into a function:

local happy

local function createHappy()

     --playeroneface
     happy = display.newImage(“happygirl.png”)
     happy.x = 100
     happy.y = 100
     physics.addBody(happy, “dynamic”)

end

createHappy()

This will get your app back to where you currently have it.  Then in your collision listener:

function onCollision (event)
  happy:removeSelf()

  local text = display.newText(“Game Over. Try Again”, display.contentCenterX, display.contentCenterY, native.systemFontBold, 20)

  timer.performWithDelay(3000, function() text:removeSelf(); text = nil; end)

  timer.performWithDelay(4000, function()

        – reset things

        createHappy();

        angry.x = 880
        angry.y = 600

         angry2.x = 830
        angry2.y = 520

  end )
end

or something like that.

Thanks Rob …

I’d read up on finite state machines as well ; these represent a sort of high level flow through different parts of the game.

This while seemingly a simple question is really complex because every game has different ways of wanting to do this.  Without knowing what your end goal is, it’s too broad of a question to answer.

In your case, I suspect the question you are really asking is how do you respawn your enemy?  Again, a simple concept but complex to answer because we don’t know enough about your code, like how you are creating your enemies, what should cause them to respawn, etc.

You probably should put your enemy creation code into a function that you can just call.  Once you have that, perhaps you could add a timer in your onCollision event that would call your enemyCreate function.  Perhaps you want it to respawn immediately.   Perhaps killing the enemy triggers a series of events like going to another scene showing a high score and then you go back and start completely over.

If you can share more thoughts…

Rob

ok i guess my question bit complex so i just want share the code ;

local physics = require “physics”
physics.start()
physics.setGravity(0,0)
–setting background image center
local centerX = display.contentCenterX
local centerY = display.contentCenterY

local background = display.newImage(“background.jpg”)
background.y = centerY
background.x = centerX

–playeroneface
local happy = display.newImage(“happygirl.png”)
happy.x = 100
happy.y = 100
physics.addBody(happy, “dynamic”)

local angry = display.newImage(“angrymen1.png”)
angry.x = 880
angry.y = 600
physics.addBody(angry, “static”)

local angry2 = display.newImage(“angrymen2.png”)
angry2.x = 830
angry2.y = 520
physics.addBody(angry2, “static”)

function touchScreen(event)
   if event.phase == “ended” then
    --print(event.x)
    transition.to(happy, {time=1000, x=event.x, y=event.y})
    end
end
Runtime:addEventListener(“touch”, touchScreen)

function moveAngry()
  transition.to(angry,{time=1000, x=math.random(80,880),y=math.random(60,580), onComplete=moveAngry})
  transition.to(angry2,{time=1000, x=math.random(80,880),y=math.random(60,580)})
end

moveAngry()

function onCollision (event)
  happy:removeSelf()

– here i want display text Restart and start gain
end
Runtime:addEventListener(“collision”, onCollision)

Move code like this into a function:

local happy

local function createHappy()

     --playeroneface
     happy = display.newImage(“happygirl.png”)
     happy.x = 100
     happy.y = 100
     physics.addBody(happy, “dynamic”)

end

createHappy()

This will get your app back to where you currently have it.  Then in your collision listener:

function onCollision (event)
  happy:removeSelf()

  local text = display.newText(“Game Over. Try Again”, display.contentCenterX, display.contentCenterY, native.systemFontBold, 20)

  timer.performWithDelay(3000, function() text:removeSelf(); text = nil; end)

  timer.performWithDelay(4000, function()

        – reset things

        createHappy();

        angry.x = 880
        angry.y = 600

         angry2.x = 830
        angry2.y = 520

  end )
end

or something like that.

Thanks Rob …

I’d read up on finite state machines as well ; these represent a sort of high level flow through different parts of the game.