2014-05-02 56 views
0

好的,我正在創建一個應用程序,我已經創建了觸摸和拖動效果。我不知道如何編碼用戶放開物體的部分,它會飛起來,因此就是「扔」。Corona SDK中的「Throw」概念

local body = event.target 
local phase = event.phase 
local stage = display.getCurrentStage() 
--- 
if event.phase == "began" then 
    --begin focus 
    display.getCurrentStage():setFocus(self, event.id) 
    self.isFocus = true 
    self.markX = self.x -- store x location of object 
    self.markY = self.y -- store y location of object 
    physics.addBody(happy) 

elseif self.isFocus then 
    if event.phase == "moved" then 
     physics.addBody(happy) 
     -- drag touch object 

     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 
     -- end focus 
     display.getCurrentStage():setFocus(self, nil) 
     self.isFocus = false 

    end 
end 

return true 

再次,我試圖讓用戶能夠拋出對象,而不是隻是拖動它。此外,如果任何人都可以幫助觸摸並保持定時器,使對象消失並重生,那麼將非常感激。

回答

0

您可能會計算釋放時的速度:計算觸摸開始階段的觸摸位置(x1,y1)和觸摸結束階段的觸摸位置(x2,y2)以及兩者之間的時間:然後設置你的物體的線速度通過object.setLinearVelocity()這個(它是一個2D矢量)。如果身體是非靜態的,這將使其運動。動議將取決於它是動態還是動態。如果是動態的,速度將被設置,然後它將根據施加在物體上的外力(比如重力)而變化。例如:

local startTouchMove = 0 

.... 

local function touchListener(event) 

    if event.phase == "began" then 
     ... 
     self.markX = event.x -- store x location of object 
     self.markY = event.y -- store y location of object 
     startTouchMove = system.timer() 
     ... 

    elseif self.isFocus then 
     if event.phase == "moved" then 
      ... 

     elseif event.phase == "ended" then 
      -- set the instantaneous velocity based on touch motion 
      local dt = system.timer() - startTouchMove 
      if dt ~= 0 then     
       local velX = (event.x - self.markX)/dt 
       local velY = (event.y - self.markY)/dt 
       yourObject:setLinearVelocity(velX, velY)   
      end 
     end 
    end 
end 
+0

我該如何在這種情況下編碼? – user3596560

+0

@ user3596560已更新 – Schollii