1

我正在为我的游戏创建一个可缩放和平移的地图(使用 CCPanZoomController)。在这张地图中,我想要一个可点击的精灵,当它被点击时,“做点什么”……</p>

我可以让这两个东西在单独的项目中完美地工作,但是当我尝试将它们结合起来时,当我点击我的精灵时没有任何反应。

我已经包含了一张图片来进一步演示: 我想要的演示

//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");
        }
    }

我希望能够放大/缩小并平移整个地图(包括“健身房”精灵)。如果用户点击了精灵“健身房”,那么我想“做点什么”。

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

4

0 回答 0