如何在corona sdk中向手指滑动的方向发射子弹



我正在科罗纳开发一款游戏。当用户用手指在物体上滑动时,我想发射一颗子弹。他挥得越远,子弹应该打得越远。我研究过这个和点击事件,子弹被发射,有两个功能启动弹和游戏循环,启动弹用于发射弹,游戏循环用于翻译武器,但我不明白如何使用手指滑动瞄准目标。请给我任何建议,谢谢。。。到目前为止已经实现的代码低于

local function startprojectile(event)     
gameIsActive=true
local firetimer
local getobj=event.target
local getxloc=getobj.x
local getyloc=getobj.y
local function firenow()
if gameIsActive=false then
timer.cancel(firetimer)
firetimer=nil
else
local bullet=display.newImageRect("object.png",50,60);
bullet.x=getxloc-50; bullet.y=getyloc-50
bullet.type=bullet1
physics.addBody(bullet,{isSensor=true})
weaponGroup:insert(bullet)
end
gameIsActive=false
end
firetimer.timer.performWithDelay(650,firenow)
end
local function gameloop()                 
local i
for i=weaponGroup.numChildren,1,-1 do
local weapon=weaponGroup[i]
if weapon ~=nil and weapon.x ~=nil
then 
weapon:translate(-20,0)
end
end

您可以使用内置的lua函数来获得子弹应该发射的角度

startX, startY = player.x, player.y
dir = math.atan2(( swipeY - player.y ), ( swipeX - player.x ))
bulletDx = bullet.speed * math.cos(dir)
bulletDy = bullet.speed * math.sin(dir)
table.insert( bullet, { x = startX, y = startY, dx = bulletDx, dy = bulletDy } )

您可以将变量从滑动Y更改为Corona使用的任何变量(我不使用它进行编码)。我假设你知道如何移动子弹,但如果不知道,

for i, v in ipairs( bullet ) do
    bullet.x = bullet.x + bullet.dx * dt
    bullet.y = bullet.y + bullet.dy * dt
end

最新更新