您好,我正在玩 Lua/Love2D,我想知道是否可以同时使用网格和 love.body。我想制作和测试网格,我还想制作一个 world = love.physics.newWorld 来设置重力 0、0 并使用 love.body:applyForce 给它一种类似空间的感觉。
所以现在我有播放器
<code>
player = {}
player.gridx = 64
player.gridy = 64
player.acty = 200
player.speed = 32
</code>
因为我正在使用网格,但我也想添加
<code>
world = love.physics.newWorld(0, 0, true)
In love.load()
</code>
然后在球员班
<code>
player = {}
player.body = love.physics.newBody(world, 200, 550, "dynamic")
player.body:setMass(100) -- make it pretty light
player.shape = love.physics.newRectangleShape(0, 0, 30, 15)
player.fixture = love.physics.newFixture(player.body, player.shape, 2)
player.fixture:setRestitution(0.4) -- make it bouncy
</code>
然后使用
<code>
if love.keyboard.isDown("right") then
player.body:applyForce(10, 0.0)
print("moving right")
elseif love.keyboard.isDown("left") then
player.body:applyForce(-10, 0.0)
print("moving left")
end
if love.keyboard.isDown("up") then
player.body:applyForce(0, -500)
elseif love.keyboard.isDown("down") then
player.body:applyForce(0, 100)
end
</code>
代替
<code>
function love.keypressed(key)
if key == "up" then
if testMap(0, -1) then
player.gridy = player.gridy - 32
end
elseif key == "down" then
if testMap(0, 1) then
player.gridy = player.gridy + 32
end
elseif key == "left" then
if testMap(-1, 0) then
player.gridx = player.gridx - 32
end
elseif key == "right" then
if testMap(1, 0) then
player.gridx = player.gridx + 32
end
end
end
</code>