我需要限制图像移动LOVE2D。
我正在使用Love2d和Lua制作游戏。目前,我有一个人物,可以从左到右“滑动”。我想要限制他的移动,以便他不会掉出屏幕。我尝试使用if语句检测他的X是否大于800(因为我的窗口大小为800x600),但它最终会导致出现故障...这是代码。请帮帮我?
```function love.load() love.graphics.setBackgroundColor(92,217,255) person={} person.image=love.graphics.newImage('/sprites/spriteTest.png') person.x=400 person.y=303 person.speed=200 hills=love.graphics.newImage('/sprites/spriteHills.png') end function love.update(dt)
if (person.x>735) then
if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
if (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
person.x=person.x+(person.speed*dt)
else
person.x=person.x
end
elseif (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
person.x=person.x+(person.speed*dt)
else
person.x=person.x
end
end
elseif (person.x<0) then
if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
if (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
person.x=person.x+(person.speed*dt)
else
person.x=person.x
end
elseif (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
person.x=person.x+(person.speed*dt)
else
person.x=person.x
end
end
else
if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
person.x=person.x+(person.speed*dt)
elseif (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
person.x=person.x-(person.speed*dt)
end
end
end function love.draw() love.graphics.draw(hills, 0, 0) love.graphics.draw(person.image, person.x, person.y) end```
我找到了答案。以下是代码
function love.update(dt)
if (player.x>735) then
if (love.keyboard.isDown('left') or love.keyboard.isDown('a') or love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
player.x=player.x-(player.speed*dt)
end
elseif (player.x<-10) then
if (love.keyboard.isDown('left') or love.keyboard.isDown('a') or love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
player.x=player.x+(player.speed*dt)
end
else
if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
person.x=person.x+(person.speed*dt)
elseif (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
person.x=person.x-(person.speed*dt)
end
end
end
它可能看起来似乎不能解决问题,但对我来说它解决了问题。我刚刚设置了它,如果他到达边缘,它将不允许他进一步前进。
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

你认为如何修改这个
update方法:function love.update(dt) if ((love.keyboard.isDown('right') or love.keyboard.isDown('d')) and person.x < 735) then person.x = person.x + person.speed * dt end if ((love.keyboard.isDown('left') or love.keyboard.isDown('a')) and person.x > 0) then person.x = person.x - person.speed * dt end end基本上你想要说的是
如果按下的是移动键并且对象可以移动就移动。另外我建议以精灵底部中心点作为旋转点。如果你有一个 64 × 64 的精灵,那么你需要使用
ox = 32和oy = 64(原点偏移)。