如何在Corona SDK中根据手指滑动的方向发射子弹

我正在用 Corona 开发一个游戏。当用户在某个对象上滑动手指时,我希望能发射一颗子弹。滑动的距离越远,子弹应该飞得越远。我已经编写了代码,在 "tap" 事件中子弹可以发射。其中,有两个函数 "start projecttile" 和 "game loop",前者用于发射子弹,后者用于移动武器。但我不知道如何使用手指滑动来瞄准目标。请给我一些建议,谢谢…

已经实现的部分代码如下:

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
点赞
用户2238176
用户2238176

你可以使用内置的 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 } )

你可以将变量从 swipe Y 更改为 Corona 所使用的任何变量(我不知道它的代码)。 我假设你知道如何移动子弹,但如果不知道,可以使用以下代码:

for i, v in ipairs( bullet ) do
    bullet.x = bullet.x + bullet.dx * dt
    bullet.y = bullet.y + bullet.dy * dt
end
2013-07-17 13:23:12