我在编写的 cocos2D 程序中处理触摸事件时遇到了一个有趣的问题。我在 CCScene 中有 3 个 CCLayer 子类层:
backgroundLayer - z:0 - 用于显示背景图像的简单静态层。
planetLayer - z:3 - 显示层 - 数据变化的可视化显示在这里。
gameControlsLayer - z:5 - 用于显示数据控制器的层,例如滑块和按钮。
我将行星层和控制层分开,因为我想平移和缩放行星层,而不用担心控制受到影响。
这是设置这些层的场景初始化代码:
- (id) init
{
self = [super init];
if (self != nil)
{
// background layer
BackgroundLayer *backgroundLayer = [BackgroundLayer node];
[self addChild:backgroundLayer z:0];
// planet layer
PlanetLayer *planetLayer = [PlanetLayer node];
[self addChild:planetLayer z:3];
// gameplay layer
GameControlsLayer *gameControlsLayer = [GameControlsLayer node];
[self addChild:gameControlsLayer z:5];
}
return self;
}
我需要在行星层和控制层上进行触摸处理。在控制层上,触摸处理位于作为子层的控制对象内部。我首先编写了所有这些代码,它工作正常。
之后,我为行星图层编写了图层范围的触摸处理,这样我就可以进行触摸滚动和缩放。我已经滚动(虽然还没有缩放),它工作正常。我的问题是:当我触摸和操作控制层上的控件时,它们工作正常,但是当我上下移动滑块拇指时,例如,控制层后面的行星层平移,好像触摸是为了它。
包含代码可能是多余的,但这里是相关的片段:
对于滑块控件,控件层的子层:
- (void) onEnterTransitionDidFinish
{
[[CCTouchDispatcher sharedDispatcher]
addTargetedDelegate:self priority:1 swallowsTouches:YES];
}
- (void) onExit
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
}
#pragma mark Touch Delegate
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint location = [[CCDirector sharedDirector]
convertToGL:[touch locationInView:[touch view]]];
float distance = [self distanceBetweenPointOne:thumbPosition
andPointTwo:location];
if (distance > thumbRadius*2)
{
return NO;
}
// touch is on the thumb. store the location so we
// can calculate changes on ccTouchMoved events
touched = YES;
return YES;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
if (!touched)
{
return;
}
CGPoint location = [[CCDirector sharedDirector]
convertToGL:[touch locationInView:[touch view]]];
location = [self convertToNodeSpace:location];
// if we're too far off the thumb, abandon it
float distance = [self distanceBetweenPointOne:thumbPosition
andPointTwo:location];
if (distance > thumbRadius*3)
{
//touched = NO;
return;
}
// this call adjusts some ivars – not relevant
[self updateValueAndPosition:location];
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
if (touched)
{
touched = NO;
}
}
- (void)ccTouchCancelled:(UITouch *)touch
withEvent:(UIEvent *)event
{
[self ccTouchEnded:touch withEvent:event];
}
以下是行星层的相关代码:
// Yes, we want touch notifications please
//
- (void) onEnterTransitionDidFinish
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self
priority:0 swallowsTouches:NO];
}
#pragma mark Touch Delegate
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector]
convertToGL:touchLocation];
// ****NOTE – the following is a kludge I used it to break
// ********** the behavior; the controls are all grouped
// ********** at the bottom of the screen. But this is not
// ********** an acceptable fix. I shouldn’t need to mask
// ********** off certain areas of the screen. This strategy
// ********** will not hold once I add other controls to the
// ********** control layer.
//
if (touchLocation.y > 250.0f)
{
self.touched = YES;
touchHash = [touch hash];
return YES;
}
return YES;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
if (!touched)
{
return;
}
if ([touch hash] == touchHash)
{
CGSize screenSize = [[CCDirector sharedDirector] winSize];
CGPoint touchLocation =
[touch locationInView:[touch view]];
CGPoint prevLocation =
[touch previousLocationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector]
convertToGL:touchLocation];
prevLocation = [[CCDirector sharedDirector]
convertToGL:prevLocation];
CGPoint diff = ccpSub(touchLocation, prevLocation);
CGPoint currentPosition = [self position];
CGPoint newPosition = ccpAdd(currentPosition, diff);
if (newPosition.x < lowestX)
newPosition.x = lowestX;
else if (newPosition.x > highestX-screenSize.width)
newPosition.x = highestX-screenSize.width;
if (newPosition.y < lowestY)
newPosition.y = lowestY;
else if (newPosition.y > highestY-screenSize.height)
newPosition.y = highestY-screenSize.height;
[self setPosition:newPosition];
}
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
if ([touch hash] == touchHash)
{
if (touched)
{
touched = NO;
}
}
}
- (void)ccTouchCancelled:(UITouch *)touch
withEvent:(UIEvent *)event
{
[self ccTouchEnded:touch withEvent:event];
}
我想添加捏缩放功能,但我需要先解决这个问题,然后再建议这样做。这里有人有答案吗?我觉得我错过了一些明显的东西……</p>
谢谢!