在iPhone Cocos2d的特定屏幕上触摸

时间:2013-01-28 07:36:56

标签: iphone ios objective-c xcode cocos2d-iphone

我试图在屏幕的特定部分检测触摸开始触摸结束。我有一个蜘蛛作为雪碧,我通过touchbegan和touchend改变它的面部方向。而且我只想从我的蜘蛛前面接触。

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //[self ccTouchesMoved:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    startTouchPoint = [touch locationInView: [touch view]];

    //location = [[CCDirector sharedDirector] convertToGL: location];
    CCLOG(@"Location of Touch %@",NSStringFromCGPoint(startTouchPoint));
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [touches anyObject];
    CGPoint endlocation = [touch locationInView: [touch view]];
    if(startTouchPoint.x != endlocation.x && startTouchPoint.y != endlocation.y) {
        CGPoint touchPoint = [touch locationInView: [touch view]];
        touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];

        [self calcAngleAndRotateObjectStartingAtPoint:sprite_Spider.position endingAtPoint:touchPoint];
    }
}

enter image description here

这是我的蜘蛛,我想只在这些红线之间触摸。我怎样才能实现它。

2 个答案:

答案 0 :(得分:0)

在你的情况下,我会选择UITapGestureRecognizer并实现委托方法

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

你应该在所有其他地方的NO和YE之间的任何地方返回false。

您还可以检测点按手势的不同阶段

答案 1 :(得分:0)

每次在蜘蛛面前制作一个假想的矩形并使用此方法检测触摸

CGRectContainsPoint(<rect>,<point>)

如果触摸在矩形

内做任何事情
相关问题