2013-04-17 66 views
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 

當將圖像拖動到任何一個框架時,它會縮小並移除該對象並創建另一個圖像。對於此圖像觸摸不起作用...

我需要在圖像轉換競爭方法時使用觸摸動作進行圖像創建。我該怎麼辦?...

回答

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