我不知道如何让基本的碰撞工作
I don't know how to get basic collision working
我是 Love2D 的新手,Lua 但到目前为止一切顺利。我正在尝试制作一个简单的游戏,但到目前为止,除了让玩家不滚动屏幕外,我还没有找到任何关于碰撞的东西。我一直在尝试使用这个
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
但我不完全确定如何使用它,因为每次我都尝试在程序中进行任何更改。请帮忙?
我对这个网站不太熟悉,抱歉。我改了还是不行
tree = {x = 20, y = 20, speed = 0, img = nil }
tree.img = love.graphics.newImage("assets/tree.png")
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 or
x2 < x1+w1 or
y1 < y2+h2 or
y2 < y1+h1
end
function tree_update(dt)
if CheckCollision(crs.x, crs.y, crs.img:getWidth(), crs.img:getHeight(), tree.x, tree.y, tree.img:getWidth(), tree.img:getHeight()) then
love.graphics.print("It touched the thing", love.graphics:getWidth()/2- 50, love.graphics:getHeight()/2-10)
end
end
function tree_draw()
love.graphics.draw(tree.img, tree.x, tree.y)
end
return 语句应该使用 or
而不是 and
因为你希望它 return 当任何语句为真时它已经发生碰撞。
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 or
x2 < x1+w1 or
y1 < y2+h2 or
y2 < y1+h1
end
Love2D 中还有一个名为“Contacts”的碰撞功能,可能有助于解决碰撞等问题。
我是 Love2D 的新手,Lua 但到目前为止一切顺利。我正在尝试制作一个简单的游戏,但到目前为止,除了让玩家不滚动屏幕外,我还没有找到任何关于碰撞的东西。我一直在尝试使用这个
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
但我不完全确定如何使用它,因为每次我都尝试在程序中进行任何更改。请帮忙?
我对这个网站不太熟悉,抱歉。我改了还是不行
tree = {x = 20, y = 20, speed = 0, img = nil }
tree.img = love.graphics.newImage("assets/tree.png")
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 or
x2 < x1+w1 or
y1 < y2+h2 or
y2 < y1+h1
end
function tree_update(dt)
if CheckCollision(crs.x, crs.y, crs.img:getWidth(), crs.img:getHeight(), tree.x, tree.y, tree.img:getWidth(), tree.img:getHeight()) then
love.graphics.print("It touched the thing", love.graphics:getWidth()/2- 50, love.graphics:getHeight()/2-10)
end
end
function tree_draw()
love.graphics.draw(tree.img, tree.x, tree.y)
end
return 语句应该使用 or
而不是 and
因为你希望它 return 当任何语句为真时它已经发生碰撞。
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 or
x2 < x1+w1 or
y1 < y2+h2 or
y2 < y1+h1
end
Love2D 中还有一个名为“Contacts”的碰撞功能,可能有助于解决碰撞等问题。