有一种简单的方法可以执行以下操作吗?
我想通过两条对角线将视图分成4个:
因此上三角区域将对应于“向上”。 其他3将分别对应“左”,“下”,“右”。
最简单或最有效的方法是什么? 我曾考虑创建4个三角形形状并检测它们内部的触摸,但这可能是不必要的吗?
上下文:用于在地图中移动精灵。
谢谢!
答案 0 :(得分:1)
在我评论之后,我想为自己模拟一个简单的实现,所以,这是一个简单的全屏操纵杆,它将确定基于类似基数的方向 触摸相对于屏幕中心的位置。
(FullScreenJoystick)
我还在使用cocos2d-iphone 1.1,所以..我不确定v2是否需要任何mod?
<强> FSJoy.h 强>
#import "cocos2d.h"
// Up, Down, Left, Right - directions
typedef enum
{
DIR_NONE = 0,
DIR_UP,
DIR_LEFT,
DIR_DOWN,
DIR_RIGHT
} ULDR;
// UpLeft, UpRight, DownLeft, DownRight - boundaries
typedef 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
答案 1 :(得分:0)
只是一个想法:
以上面视图的大小创建一个图像(不要将它添加到任何视图只是将其存储在某处)。图像文件将包含您想要的四个三角形,但为每个三角形着色,颜色不同(没有渐变!:))
点击视图时,请触摸触摸位置坐标(如果需要,将其转换为图像坐标)并测试图像中相同位置的像素颜色。颜色将告诉您按下了哪个三角形。