Lua获取游戏中玩家的 (x,y) 坐标

我想实时找到我的玩家的 x 和 y 坐标,以便知道在哪里制作我的游戏的下一级。我目前使用 LÖVE 2D 运行我的代码。当我尝试打印 player.xplayer.y 时,游戏虽然没有文本输出坐标,但运行正常。我尝试改变文本位置的位置,但效果不佳。任何帮助都会受到赞赏。注意:我今天刚学 Lua,请直截了当地说。 :)

love.graphics.setDefaultFilter('nearest','nearest')
function love.load()
  room1Image = love.graphics.newImage('room1.png')
  room2Image = love.graphics.newImage('room2.png')
  room3Image = love.graphics.newImage('room3.png')
  room1 = true
  room2 = false
  room3 = false
  player = {}
  player.x = 0
  player.y = 255
  player.speed = 5
  player.image = love.graphics.newImage('player.png')
end

function love.update(dt)
  if love.keyboard.isDown("left") then
    player.x = player.x - 5
  end
  if love.keyboard.isDown("right") then
    player.x = player.x + 5
  end
  if love.keyboard.isDown("up") then
    player.y = player.y - 5
  end
  if love.keyboard.isDown("down") then
    player.y = player.y + 5
  end
  if player.y >= 600 and room1 then
    room1 = false
    room2 = true
    player.y = 5
  end
  if player.y <= 0 and room2 then
    room1 = true
    room2 = false
    player.y = 600
  end
  if player.y >= 600 and room2 then
    room2 = false
    room3 = true
    player.y = 5
  end
  if player.y <= 0 and room3 then
    room2 = true
    room3 = false
    player.y = 600
  end

end

function love.draw()
  --画背景
  if room1 then
    love.graphics.draw(room1Image, room1Image.x, room1Image.y)
  elseif room2 then
    love.graphics.draw(room2Image, room2Image.x, room2Image.y)
  elseif room3 then
    love.graphics.draw(room3Image, room3Image.x, room3Image.y)
  end
  --画玩家
  love.graphics.draw(player.image, player.x, player.y, 0, 5)
  end
点赞
用户6575359
用户6575359

如果你想要将某些内容输出到控制台,请使用 print()。这将不会在游戏窗口中可见。

如果你想要向玩家显示一些文本(在游戏中),请在 love.draw() 中调用 love.graphics.print

local x,y = 0, 0 --文本的打印坐标
function love.load()
end

function love.update(dt)
end

function love.draw()
  love.graphics.print("这是我想让你看到的一些内容。", x, y)
end
2018-02-19 09:50:10