【发布时间】:2012-01-07 05:38:42
【问题描述】:
我正在创建一个应用程序,我正在尝试制作它,以便我可以在屏幕上拖动一个精灵(顶部有第二个精灵),但如果我只是点击精灵,应该调用其他方法。
当我触摸精灵的边缘时,我的拖动工作正常,但是当我尝试从精灵的中间(第二个精灵在顶部)拖动时,拖动根本不起作用,但是点击被调用。
我知道为什么不这样做,但是,两个精灵的触摸会发生冲突,因为顶部的精灵在接触到下面的第二个之前已经吞下了触摸。
如果手指移动,我希望精灵拖动,但我希望仅在轻按时注册轻按(即手指没有在屏幕上移动),我将如何实现这一点?
我正在使用的精灵的视觉效果(如果有帮助的话):
黄色符文是与其下面的石头分开的精灵(因为涉及动画)。
--------Touch for the top sprite----------
-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event{
lastTouchLocation = [RuneScene locationFromTouch:touch];
BOOL isTouchHandled = CGRectContainsPoint([charSprite boundingBox], lastTouchLocation);
return isTouchHandled;
}
-(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event{
NSLog(@"Tap received!");
}
------Touch for the bottom sprite--------
-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event{
lastTouchLocation = [RuneScene locationFromTouch:touch];
BOOL isTouchHandled = NO;
// Check if this touch is on the Spider's sprite.
if (CGRectContainsPoint([current.runeSprite boundingBox], lastTouchLocation)){
mover = current;
isTouchHandled = YES;
}
else if(CGRectContainsPoint([rune1.runeSprite boundingBox], lastTouchLocation)){
mover = rune1;
isTouchHandled = YES;
}
else if(CGRectContainsPoint([rune2.runeSprite boundingBox], lastTouchLocation)){
mover = rune2;
isTouchHandled = YES;
}else if(CGRectContainsPoint([rune3.runeSprite boundingBox], lastTouchLocation)){
mover = rune3;
isTouchHandled = YES;
}else if(CGRectContainsPoint([rune4.runeSprite boundingBox], lastTouchLocation)){
mover = rune4;
isTouchHandled = YES;
}else if(CGRectContainsPoint([rune5.runeSprite boundingBox], lastTouchLocation)){
mover = rune5;
isTouchHandled = YES;
}else if(CGRectContainsPoint([rune6.runeSprite boundingBox], lastTouchLocation)){
mover = rune6;
isTouchHandled = YES;
}else if(CGRectContainsPoint([rune7.runeSprite boundingBox], lastTouchLocation)){
mover = rune7;
isTouchHandled = YES;
}else if(CGRectContainsPoint([rune0.runeSprite boundingBox], lastTouchLocation)){
mover = rune0;
isTouchHandled = YES;
}
// Stop the move action so it doesn't interfere with the user's scrolling.
//[self stopActionByTag:ActionTagCastingLayerMovesBack];
// Always swallow touches, GameLayer is the last layer to receive touches.
return isTouchHandled;
}
-(void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event{
CGPoint currentTouchLocation = [RuneScene locationFromTouch:touch];
// Take the difference of the current to the last touch location.
CGPoint moveTo = ccpSub(lastTouchLocation, currentTouchLocation);
// Then reverse it since the goal is not to give the impression of moving the camera over the background,
// but to touch and move the background.
moveTo = ccpMult(moveTo, -1);
lastTouchLocation = currentTouchLocation;
[self moveActionWithLocation: moveTo];
}
-(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event{
if (!current.isPlaced && mover == current && currentR < Rune6) {
// Move the game layer back to its designated position.
CCMoveTo* move = [CCMoveTo actionWithDuration:1 position:curPt];
CCEaseIn* ease = [CCEaseIn actionWithAction:move rate:0.5f];
//ease.tag = ActionTagCastingLayerMovesBack;
[current.runeSprite runAction:ease];
[current setIsPlaced:YES];
current.charSprite = [characters objectAtIndex:currentR];
current.charSprite.position = curPt;
//charSprite.visible = YES;
[current performSelector:@selector(fade:) withObject:current.charSprite afterDelay:1];
[current reorderChild:current.charSprite z:10];
[self updateCurrentRune:currentR];
[self updateCurrentCastNum:currentP];
[self reorderChild:current z:10];
}
}
我尝试查看 UITapGestureRecognizer,但我尝试实现的所有操作都不起作用。我的图层/精灵不允许我将它们添加为手势。我还在 cocos2d 论坛上阅读了有关 CCGrstureRecognizer 或其他内容的内容,但我找不到该类的任何文档,也找不到实际使用它的方法...
这里有人知道可以帮助我解决问题的方法吗?
【问题讨论】:
-
FWIW Kobold2D 为手势识别器提供了一个超级简单的包装器:learn-cocos2d.com/api-ref/latest/Kobold2D/html/…
-
哈,我没想到我正在阅读的书的作者会对我的问题发表评论。
-
那我猜我是西班牙宗教裁判所。 ;)
标签: objective-c ios cocos2d-iphone touch gesture-recognition