如何区分点击和拖动
我正在创建一个应用程序,我正在尝试制作它,以便我可以在屏幕上拖动一个精灵(顶部有第二个精灵),但如果我只是点击精灵,则应该调用其他方法。
当我触摸精灵的边缘时,拖动工作正常,但是当我尝试从精灵的中间(第二个精灵位于顶部)拖动时,拖动根本不起作用,但点击被调用。
我知道为什么不这样做,不过,两个精灵的触碰是相互冲突的,因为上面的那个在触碰下面的第二个之前就吞噬了触碰。
如果手指移动,我希望精灵拖动,但我希望仅在轻击时注册轻击(即手指在屏幕上没有移动),我该如何实现这一点?
我正在使用的精灵的视觉效果(如果有帮助的话):
黄色符文是与其下面的石头分开的精灵(因为涉及动画)。
--------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,但我所做的一切尝试都不起作用。我的图层/精灵不允许我将它们添加为手势。我还阅读了有关 CCGrstureRecognizer 的内容或 cocos2d 论坛上的内容,但我找不到该类的任何文档,也无法找到如何实际使用它...
这里有人知道如何帮助我解决问题吗?问题?
I'm creating an app and I'm trying to make it so I can drag a sprite (with a second sprite on top) around the screen, but if I just tap the sprite some other method should get called.
I got the dragging working fine when I touch the edge of my sprite, but when I try to drag from the middle of the sprite (where the second sprite is on top) the dragging doesn't work at all, but the tap gets called.
I know why is't doing this, tho, there's conflicting touches from both the sprits because the one on top is swallowing the touch before it hits the second one below it.
How would I be able to implement this where I want the sprite to drag if the finger moves, but I want the tap to register when only a tap is given (IE the finger didn't move on the screen)?
A visual of the sprites I'm working with (if it helps):
The yellow rune is a separate sprite from the stone underneath it (because there's animations involved).
--------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];
}
}
I tried looking into the UITapGestureRecognizer, but everything I do to try to implement that never works. My layers/sprites won't let me add them as gestures. I also read something about CCGrstureRecognizer or something on the cocos2d forums, but I can't find any documentation on that class, nor can I find out how to actually use it...
Does anyone here know of a way to help me with my issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是使用(如果 CGRectContainsPoint)尝试使用视图的 pointInside:withEvent: 返回 NO。这将导致系统表现得好像您的视图根本不存在一样。
Rather than using (if CGRectContainsPoint) try using view's pointInside:withEvent: to return NO. That will cause the system to act like your view isn't even there.