1

我有动态图像创建的触摸问题。我的代码是

local widget = require "widget"

--Hide status bar
display.setStatusBar( display.HiddenStatusBar )

--bg image
local backgroundPortrait = display.newImage( "background.png", 0, 0 )

--local image1Group
--image1Group = display.newGroup()

--3 frames
local frame1 = display.newImageRect( "icon_1.png", 75, 75 )
frame1:setReferencePoint( display.CenterReferencePoint )
frame1.x = 160
frame1.y = 120

local frame2 = display.newImageRect( "icon_2.png", 75, 75 )
frame2:setReferencePoint( display.CenterReferencePoint )
frame2.x = 60
frame2.y = 360

local frame3 = display.newImageRect( "icon_3.png", 75, 75 )
frame3:setReferencePoint( display.CenterReferencePoint )
frame3.x = 260
frame3.y = 360

-- Center image
function createImage()
    centerFrame = display.newImageRect( "additionalvampire1.jpg", 75, 75 )
    centerFrame:setReferencePoint( display.CenterReferencePoint )
    centerFrame.x = display.contentCenterX
    centerFrame.y = display.contentCenterY
    centerFrame:addEventListener("touch", centerFrame)
end
createImage()
function removeTouchEventFun()
    centerFrame:removeEventListener("touch", centerFrame)
end

function transitionCompleted(centerFrame)
    print("transitionCompleted called")
    removeTouchEventFun()
    centerFrame:removeSelf()
    centerFrame=nil
    createImage()
end
function centerFrame:touch(event)
    if event.phase == "began" then
        self.markX = self.x    -- store x location of object
        self.markY = self.y    -- store y location of object
    elseif event.phase == "moved" then
        local x = (event.x - event.xStart) + self.markX
        local y = (event.y - event.yStart) + self.markY
        self.x, self.y = x, y    -- move object based on calculations above
    elseif event.phase == "ended" then
        if (centerFrame.x<=160  and centerFrame.y>=240) then
            transition.to( centerFrame, { time=1000, alpha=1, x=60, y=360, width=1 ,height=1, onComplete= transitionCompleted } )
        elseif (centerFrame.x>=160  and centerFrame.y>=240) then
            transition.to( centerFrame, { time=1000, alpha=1, x=260, y=360, width=1 ,height=1, onComplete= transitionCompleted } )
        else
            transition.to( centerFrame, { time=1000, alpha=1, x=160, y=120, width=1 ,height=1, onComplete= transitionCompleted } )
        end
    end
    return true
end

当将图像拖到任何一帧时,它会缩小并删除对象并创建另一个图像。对于此图像,触摸不起作用...

当图像转换竞争方法时,我需要通过触摸动作创建图像。我应该怎么办?...

4

1 回答 1

2
local centerFrame     

function transitionCompleted(centerFrame)
    print("transitionCompleted called")
    if(centerFrame) then 
        centerFrame:removeSelf()
        centerFrame=nil
        createImage()
    end

end

function onCenterFrameTouch(event)
    //your code here
end

function createImage()
    centerFrame = display.newImageRect( "additionalvampire1.jpg", 75, 75 )
    centerFrame:setReferencePoint( display.CenterReferencePoint )
    centerFrame.x = display.contentCenterX
    centerFrame.y = display.contentCenterY
    centerFrame:addEventListener("touch", onCenterFrameTouch)
end
于 2013-05-05T19:57:41.850 回答