object:removeEventListener()

Type Function
Object EventListener
Return value none
Revision Current Public Release (2014.2393)
Keywords removeEventListener, event listener
See also object:addEventListener()

Overview

Removes the specified listener from the object's list of listeners so that it no longer is notified of events corresponding to the specified event.

Gotchas

Note that nil is a valid value for listener under certain circumstances so it is not an error to call removeEventListener with a nil listener. This can lead to hard to find bugs if you accidentally use the wrong name for the listener you are trying to remove.

Gotchas

Note that nil is a valid value for listener under certain circumstances so it is not an error to call removeEventListener with a nil listener. This can lead to hard to find bugs if you accidentally use the wrong name for the listener you are trying to remove.

Syntax

object:removeEventListener( eventName, listener )
eventName (required)

String. Specifies the name of the event whose corresponding listener should be removed from the list.

listener (required)

Listener. Reference to the listener to remove from the list.

Example

-- define motion and limits
local DX, DY = 3, 3
local XMIN, XMAX = 1, display.contentWidth-1
local YMIN, YMAX = 20, display.contentHeight-1

-- create a shape 
local button = display.newRoundedRect( XMIN, YMIN, 150, 50, 5 )
button:setFillColor( 0.4, 0, 1 )
button.strokeWidth = 6
button:setStrokeColor( 0.8 )

-- move the shape until it hits the limits
local function onFrame (event)
    if button.x + button.width/2 > XMAX or button.x < XMIN 
    or button.y + button.height/2 > YMAX or button.y < YMIN then
        -- stop
        Runtime:removeEventListener( "enterFrame", onFrame )
    else
        -- move
        button.x = button.x + DX
        button.y = button.y + DX
    end
end

-- update the position every frame
Runtime:addEventListener( "enterFrame", onFrame )