使用Corona SDK实现敌人上下移动。

我希望我的鸟在玩耍时能够上下移动。以下是我的代码:

function updateMons2()
      for a = 1, mons2.numChildren, 1 do
        physics.addBody(mons2[a],"kinematic")
          if(mons2[a].isAlive == true) then
              (mons2[a]):translate(speed * -1, 0)

              if(mons2[a].x < -80) then
                  mons2[a].x = 1000
                  mons2[a].y = 500
                  mons2[a].isAlive = false
              end
          end
      end
  end

这段代码只能使物体从右到左运动,我希望我的鸟在向左移动的同时能上下移动。能否有人帮我解决这个问题?

点赞
用户1979583
用户1979583

下面是示例,请尝试一下:

local mons2 = {}
local yPos = {}
for i=1,2 do
  mons2[i] = display.newImageRect("1.png",50,50)
  mons2[i].x = 100
  mons2[i].y = 100+(100*(i-1))
  mons2[i].isAlive = true
  yPos[i] = mons2[i].y
end

speed = 10
count_ = 0
function updateMons2()
  count_ = count_ + 1
  for a = 1, 2, 1 do
    physics.addBody(mons2[a],"kinematic")
     if(mons2[a].isAlive == true) then
       mons2[a]:translate(speed * -1, 0)
       transition.to(mons2[a],{time=50,y=yPos[a]+(20*(count_%2)*-1)})
         if(mons2[a].x < -80) then
           mons2[a].x = 350
         end
     end
  end
end
timer.performWithDelay(100,updateMons2,-1)

继续写代码... :)

2013-08-12 10:33:36