ios joystick 虚拟摇杆实现 ( 非 Cocos2d )
来源:互联网 发布:circrna数据库 编辑:程序博客网 时间:2024/05/17 22:50
本文 Demo 代码已经上传 Github
https://github.com/pyzhangxiang/joystick-iosiphone
ios 上控制人物的移动,一是可以利用加速传感器计算设备的倾角来控制,另一个就是使用虚拟摇杆。使用传感器的方法不需要额外的 UI 开发,相对简单,但是并不一定适合所有的游戏。下面分享一个自己做的虚拟摇杆。
网上有一些模拟摇杆的实现,但都是基于 Cocos2d 的,我这个就不在 Cocos2d 上做了。
先上个 demo 的效果图。
左下角的就是虚拟摇杆了。为它单独做一个类,继承UIView
@interface JoyStickView : UIView{ IBOutlet UIImageView *stickViewBase; IBOutlet UIImageView *stickView; UIImage *imgStickNormal; UIImage *imgStickHold; CGPoint mCenter;}
mCenter 是摇杆的中心位置,我的摇杆图片都是 128 * 128 的,所以 mCenter 在初始化函数中设置为 (64, 64)
stickViewBase 是摇杆的底盘
stickView 是摇杆的操纵杆
这两个 UIImageView 都直接在 xib 中配置好。我的底盘是不变的,所以在 xib 中直接指定了图片。而操纵杆有两种状态,普通状态和手握状态。手指没有触摸摇杆时,操纵杆处于普通状态,设置 stickView.image = imgStickNormal; 当手触摸摇杆时,设置 stickView.image = imgStickHold;具体的实现在 UIView 的三个 touch event 中进行。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ stickView.image = imgStickHold; [self touchEvent:touches];}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ [self touchEvent:touches];}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ stickView.image = imgStickNormal; CGPoint dtarget, dir; dir.x = dtarget.x = 0.0; dir.y = dtarget.y = 0.0; [self stickMoveTo:dtarget]; [self notifyDir:dir];}
touchesBegan 首先修改摇杆状态。 然后调用另一个函数计算摇杆的位置,这个和 touchesMoved 一样。
touchesEnded 恢复摇杆的状态和位置。在 touchEvent 里面计算并设置摇杆的位置,见如下代码。
- (void)touchEvent:(NSSet *)touches{ if([touches count] != 1) return ; UITouch *touch = [touches anyObject]; UIView *view = [touch view]; if(view != self) return ; CGPoint touchPoint = [touch locationInView:view]; CGPoint dtarget, dir; // calculate stick direction to the center dir.x = touchPoint.x - mCenter.x; dir.y = touchPoint.y - mCenter.y; double len = sqrt(dir.x * dir.x + dir.y * dir.y); if(len < 10.0 && len > -10.0) { // on center pos dtarget.x = 0.0; dtarget.y = 0.0; dir.x = 0; dir.y = 0; } else { double len_inv = (1.0 / len); dir.x *= len_inv; dir.y *= len_inv; dtarget.x = dir.x * STICK_CENTER_TARGET_POS_LEN; dtarget.y = dir.y * STICK_CENTER_TARGET_POS_LEN; } [self stickMoveTo:dtarget]; [self notifyDir:dir];}代码中 dir 即为摇杆中心到触摸位置的方向,我这里规定当触摸位置离开中心小于 10 的时候,认为摇杆不动。有需要可以根据自己的实际情况修改。如果距离大于 10 ,对 dir 做归一化,然后算一个摇杆和中心位置的偏离 dtarget ,我这里用于计算的常数值 STICK_CENTER_TARGET_POS_LEN 为 20。
最后调用 stickMoveTo 修改摇杆位置,发送摇杆变化的通知给游戏逻辑。
- (void)notifyDir:(CGPoint)dir{ NSAutoreleasePool * pool = [[NSAutoreleasePoolalloc] init]; NSValue *vdir = [NSValuevalueWithCGPoint:dir]; NSDictionary *userInfo = [NSDictionarydictionaryWithObjectsAndKeys: vdir, @"dir", nil]; NSNotificationCenter *notificationCenter = [NSNotificationCenterdefaultCenter]; [notificationCenter postNotificationName:@"StickChanged"object:niluserInfo:userInfo]; [pool release];}- (void)stickMoveTo:(CGPoint)deltaToCenter{ // because the original position of stickView is (0, 0) // so just set its position to the delta is ok CGRect fr = stickView.frame; fr.origin = deltaToCenter; stickView.frame = fr;}
- ios joystick 虚拟摇杆实现 ( 非 Cocos2d )
- Unity Joystick 虚拟摇杆的实现
- cocos2d-x摇杆JoyStick
- cocos2d-x的初步学习十二之虚拟摇杆Joystick
- cocos2d-x的初步学习十二之虚拟摇杆Joystick
- NGUI版虚拟摇杆joystick
- Cocos Creator Joystick虚拟摇杆
- unity虚拟摇杆控制 Virtual Joystick
- Cocos2d-js3.3 虚拟摇杆的实现
- Cocos2d-js3.3虚拟摇杆的实现
- Cocos2d-X 摇杆的使用(Joystick)
- 【iOS-cocos2d-X 游戏开发之十二】自定义Cocos2dx摇杆(增强Joystick),增加摇杆跟随用户触点作为摇杆坐标,让摇杆不再死板!
- Unity3D 双摇杆 c# JoyStick 实现自己的双摇杆
- Cocos2d-x3.2实现虚拟摇杆多点触摸
- Cocos2d-x3.2实现虚拟摇杆多点触摸-----沈大海
- Cocos2d-x3.2实现虚拟摇杆多点触摸
- Cocos2d-x3.2实现虚拟摇杆多点触摸
- Cocos2d-x3.2实现虚拟摇杆多点触摸
- OSX: 在VirtualBox中运行BootCamp上的Windows-补遗
- UVA 10034 By ACReaper
- 数据库提示,正在使用无法还原
- 黑马程序员_GUI
- Java虚拟机(JVM)中的内存设置详解
- ios joystick 虚拟摇杆实现 ( 非 Cocos2d )
- 数据结构之单循环链表C++(模板)
- 安装程序时,内存不能读或不能写问题解决
- Linux I2C核心、总线与设备驱动I
- 用户、组或角色’XXXX’在当前数据库中已存在
- Qt编译错误:usr/bin/ld: cannot find lxxx
- 法语数词
- 数学常数e的含义
- linux下解压缩rar、zip文件方法