Type Function Object StageObject Library display.* Return value none Revision Current Public Release (2013.2076) Keywords setFocus, focus, touch focus Sample code /CoronaSDK/SampleCode/ See also display.getCurrentStage()
Sets a specific DisplayObject as the target for all future hit (e.g. "touch", "tap") events. Pass
nil to restore default behavior for hit event dispatches. This is typically used to implement buttons that change appearance when a user initially presses them.
When calling this method while multi-touch is enabled (via
system.activate( "multitouch" )) with the optional parameter (
touchId) means that the specified touch has focus on that object, but other touches do not. Using this API, it is possible to create an object that will "own" the first touch it gets, for the lifetime of that touch, and for multiple objects to obtain their own focused touches at the same time. See the "Interface/DragMeMultitouch" sample code for a demonstration of this behavior.
With multi-touch enabled, focus will be on a per-object basis. To turn off focus, you must specify the object and pass
nil for the
When multi-touch is not enabled (default),
StageObject:setFocus() works as follows:
StageObject:setFocus( obj, nil ) behaves like
StageObject:setFocus( nil )
StageObject:setFocus( obj, touchID ) behaves like
StageObject:setFocus( obj )
As a result, library code that supports multitouch (e.g. widget.newButton()) will work as expected in both multi-touch and non-multitouch modes.
StageObject:setFocus( object [, touchId ] )
[Reference][api.type.Reference]. The touchId passed to the touch event for the touched object (when multi-touch is enabled).
local button = display.newRect( 50, 50, 100, 50 ) function doTouch( event ) if event.phase == "began" then print("began phase") event.target.alpha = 0.5 display.getCurrentStage():setFocus(event.target) elseif event.phase == "ended" or event.phase == "cancelled" then event.target.alpha = 1 print("end phase") display.getCurrentStage():setFocus(nil) end end button:addEventListener("touch", doTouch)