【问题标题】:How to fire a bullet in the direction of finger swipe in corona sdk如何在电晕sdk中向手指滑动方向发射子弹
【发布时间】:2013-07-15 00:37:36
【问题描述】:

我正在开发一款电晕游戏。当用户在对象上滑动手指时,我想发射子弹。他扫得越远,子弹应该走得越远。我对此进行了研究,并在点击事件时发射了子弹,有两个功能启动弹丸和游戏循环,开始弹丸用于发射弹丸,游戏循环用于翻译武器,但我不明白如何瞄准使用手指滑动的目标。请给我任何建议,谢谢... 到目前为止已经实现的代码如下

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 coronasdk


【解决方案1】:

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

【讨论】:

  • 你能告诉我在我的代码中我必须把你建议的代码放在哪里,因为我是电晕新手...谢谢
  • 就像我说的,我不知道电晕,但我会把它放在我的发射子弹的函数中。然后将其放入一个检测手指按下位置的函数中。可能类似于:corona.getX() 或类似的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-07
  • 2018-03-29
  • 1970-01-01
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
相关资源
最近更新 更多