2013-07-27 123 views
1

我試過用Google搜索這個,但是教程只告訴你如何使彈丸過渡到某個目標。在我的情況下,我希望它向目標方向前進,但是直到屏幕邊緣。使彈丸過渡到屏幕邊緣

這是我目前拍攝功能:

local function shoot() 
--Invert coordinates 
local x = screenWidth - x 
local y = screenHeight - y 

--a2+b2=c2 
local sqDistance = ((x-centerX)*(x-centerX))+((y-centerY)*(y-centerY)) 
--Sqaure root it. 
local dist = mSqrt(sqDistance); 

--speed = d/t 
distTime = dist/BULLET_SPEED 
--if distTime is negative, this makes it positive 
if distTime < 0 then 
    distTime = distTime -distTime -distTime 
end 

--Create the bullet 
local shot = display.newRect(1,1,6,2) 
shot.x = centerX; shot.y = centerY; 
shot:setFillColor(240,200,0) 
shot.rotation = angleBetween+90;       
bulletGroup:insert(shot) 

--Remove bullet 
local function removeShot() 
    display.remove(shot) 
    shot = nil 
end  

--Move the bullet 
transition.to(shot, {time = distTime, x = x, y = y, onComplete = removeShot}) 


end 

回答

1

發現瞭如何做到這一點,不幸的是,似乎你必須使用物理庫。

當創建子彈加入這一行:

physics.addBody(shot,"dynamic") 

,並使用此而不是transition.to移動子彈:

--Move the bullet 
local xVelocity = (x-centerX)*BULLET_SPEED 
local yVelocity = (y- centerY)*BULLET_SPEED 
shot:setLinearVelocity(xVelocity,yVelocity)