1
我想使用transition.to函數發射子彈,我可以在物體的點擊事件上生成子彈,但是當我旋轉我的物體時,如何改變子彈被擊發的方向, 並且還改變了射擊方向上的角度。生成子彈的代碼如下... 請給我一些想法如何實現這一點功能....感謝如何使用transition.to發射子彈,並在發射光暈時改變子彈的角度和方向sdk
display.setStatusBar(display.HiddenStatusBar)
local function rotateObj(event)
local t = event.target
local phase = event.phase
if (phase == "began") then
display.getCurrentStage():setFocus(t)
t.isFocus = true
t.x1 = event.x
t.y1 = event.y
elseif t.isFocus then
if (phase == "moved") then
t.x2 = event.x
t.y2 = event.y
angle1 = 180/math.pi * math.atan2(t.y1 - t.y , t.x1 - t.x)
angle2 = 180/math.pi * math.atan2(t.y2 - t.y , t.x2 - t.x)
print("angle1 = "..angle1)
rotationAmt = angle1 - angle2
t.rotation = t.rotation - rotationAmt
print ("t.rotation = "..t.rotation)
t.x1 = t.x2
t.y1 = t.y2
elseif (phase == "ended") then
display.getCurrentStage():setFocus(nil)
t.isFocus = false
end
end
return true
end
local function shootfunc(event)
local getxpos=event.target.x
local getypos=event.target.y
local laser = display.newRect(1,1,10,35)
laser.x = getxpos
laser.y = getypos
laser:setFillColor(240,200,0)
transition.to(laser, { time = 800,x = 600, y = 20 })
end
local shot= display.newRect(1,1,40,100)
shot.x = 450; shot.y = 700
shot:setFillColor(240,200,0)
shot:addEventListener("touch", rotateObj)
shot:addEventListener("tap", shootfunc)