我想通过两条对角线将视图分成4个:
因此上三角区域将对应于“向上”.
其他3分别对应“左”,“下”,“右”.
这样做最简单或最有效的方法是什么?
我曾考虑创建4个三角形形状并检测它们内部的触摸,但是这可能是不必要的?
上下文:用于在地图中移动精灵.
谢谢!
解决方法 在我的评论之后,我想为自己模拟一个简单的实现,所以,这是一个简单的全屏 *** 纵杆,它将确定基于类似基数的方向触摸相对于屏幕中心的位置.
(FullScreenJoystick)
我还在使用cocos2d-iphone 1.1,所以..我不确定v2是否需要任何mods
FSJoy.h
#import "cocos2d.h"// Up,Down,left,Right - directionstypedef enum{ DIR_NONE = 0,DIR_UP,DIR_left,DIR_DOWN,DIR_RIGHT} ulDR;// Upleft,UpRight,Downleft,DownRight - boundarIEstypedef enum{ ul = 135,UR = 45,DL = 225,DR = 315} ulDR_BOUNDS;@interface FSJoy : cclayer{ CGPoint origin; float angleCurrent; BOol isActive;}@property CGPoint origin;@property float angleCurrent;@property BOol isActive;@end
FSJoy.m
#import "FSJoy.h"@implementation FSJoy@synthesize origin,angleCurrent,isActive;-(ID) init{ if( (self = [super init]) ) { self.istouchEnabled = YES; CGSize screenSize = [[CCDirector sharedDirector] winSize]; angleCurrent = 0.0f; origin = ccp(screenSize.wIDth / 2.0f,screenSize.height / 2.0f); isActive = NO; } return self;}-(voID) registerWithtouchdispatcher{ [[CCtouchdispatcher shareddispatcher] addTargetedDelegate:self priority:(INT_MIN - 4) swallowstouches:YES];}-(BOol) cctouchBegan:(UItouch *)touch withEvent:(UIEvent *)event{ CGPoint touchCurrent = [touch locationInVIEw:[touch vIEw]]; touchCurrent = [[CCDirector sharedDirector] convertToGL:touchCurrent]; angleCurrent = [self determineAngleFromPoint:origin topoint:touchCurrent]; [self determineDirectionFromAngle: angleCurrent]; isActive = YES; return YES; // full screen controller,so always return YES}-(voID) cctouchmoved:(UItouch *)touch withEvent:(UIEvent *)event{ CGPoint touchCurrent = [touch locationInVIEw:[touch vIEw]]; touchCurrent = [[CCDirector sharedDirector] convertToGL:touchCurrent]; angleCurrent = [self determineAngleFromPoint:origin topoint:touchCurrent]; [self determineDirectionFromAngle: angleCurrent];}-(voID) cctouchended:(UItouch *)touch withEvent:(UIEvent *)event{ isActive = NO;}-(voID) cctouchCancelled:(UItouch *)touch withEvent:(UIEvent *)event{ [self cctouchended:touch withEvent:event];}-(float) determineAngleFromPoint:(CGPoint)from topoint:(CGPoint)to{ float angle = ccpToAngle(ccpsub(to,from)); if (angle <= 0.0f) { angle += M_PI * 2.0f; } angle = CC_radians_TO_degrees(angle);// NSLog(@"angle is %f",angle); return angle;}-(ulDR) determineDirectionFromAngle:(float) angle{ ulDR dir = DIR_NONE; dir = ((angle >= UR) && (angle <= ul)) ? DIR_UP : dir; dir = ((angle >= DL) && (angle <= DR)) ? DIR_DOWN : dir; dir = ((angle > ul) && (angle < DL)) ? DIR_left : dir; dir = ((angle > DR) || (angle < UR)) ? DIR_RIGHT : dir; NSLog(@"direction is %d",dir); return dir;}@end
用法只是将 *** 纵杆添加到场景/图层:
FSJoyTest.h
#import "cocos2d.h"@interface FSJoyTest : cclayer@end
FSJoyTest.m
#import "FSJoyTest.h"#import "FSJoy.h"@implementation FSJoyTest-(ID) init{ if( (self=[super init]) ) { [self addChild: [FSJoy node]]; } return self;}@end总结
以上是内存溢出为你收集整理的将视图拆分为4个三角形区域并检测触摸(iOS和Cocos2d)全部内容,希望文章能够帮你解决将视图拆分为4个三角形区域并检测触摸(iOS和Cocos2d)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)