如何在Lua中使对象从上到下移动?

时间:2018-09-09 18:51:17

标签: lua position drawing

在我正在制作的游戏中,有一个正方形正在其中进行游戏。我希望生成的圆圈从顶部开始,然后向下移动到底部。在原始程序中,它们从左到右移动。我将如何处理?我不知道如何让它从顶部开始并向下移动,而不是从左侧开始并向右移动。

这是原始代码(很抱歉,我知道很多)。我正在尝试帮助一个朋友。

--Made by Joms or /u/jomy582
--Please credit me if using this in a battle.

spawntimer = 0
timerspawn = 0
storedtime = Time.time

bullets = {}
bulletsbig = {}
bulletswarn = {}
dir = 1
bigheight = {33, 98}

function Update()
  spawntimer = spawntimer + (Time.dt*Time.mult)
  timerspawn = timerspawn + (Time.dt*Time.mult)

  --normal bullets
  --change the number in the if statement to make them spawn more frequently/less frequently
  --EX: spawntimer > 0.2 spawns them pretty fast
  --EX2: spawntimer > 1 spawns them pretty slow
  --Make sure to change the subtraction method in the if statement
  if spawntimer > 0.16 then
    spawntimer = spawntimer-0.16
    local bullet = CreateProjectile("bullet", -Arena.width/2, math.random(-Arena.height/2, Arena.height/2))
    bullet.SetVar("deadly", true)
    table.insert(bullets, bullet)
  end

  --warning. spawns a warning every 5 seconds
  --You could change it, but that may cause some bugs
  if timerspawn > 2.2 then
    timerspawn = timerspawn-2.2
    dir = math.random(1,2)
    local bulletwarn = CreateProjectile("warning", 0, Arena.height/2 - bigheight[dir])
    bulletwarn.SetVar("warningtimer", 0)
    bulletwarn.SetVar("soundtimer", 0)
    bulletwarn.SetVar("animtimer", 0)
    bulletwarn.SetVar("anim", 1)
    table.insert(bulletswarn, bulletwarn)
  end

  --controlling normal bullets
  --a simple method that moves the bullets 1 pixel each frame
  --you can change it by editing the bullet.move
  --EX: bullet.Move(5*Time.mult, 0) moves the bullets 5 pixels each frame
  for i=1, #bullets do
    local bullet = bullets[i]
    if bullet.isactive then
      bullet.Move(2*Time.mult, 0)
      if bullet.y > -Arena.height/2 then
        bullet.Remove()
      end
    end
  end

  --controlling warning timer
  --a method that controls the warning timer
  for i=1, #bulletswarn do
    local bullet = bulletswarn[i]
    if bullet.isactive then
      local warningtimer = bullet.GetVar("warningtimer") + (Time.mult*Time.dt)
      local soundtimer = bullet.GetVar("soundtimer") + (Time.mult*Time.dt)
      local animtimer = bullet.GetVar("animtimer") + (Time.mult*Time.dt)
      local bulletSprite = bullet.sprite
      local anim = bullet.GetVar("anim")

      --flashing colors
      --change the animtimer > TIME where time is how often you want the warning to blink
      --warnings last for 3 seconds. Can change that as well
      --to get different colors, find the rgb value of the color you want and insert them below
      if animtimer > 0.08 then
        animtimer = animtimer-0.08
        if anim == 1 then
          local r = 0 --put Red value here
          local g = 0 --put Green value here
          local b = 169 --put Blue value here
          bulletSprite.color = {r/255, g/255, b/255} -- changes the color to whatever you'd like. Uses RGB.
          bullet.SetVar("anim", 2)
        elseif anim == 2 then
          local r = 0 --put Red value here
          local g = 0 --put Green value here
          local b = 255 --put Blue value here
          bulletSprite.color = {r/255, g/255, b/255} -- changes the color to whatever you'd like. Uses RGB.
          bullet.SetVar("anim", 1)
        end
      end

      --plays a timer evert 10 frames for 3 seconds.
      --change the soundname to change the sound
      --change the soundtimer > 0.16 to change how often it plays
      --can change how long it lasts as well by changing the less than statement
      if soundtimer > 0.10 then
        soundtimer = soundtimer-0.10
        Audio.PlaySound("alarm")
      end

      --this controls when to spawn the bullets
      --change the statement to change how long the warning timer is
      if warningtimer > 2 then
        warningtimer = warningtimer-2
        Audio.PlaySound("shoot")
        local bullet1 = CreateProjectile("leftbigbullet", Arena.width/2+30, Arena.height/2 - bigheight[dir]) --where to spawn them
        bullet1.SetVar("speed", -4) --how fast
        bullet1.SetVar("deadly", true)
        local bullet2 = CreateProjectile("rightbigbullet", -Arena.width/2-30, Arena.height/2 - bigheight[dir]) --where to spawn them
        bullet2.SetVar("speed", 4) --how fast
        bullet2.SetVar("deadly", true)
        table.insert(bulletsbig, bullet1)
        table.insert(bulletsbig, bullet2)
        bullet.Remove()
      end

      bullet.SetVar("warningtimer", warningtimer)
      bullet.SetVar("animtimer", animtimer)
      bullet.SetVar("soundtimer", soundtimer)
    end
  end

  --controlling big bullets
  --this method controls the big bullets
  for i=1, #bulletsbig do
    local bullet = bulletsbig[i]
    if bullet.isactive then
      local speed = bullet.GetVar("speed")
      bullet.SendToBottom()
      bullet.Move(speed*Time.mult, 0)
      if bullet.absx > 700 or bullet.absx < -70 then
        bullet.Remove()
      end
    end
  end

end

function OnHit(bullet)
    if(bullet.getVar("deadly")) then
    Player.Hurt(3)
    end
end

0 个答案:

没有答案