David Gruet
David Gruet

Reputation: 23

How to define a "non-touch event listener" in Corona SDK?

I have a problem programming a game with Corona SDK. I have different objects (stored in tables) falling down and a "touch" event listener to define what to do when an object is touched. But I also want to define actions when an object has NOT been touched after falling below the bottom of the screen. I tried several methods and every one is "working" (no error generated) but the defined actions are not executed... Is there a way to define a sort of "non-touch event listerner" ?? Here is the listerner part of my code (in this example, the code is working but "Game Over" is not printed).

Many thanks for your help !

local function BlackBalloon ()

local Black = display.newImageRect("BlackBallon.png", 80, 120)

function Black:touch (event)
    if event.phase == "began" then
        score = score + 1
        print ( score )

    elseif Black.y >= 540 then
        print ( "Game Over" )
    end
return true
end

Black:addEventListener( "touch", Black )
return Black
end

Upvotes: 2

Views: 264

Answers (1)

Nicklas Kevin Frank
Nicklas Kevin Frank

Reputation: 6337

There is no such a thing as a "purity" checker of an object. You can simulate this by simply having a variable on the created object Black.touched = false when you create it and changing this to true once it has been touched.

I am going to give you the basic functionality to get this work - you will have to add a variable on the Black object when you detect a touch, setting the variable ex. Black.touched = true and checking for that variable in the gameOverDetection function.

Add this function in your code.

-- Function to handle detection.
local function gameOverDetection()
    if Black.y > 540 then
        print ("Game Over")
    end
end

Add this listener at the bottom of your code

-- Listener to check on each entered frame.
Runtime:addEventListener("enterFrame",gameOverDetection)

To explain why your own code won't work, the function Black:touch(event) is only called when a touch event happens on an object. So course of action is

  1. Black gets touched
  2. You check if the touch is new (with began)
  3. If it is, you add the score + 1
  4. If it is NOT new you check if Black's Y value is larger than or equal to 540.
  5. Program Continues.

You are never going to get a situation where you touch an object where the second condition >= 540 is met and the second condition will only ever get met if you "hold" your finger on an object.

Upvotes: 1

Related Questions