2

我有一个角色对象,我可以用这段代码顺利拖动它,但是当我释放它时它并没有消除对它的关注。我怎样才能解决这个问题?

W=display.contentWidth
H=display.contentHeight

local character = display.newImage("moni.png")
character.x=W/2; character.y=H-50
character.type="character"

local function move(self, event)
if event.phase=="began" then
    --set focus on the moved object, so it won't interfere with other objects
    display.getCurrentStage():setFocus(self,event.id)
    self.isFocus=true
    --record first position of the object
    self.x0=self.x; self.y0=self.y
elseif event.phase=="moved" then
    self.y=self.y0; --we force the object not to change its first y location
    self.x=self.x0+(event.x-event.xStart) 
elseif event.phase=="canceled" or event.phase=="end" then
    display.getCurrentStage():setFocus(self, nil)
    self.isFocus=false
end 
return true
end 

character.touch=move
character:addEventListener("touch", character)
4

2 回答 2

1

代替:

event.phase=="end"

尝试:

event.phase=="ended"

有关详细信息,请访问基本交互和事件检测Setting Focus部分

继续编码...... :)

于 2013-07-23T17:32:38.880 回答
1

为了正确使用焦点,您应该使用这种功能:

function inBounds( event )
    local bounds = event.target.contentBounds
    if event.x > bounds.xMin and event.x < bounds.xMax and event.y > bounds.yMin and event.y  < bounds.yMax then
        return true
    end
    return false
end 


function touchHandler( event )
    if event.phase == "began" then
        -- Do stuff here --
        display.getCurrentStage():setFocus(event.target)
        event.target.isFocus=true
    elseif event.target.isFocus == true then
        if event.phase == "moved" then
            if inBounds( event ) then
                -- Do stuff here --
            else
                -- Do stuff here --
                display.getCurrentStage():setFocus(nil)
                event.target.isFocus=false
            end
        elseif event.phase == "ended" then
            -- Do stuff here --
            display.getCurrentStage():setFocus(nil)
            event.target.isFocus=false
        end
    end
end
于 2013-07-23T23:55:31.020 回答