iPhone游戏中模拟摇杆的教程
2014-12-11 15:37
417 查看
很多比较激烈,操作频率很高的游戏中,使用加速度感应往往不如用模拟摇杆操作。今天带来CocoaChina会员 “qwertyuasdf”
的原创教程:模拟摇杆的简单实现。
作者博客原文:http://blog.team-cusr.com/?p=152
论坛讨论帖地址 http://www.cocoachina.com/bbs/read.php?tid-16365.html
实现功能:
初始化:模拟摇杆自定位,自定范围(半径),自定图片.
控制:摇杆开关
输出:摇杆方向,摇杆 速度
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
效果
直接上代码了,直接看注释吧
joystick.h:
joystick.m
的原创教程:模拟摇杆的简单实现。
作者博客原文:http://blog.team-cusr.com/?p=152
论坛讨论帖地址 http://www.cocoachina.com/bbs/read.php?tid-16365.html
实现功能:
初始化:模拟摇杆自定位,自定范围(半径),自定图片.
控制:摇杆开关
输出:摇杆方向,摇杆 速度
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
效果
直接上代码了,直接看注释吧
joystick.h:
#import "cocos2d.h" @interface joystick : CCLayer {//继承于cocos的层 CGPoint centerPoint;//摇杆中心 CGPoint currentPoint;//摇杆当前位置 BOOL active;//是否激活摇杆 float radius;//摇杆半径 CCSprite *jsSprite; } //初始化 aPoint是摇杆中心 aRadius是摇杆半径 aJsSprite是摇杆控制点 aJsBg是摇杆背景 + (id) initWithCenter:(CGPoint)aPoint Radius:(float)aRadius jsPic:(CCSprite*)aJsSprite jsBg:(CCSprite*)aJsBg; - (id) initWithCenter:(CGPoint)aPoint Radius:(float)aRadius jsPic:(CCSprite*)aJsSprite jsBg:(CCSprite*)aJsBg; - (void) Active; - (void) Inactive; - (CGPoint) getDirection; - (float) getVelocity; - (void) updatePos:(ccTime) dt;//控制点位置刷新 @end |
#import "joystick.h" @implementation joystick //初始化 + (id) initWithCenter:(CGPoint)aPoint Radius:(float)aRadius jsPic:(CCSprite *)aJsSprite jsBg:(CCSprite *)aJsBg{ return [[[self alloc] initWithCenter:aPoint Radius:aRadius jsPic:aJsSprite jsBg:aJsBg] autorelease]; } - (id) initWithCenter:(CGPoint)aPoint Radius:(float)aRadius jsPic:(CCSprite*)aJsSprite jsBg:(CCSprite*)aJsBg; { if ( (self=[super init]) ) { active = NO; radius = aRadius; centerPoint = aPoint; currentPoint = centerPoint; jsSprite = aJsSprite; jsSprite.position=centerPoint; aJsBg.position=centerPoint; [self addChild:jsSprite]; [self addChild:aJsBg]; } return self; } - (void) updatePos:(ccTime)dt{ jsSprite.position = ccpAdd(jsSprite.position,ccpMult(ccpSub(currentPoint, jsSprite.position),0.5)); } //激活摇杆 - (void) Active { if (!active) { active=YES; [self schedule:@selector(updatePos:)];//添加刷新函数 [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:NO];//添加触摸委托 }else { NSLog(@"JoyStick is already active."); } } //冻结摇杆 - (void) Inactive { if (active) { active=NO; [self unschedule:@selector(updatePos:)];//删除刷新 [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];//删除委托 }else { NSLog(@"JoyStick is already inactive."); } } //3个触摸函数 - (BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event { if (!active) return NO; CGPoint touchPoint = [touch locationInView:[touch view]]; touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint]; if (ccpDistance(touchPoint, centerPoint) > radius) return NO; currentPoint = touchPoint; return YES; } - (void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent*)event { CGPoint touchPoint = [touch locationInView:[touch view]]; touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint]; if (ccpDistance(touchPoint, centerPoint) > radius) { currentPoint =ccpAdd(centerPoint,ccpMult(ccpNormalize(ccpSub(touchPoint, centerPoint)), radius)); }else { currentPoint = touchPoint; } |
相关文章推荐
- iPhone游戏中模拟摇杆的教程
- 游戏摇杆之Easy Touch 3教程
- iPhone iPad游戏应用开发视频教程
- 知易Cocos2D-iPhone 游戏开发教程
- 《Cocos2D-iPhone 游戏开发之路》第3节 高仿<Flappy Bird>教程(1)
- iphone用GDB修改游戏教程!详细的图文教程!
- 游戏摇杆之Easy Touch 3教程
- Unity手游之路<六>游戏摇杆之Easy Touch 3教程
- Cocos2d_iphone游戏开发_视频教程__国内顶级专业IOS培训机构__(内部资料不外传)
- 游戏摇杆之Easy Touch 3教程
- 【Untiy3D 游戏开发之一】Unity3D For Mac最新3.4.1版本破解教程分享并将Unity3D项目在iphone上运行;
- 【Untiy3D 游戏开发之一】Unity3D For Mac最新3.4.1版本破解教程分享并将Unity3D项目在iphone上运行
- iPhone手机ipa破解补丁安装方法和ipa软件游戏安装教程(1)
- 用iPhone修改及调试游戏作弊版教程【精华】
- Unity手游之路<六>游戏摇杆之Easy Touch 3教程
- 知易cocos2D-iphone游戏开发教程
- 几行代码为您的游戏增加模拟摇杆
- 知易Cocos2D-iPhone 游戏开发教程001
- 《Cocos2D-iPhone 游戏开发之路》第5节 高仿<Flappy Bird>教程(3)
- 守卫者游戏开发教程之cocos2d-iphone2.0第二部分(选择游戏场景)