LOVE2D中实现一个简单的摇杆
来源:互联网 发布:mac的flash屏蔽 编辑:程序博客网 时间:2024/06/05 14:10
转自:http://blog.csdn.net/jkevin2016/article/details/52885137
local local_class = class() --这里用到云风的CLASS代码local gr=love.graphicslocal function getDistance (pointO,pointA ) distance = math.pow((pointO.x - pointA.x),2) + math.pow((pointO.y - pointA.y),2); distance = math.sqrt(distance); pointO,pointA=nil,nil return distance; endfunction local_class:ctor(bg,spr) self.bg = bg self.spr = spr self.dir = 0 self.renge = 0 self.power = 0 self.w2 = self.bg:getWidth()/2 self.point = {x=0,y=0} self.offset = {x=0,y=0} self.press = falseendfunction local_class:update()endfunction local_class:setPoint(x,y) self.point.x ,self.point.y = x,yendfunction local_class:draw() gr.draw(bg,self.point.x,self.point.y,0,1,1,110,110) gr.draw(center,self.point.x+self.offset.x,self.point.y+self.offset.y,0,1,1,48,48)endfunction local_class:touchpressed( id, x, y, dx, dy, pressure ) local radius = self.w2 local dis = getDistance({x=x,y=y},self.point); if dis < radius then self.press = true self.offset.x = (x-self.point.x) self.offset.y = (y-self.point.y) endendfunction local_class:touchreleased( id, x, y, dx, dy, pressure ) if self.press then self.offset.x = 0 self.offset.y = 0 end self.press = false self.dir = 0endfunction local_class:touchmoved( id, x, y, dx, dy, pressure ) if self.press then local radius = self.w2 local dis = getDistance({x=x,y=y},self.point); local angle = math.acos ( (x - self.point.x)/dis ); if dis>radius then if ( y > self.point.y ) then self.offset.x = radius*math.cos(angle) self.offset.y = radius*math.sin(angle) else self.offset.x = radius*math.cos(angle) self.offset.y = -(radius*math.sin(angle)) end else self.press = true self.offset.x = (x-self.point.x) self.offset.y = (y-self.point.y) end if dis > self.renge then if (y > self.point.y) then self:__checkDirection(angle); else self:__checkDirection(-angle); end else self.dir = 0 end self.power = dis / radius endendfunction local_class:getDirection() return self.direndfunction local_class:__checkDirection(angle) local dir = 0 local M_PI=math.pi if (angle >= -M_PI/8.0 and angle <= M_PI/8.0) then dir = 6; end --[[右边走 -八分之派 到 八分之派--]] if (angle >= -(M_PI/8.0) and angle <= M_PI/8.0) then dir = 6; --[[右上方向 八分之派 到 八分之三派--]] elseif( angle >= M_PI/8.0 and angle < 3*M_PI/8.0) then dir = 3; --[[上方向 八分之三派 到 八分之五派--]] elseif( angle >= 3*M_PI/8.0 and angle <= 5*M_PI/8.0) then dir = 2; --[[左上方向 八分之5派 到 八分之七派--]] elseif( angle > 5*M_PI/8.0 and angle < 7*M_PI/8.0) then dir = 1; --[[左方向--]] elseif( (angle >= 7*M_PI/8.0 and angle <= M_PI) or (angle <= -7*M_PI/8.0 and angle >= -M_PI)) then dir = 4; --[[左下方向--]] elseif( angle > -7*M_PI/8.0 and angle < -5*M_PI/8.0) then dir = 7; --[[下方向--]] elseif( angle >= -5*M_PI/8.0 and angle <= -3*M_PI/8.0) then dir = 8; --[[右下方向--]] elseif( angle > -3*M_PI/8.0 and angle < -M_PI/8.0 ) then dir = 9; end self.dir = direndreturn local_class
这里有个小例子
链接: http://pan.baidu.com/s/1boQcpVT 密码: drsm
阅读全文
0 0
- LOVE2D中实现一个简单的摇杆
- Unity中虚拟摇杆的简单实现
- Ogre+ UIKit GUI 摇杆功能的简单实现
- Unity中实现摇杆功能
- 摇杆的简单使用 Unity3d
- Unity简单的摇杆制作
- Unity3D中关于游戏摇杆的实现方法
- LOVE2D-03-完整的LOVE2D程序
- 手游摇杆(一)最简单的四方向摇杆
- UGUI实现的变速摇杆
- UGUI 虚拟摇杆的实现
- unity虚拟摇杆的实现
- 摇杆是如何实现的
- 教你一步步实现一个虚拟摇杆
- Unity3D 双摇杆 c# JoyStick 实现自己的双摇杆
- 如何在unity里创建一个简单酷炫的摇杆
- Unity3d中利用NGUI实现虚拟摇杆
- 请教一个EasyTouch的问题,如何实现摇杆和之外的触模区域独立
- 移动web开发
- Android WebView通过代码修改网页的字体大小
- VideoRangeSlider视频裁剪控件
- java需要关注的知识点---LinkedList
- 使用WinINet实现HTTP下载
- LOVE2D中实现一个简单的摇杆
- 二叉树的下一个节点
- 在hibernate中同一个session里面有了两个相同标识但是是不同实体。
- Java 创建文件夹路径
- 移动web开发--Hello world
- Android使用系统方法实现分享到QQ和微信!
- 微信小程序入门与实战 常用组件 API 开发技巧 项目实战
- linux定时计划的学习
- androdi 数字验证码