将物体围绕中心旋转
2013-6-14 0:44:47
收藏:0
阅读:139
评论:2
事情是这样的,我在我的游戏中有一门大炮,我想用手指来转动它。我希望炮口始终指向手指拖动的相反方向,即如果手指向左下拖动,则炮口应该指向右上。
我只是无法弄清楚如何使它正常工作。我已经成功地使用以下代码将其旋转:
local function rotateObj(event)
local t = event.target
local phase = event.phase
if (phase == "began") then
display.getCurrentStage():setFocus(t)
t.isFocus = true
-- 存储手指的初始位置
t.x1 = event.x
t.y1 = event.y
elseif t.isFocus then
if (phase == "moved") then
t.x2 = event.x
t.y2 = event.y
angle1 = 180 / math.pi * math.atan2(t.y1 - t.y,t.x1 - t.x)
angle2 = 180 / math.pi * math.atan2(t.y2 - t.y,t.x2 - t.x)
print("angle1 = "..angle1)
rotationAmt = angle1 - angle2
-- 旋转
t.rotation = t.rotation - rotationAmt
print("t.rotation = "..t.rotation)
t.x1 = t.x2
t.y1 = t.y2
elseif (phase == "ended") then
display.getCurrentStage():setFocus(nil)
t.isFocus = false
end
end
-- 阻止触摸事件的进一步传播
return true
end
cannon:addEventListener("touch", rotateObj)
虽然这确实允许我旋转我的大炮,但它不会保持炮口与我拖动的位置的关系。我甚至不知道该从哪里开始。
点赞
用户1212870
看一下你的第一个 if else if 语句:
if (phase == "began") then
t.isFocus = true
elseif t.isFocus then
XXX
end
当 phase == "began" 时,它将不起作用,因为 if else if 语句只会进入其中一条路径。在你的 if 语句中,你将 t.isFocus 设置为 true,但是它在 elseif 路径中不会立即被判断。
2013-06-14 01:25:43
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的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中获取用户配置主目录的跨平台方法
你可以试试我的代码。
我也创建了一个类似于你的游戏,但你是控制弓箭的。
function getAngle(x1,y1,x2,y2) local PI = 3.14159265358 local deltaY = y2 - y1 local deltaX = x2 - x1 local angleInDegrees = (math.atan2( deltaY, deltaX) * 180 / PI)*-1 local mult = 10^0 return math.floor(angleInDegrees * mult + 0.5) / mult end local arrow = display.newImage("wind_arrow.png") arrow.x = display.contentWidth/2 arrow.y = display.contentHeight/2 arrow.touch = function(self, event) print(event.phase) if event.phase == "moved" then -- 如果你的图片原本朝向北方,则使用 +90 -- 如果你的图片原本朝向东方,则使用 +180 -- 如果你的图片原本朝向南方,则使用 +270 -- 如果你的图片原本朝向西方,则使用 +360 或 +0 -- 我的 wind_arrow.png 是朝向东方的,因此我使用 +180 -- 你可以使用这个公式 arrow.rotation = (getAngle(arrow.x,arrow.y,event.x,event.y)+180)*-1 end end Runtime:addEventListener("touch",arrow)