2015-02-10 35 views
0

我有一个对象,我只是想上下移动。我怎么能让这个物体在没有实际接触物体的情况下上下移动呢?这里是我的代码至今:在日冕中拖动对象

function Scientist:touch(event) 
if event.phase == "began" then 

     display.getCurrentStage():setFocus( nil) 
     self.markY = self.y  
     self.isFocus = false 
elseif event.phase == "moved" then 

     local y = (event.y - event.yStart) + self.markY 
     self.y = y 

elseif event.phase == "ended" or event.phase == "cancelled" then 

     display.getCurrentStage():setFocus(nil) 

end 

     return true 

end 
     Scientist:addEventListener('touch', Scientist) 

回答

0

只需创建一个大矩形,设置透明度为0,使宽度display.contentWidth和高度display.contentHeight。当你触摸那个矩形时,让你以类似的方式移动科学家。

0

让Alpha设置整个页面上的一个矩形,以0.01:

local background = display.newRect(display.contentWidth/2, display.contentHeight/2, display.contentWidth, display.contentHeight) 

然后写一个触摸监听像你这样background:addEventListener('touch', background)

这里是完整的代码为您提供:

function Scientist:touch(event) 
    if event.phase == "began" then 
     display.getCurrentStage():setFocus( nil) 
     Scientist.markY = Scientist.y  
     self.isFocus = false 
    elseif event.phase == "moved" then  
     local y = (event.y - event.yStart) + Scientist.markY 
     Scientist.y = y  
    elseif event.phase == "ended" or event.phase == "cancelled" then  
     display.getCurrentStage():setFocus(nil)  
    end  
return true 
end 
Scientist:addEventListener('touch', Scientist)