爱2D简单绘制交换图片。
2013-12-10 19:33:18
收藏:0
阅读:103
评论:3
我对 Lua 和 Love2D 完全是新手,可能根本不理解概念。 好吧,这是一个 Love2D 教程,我想改动它,让当我按下键盘上的“a”键时,对象就会交换(从大腿鼠变成汽车),以此类推。
你能帮帮我吗?
-- 教程 1:大腿鼠球
-- 将一张图片添加到游戏中,使用箭头键移动它。
-- 兼容 löve 0.6.0 及以上版本
function love.load()
hamster = love.graphics.newImage("hamster.png")
auto = love.graphics.newImage("auto.png")
x = 50
y = 50
speed = 300
end
function love.update(dt)
if love.keyboard.isDown("right") then
x = x + (speed * dt)
end
if love.keyboard.isDown("left") then
x = x - (speed * dt)
end
if love.keyboard.isDown("down") then
y = y + (speed * dt)
end
if love.keyboard.isDown("up") then
y = y - (speed * dt)
end
if love.keyboard.isDown("escape") then
love.event.quit()
end
if love.keyboard.isDown("a") then
love.graphics.draw(auto,x,y)
else
love.graphics.draw(hamster, x, y)
end
end
function love.draw()
love.graphics.draw(hamster, x, y)
end
点赞
用户2610652
感谢Corbin,我在没有“state”本地变量的情况下想出来了。你的解决方案对我很有启发,现在它可以工作了。
-- 教程1: 仓鼠球
-- 在游戏中添加图像并使用箭头键移动它。
-- 与löve 0.6.0兼容
function love.load()
hamster = love.graphics.newImage("hamster.png")
auto = love.graphics.newImage("auto.png")
activeImage = hamster
activeImageName = "hamster"
x = 50
y = 50
speed = 300
end
function love.update(dt)
if love.keyboard.isDown("right") then
x = x + (speed * dt)
end
if love.keyboard.isDown("left") then
x = x - (speed * dt)
end
if love.keyboard.isDown("down") then
y = y + (speed * dt)
end
if love.keyboard.isDown("up") then
y = y - (speed * dt)
end
if love.keyboard.isDown("escape") then
love.event.quit()
end
if love.keyboard.isDown("a") then
activeImage = auto
activeImageName = "auto"
end
if love.keyboard.isDown("h") then
activeImage = hamster
activeImageName = "hamster"
end
end
function love.draw()
love.graphics.draw(activeImage, x, y)
end
2013-12-10 15:18:17
用户15890676
这可能不是解决这个问题的最佳方式,但它能够工作——创建一个对象,该对象具有 X 值、Y 值、速度值和 IMG 值。将 X 和 Y 设为 50 和 50,速度设为 300,IMG 设为 nil。在 load 函数中,将 IMG 值设置为您希望它开始的任何图像。在 update 函数中,检查是否有按键按下,当按键按下时,将 IMG 值更改为新图像。
下面是一个示例脚本以作说明:
player = {X = 50, Y = 50, speed = 300, IMG = nil}
function love.load()
hamster = love.graphics.newImage('hamster.png')
auto = love.graphics.newImage('auto.png')
player.IMG = hamster
end
function love.update(dt)
if love.keyboard.isDown('d') then
player.x = player.x + player.speed
elseif love.keyboard.isDown('a') then
player.x = player.x - player.speed
end
if love.keyboard.isDown('w') then
player.y = player.y + player.speed
elseif love.keyboard.isDown('s') then
player.y = player.y - player.speed
end
if love.keyboard.isDown('space') then
if player.IMG == hamster then
player.IMG = auto
else
player.IMG = hamster
end
end
end
function love.draw()
love.graphics.draw(player.IMG, player.X, player.Y)
end
希望这可以帮助您!
2023-02-12 18:22:12
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的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 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
我建议使用
love.update只更新状态,不要在其中绘制图形。然后在love.draw中进行所有的绘制。一个解决方案可能是:local state = {} function love.load() hamster = love.graphics.newImage("hamster.png") auto = love.graphics.newImage("auto.png") state.activeImage = hamster state.activeImageName = "hamster" -- <snip> ... end function love.update(dt) -- <snip> ... if love.keyboard.isDown("a") then if state.activeImageName == "hamster" then state.activeImage = auto state.activeImageName = "auto" else state.activeImage = hamster state.activeImageName = "hamster" end end end function love.draw() love.graphics.draw(state.activeImage, x, y) end