使射弹过渡到屏幕边缘

时间:2013-07-27 16:54:32

标签: corona transition projectile

我已经尝试使用谷歌搜索,但是教程只告诉你如何将射弹过渡到某个目标。在我的情况下,我希望它向目标移动,然后继续直到屏幕的边缘。

这是我目前的拍摄功能:

local function shoot()
--Invert coordinates 
local x = screenWidth - x
local y = screenHeight - y

--a2+b2=c2
local sqDistance = ((x-centerX)*(x-centerX))+((y-centerY)*(y-centerY)) 
--Sqaure root it.
local dist = mSqrt(sqDistance); 

--speed = d/t
distTime = dist/BULLET_SPEED
--if distTime is negative, this makes it positive
if distTime < 0 then 
    distTime = distTime -distTime -distTime
end 

--Create the bullet
local shot = display.newRect(1,1,6,2)
shot.x = centerX; shot.y = centerY; 
shot:setFillColor(240,200,0)
shot.rotation = angleBetween+90;                        
bulletGroup:insert(shot)

--Remove bullet 
local function removeShot()
    display.remove(shot)
    shot = nil
end     

--Move the bullet
transition.to(shot, {time = distTime, x = x, y = y, onComplete = removeShot})


end

2 个答案:

答案 0 :(得分:1)

发现如何做到这一点,不幸的是你似乎必须使用物理库。

创建项目符号时添加以下行:

physics.addBody(shot,"dynamic") 

使用此而不是transition.to移动子弹:

--Move the bullet
local xVelocity = (x-centerX)*BULLET_SPEED
local yVelocity = (y- centerY)*BULLET_SPEED
shot:setLinearVelocity(xVelocity,yVelocity)

答案 1 :(得分:0)

通过将object.isBullet = true设置为子弹来使身体成为子弹。

请参阅此处的参考http://docs.coronalabs.com/api/type/Body/isBullet.html

相关问题