Issue when reloading a scene

Hi guys, in my game I’ve successfully added the “replay” feature.

The point is that in this app, player has to draw a line using the code below, and the first time the game is loaded it works fine, but when I tap on replay, the whole game works too, but it seems to be impossible to draw the line.

Here’s the code I’m using:

[lua]

local lines = {}

local lineGroup = display.newGroup()

local prevX,prevY

local isDrawing = true

local i = 1

local function distanceBetween(x1, y1, x2, y2)

    local dist_x = x2 - x1

    local dist_y = y2 - y1

    local distanceBetween = math.sqrt((dist_x*dist_x) + (dist_y*dist_y))

    return distanceBetween

end

local function drawLine(e)

    if(e.phase == “began”) then

   

        for i = #lines, 1, -1 do

      if (lines[i]) then

       lines[i].parent:remove(lines[i])

       lines[i] = nil

      end

    end

    lines = {}

    line_number = 100

    

        

        prevX = e.x

        prevY = e.y

        isDrawing = true

        

        

    elseif(e.phase == “moved”) then

        local distance = distanceBetween(prevX, prevY, e.x, e.y)

        if(isDrawing and distance < 100) then

            if(lines[i]) then lineGroup:remove(i) end

            lines[i] = display.newLine(prevX, prevY, e.x, e.y)

            lines[i]:setColor(255, 255, 0)

            lines[i].width = 3

            lines[i].myName = “lines”

    

    

if(lines[i].y < 400) then

  for i = #lines, 1, -1 do

      if (lines[i]) then

       lines[i].parent:remove(lines[i])

       lines[i] = nil

      end

    end

 end

            local dist_x = e.x - prevX

            local dist_y = e.y - prevY

            physics.addBody(lines[i], “static”, { density = 1, friction = 0.5, bounce = -0.8, shape = {0, 0, dist_x, dist_y, 0, 0} } )

            lineGroup:insert(lines[i])

        end

   

  

    elseif(e.phase == “ended”) then

        isDrawing = true

    end

    return lineGroup

end

Runtime:addEventListener(“touch”,drawLine)

[/lua]

any suggestions? thanks! :slight_smile:

Is this code in your main.lua or in a separate module?  How are you reloading this?

Have you put in print statements into your touch handler to  make sure it’s getting called correctly?

Hi, thanks for the reply.

No, this code is in a separate module, and I’m reloading using:

[lua]

local replay = display.newImageRect(“replay.png”, 30, 30)

replay.x = _W/2

replay.y = _H/2

localGroup:insert(replay)

local function replayFunction()

director:changeScene(“game”)

end

replay:addEventListener(“touch”, replayFunction)

[/lua]

What do you mean with “Have you put in print statements into your touch handler to  make sure it’s getting called correctly?”

recode your replay touch handler to this:

local function replayFunction(event) &nbsp;&nbsp;&nbsp; if event.phase == "ended" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; director:changeScene("game") &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return true end

touch events fire off multiple events and you could be trying to change scenes too many times.  Also the return true at the end stops the event from propagating to other objects.

Now I don’t know if that’s going to solve your problem or not.  It sounds like for some reason you’re event listener isn’t getting added.  I would, for the purpose of a test, change it to:

local function replayFunction(event) &nbsp;&nbsp;&nbsp; print("replayFunction: ", event.phase) &nbsp;&nbsp;&nbsp; if event.phase == "ended" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; director:changeScene("game") &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return true end

And make sure you’re getting to where you need to be.  I would also put a print in front of:

Runtime:addEventListener(“touch”,drawLine)

to make sure that line is getting re-executed when your scene reloads.  Director is not my specialty and I don’t see enough of your module code in the top post to know where that Runtime listener is being added. 

Is this code in your main.lua or in a separate module?  How are you reloading this?

Have you put in print statements into your touch handler to  make sure it’s getting called correctly?

Hi, thanks for the reply.

No, this code is in a separate module, and I’m reloading using:

[lua]

local replay = display.newImageRect(“replay.png”, 30, 30)

replay.x = _W/2

replay.y = _H/2

localGroup:insert(replay)

local function replayFunction()

director:changeScene(“game”)

end

replay:addEventListener(“touch”, replayFunction)

[/lua]

What do you mean with “Have you put in print statements into your touch handler to  make sure it’s getting called correctly?”

recode your replay touch handler to this:

local function replayFunction(event) &nbsp;&nbsp;&nbsp; if event.phase == "ended" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; director:changeScene("game") &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return true end

touch events fire off multiple events and you could be trying to change scenes too many times.  Also the return true at the end stops the event from propagating to other objects.

Now I don’t know if that’s going to solve your problem or not.  It sounds like for some reason you’re event listener isn’t getting added.  I would, for the purpose of a test, change it to:

local function replayFunction(event) &nbsp;&nbsp;&nbsp; print("replayFunction: ", event.phase) &nbsp;&nbsp;&nbsp; if event.phase == "ended" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; director:changeScene("game") &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return true end

And make sure you’re getting to where you need to be.  I would also put a print in front of:

Runtime:addEventListener(“touch”,drawLine)

to make sure that line is getting re-executed when your scene reloads.  Director is not my specialty and I don’t see enough of your module code in the top post to know where that Runtime listener is being added.