将屏幕可见范围内的物体射击至最后,不只是到光标位置

local function shoot( event )

    local x, y = event.x, event.y
    rotateShip(x, y)

    local laser = display.newImage("Images/laser.png")
    laser.x = player.x
    laser.y = player.y
    transition.to(laser, {time=300, x=x, y=y, onComplete=shotDone})

end

^ 这是我的代码到目前为止。它会将物体射击到点击位置。我想让它继续通过点击,直到它到达屏幕的边缘。为了方便起见,我已经将射击角度存储在一个变量中,我们将其称为“shotAngle”

任何帮助都将不胜感激!

Liv :)

点赞
用户4567755
用户4567755

首先,你不应该使用transition.to()来进行移动。我假设你制作了一个可以发射激光投射物的游戏。更简单的方法是通过velocity*dt(在它们当前的移动方向上)移动您当前在游戏中的所有投射物,其中dt是自上一帧以来经过的时间量。然后,您只需检查它们是否已经离开游戏区域,如果是则删除它们。但是,如果您真的想以这种方式做...

好了,我们开始吧!

local function shoot(event)
    local width, height -- 游戏区域的大小
    local ex, ey = event.x, event.y -- 玩家点击/轻击/等的地方
    local px, py = player.x, player.y -- 玩家当前的位置
    local speed -- 投射物速度,以[像素每毫秒]表示

    -- 我们的激光投射物
    local laser = display.newImage("Images/laser.png")
    laser.x, laser.y = player.x, player.y

    -- 边界:bx,by
    local bx, by = width, height
    if ex < px then
        bx = 0
    end
    if ey < py then
        by = 0
    end

    -- 让我们获得目标坐标
    local tx, ty = bx
    ty = ((py-ey)/(px-ex))*bx+py-((py-ey)/(px-ex))*px
    if ty > height or ty < 0 then
        ty = by
        tx = (by-py+((py-ey)/(px-ex))*px)/((py-ey)/(px-ex))
    end

    -- 现在让我们获得动画时间!
    local distance = math.sqrt((tx-px)*(tx-px)+(ty-py)*(ty-py))
    local time = distance/speed

    -- 现在,开火吧
    transition.to(laser, {time=time, x=tx, y=ty, onComplete=shotDone})
end
2016-08-22 01:12:56