拍摄对象直到屏幕结束,而不仅仅是直到光标位置

Shooting object until the end of the screen not just until cursor position

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"

如有任何帮助,我们将不胜感激!

丽芙:)

好的,所以首先:你不应该用 transition.to() 做这个动作。正如我假设您制作了一款可以发射激光炮弹的游戏。更简单的方法是将你当前在游戏中的所有射弹移动 velocity*dt(在它们当前的移动方向),其中 dt 是自上一帧以来经过的时间量。然后你可以检查它们是否不在操场上,如果是则删除它们。但是如果你真的想这样做...

天啊,我们开始了!

local function shoot(event)
    local width, height -- Size of playground
    local ex, ey = event.x, event.y -- Where player have clicked/tapped/etc
    local px, py = player.x, player.y -- Current position of player
    local speed -- Projectile speed in [pixels per milisecond]

    -- Our laser projectile
    local laser = display.newImage("Images/laser.png")
    laser.x, laser.y = player.x, player.y

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

    -- Let's get our target coordinates
    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

    -- Let's get animation time now!
    local distance = math.sqrt((tx-px)*(tx-px)+(ty-py)*(ty-py))
    local time = distance/speed

    -- Now, just shoot
    transition.to(laser, {time=time, x=tx, y=ty, onComplete=shotDone})
end