CCPanZoomController + Tappable Sprites

时间:2012-08-24 15:43:39

标签: objective-c xcode cocos2d-iphone

我正在为我的游戏创建一个可缩放和可扩展的地图(使用CCPanZoomController)。在这张地图中,我希望有一个可点击的精灵,当它被点击时,“做点什么”......

我可以在不同的项目中完美地完成这两项工作,但是当我尝试将它们组合在一起时,当我点击我的精灵时没有任何反应。

我已经包含了一张图片以进一步展示: Demo Of What I Want

//in my init section


        self.isTouchEnabled = YES;

            mapBase = [CCSprite spriteWithFile:@"MapBase.png"];
            mapBase.anchorPoint = ccp(0, 0);
            [self addChild:mapBase z:-10];

            gym = [CCSprite spriteWithFile:@"Gym.png"];
            gym.scale = 0.3;
            gym.position = ccp(1620, 250);
            [self addChild:gym z:1];

            CGRect boundingRect = CGRectMake(0, 0, 2499, 1753);
            _controller = [[CCPanZoomController controllerWithNode:self] retain];
            _controller.boundingRect = boundingRect;
            _controller.zoomOutLimit = _controller.optimalZoomOutLimit;
            _controller.zoomInLimit = 2.0f;
            [_controller enableWithTouchPriority:1 swallowsTouches:YES];
//end of init

    -(void) registerWithTouchDispatcher
    {
        [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self
                                                                  priority:0 swallowsTouches:NO];
    }

    -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
        CGPoint touchPoint1 = [touch locationInView:[touch view]];

        if (CGRectContainsPoint(gym.boundingBox, touchPoint1)) return YES;

        return NO;

    }

    -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
        CGPoint touchPoint2 = [touch locationInView:[touch view]];
        if (CGRectContainsPoint(gym.boundingBox, touchPoint2)){
            CCLOG(@"SPRITE HAS BEEN TAPPED");
        }
    }

我希望能够放大/缩小整个地图(包括'Gym'精灵)。 如果精灵'Gym'被用户点击,那么我想“做点什么”。

如果有人能搞清楚这一点,我将非常感激! 感谢。

0 个答案:

没有答案