onTouch事件不被调用,当精灵离开地图的初始部分?

当我的播放器被移动到地图的右上方时,看起来没有onTouch事件被调用(把NSLog语句放进来确认)。 地图足够大,当层到达顶部或右侧时,仍然可以稍微看到地图,但是当点击任何进一步的内容时,屏幕上最初加载的内容不会触发事件,并且字符是卡住。 有任何想法吗?

- (void)setCenterOfScreen:(CGPoint) position { CGSize screenSize = [[CCDirector sharedDirector] viewSize]; int x = MAX(position.x, screenSize.width/2); int y = MAX(position.y, screenSize.height/2); x = MIN(x, theMap.mapSize.width * theMap.tileSize.width - screenSize.width/2); y = MIN(y, theMap.mapSize.height * theMap.tileSize.height - screenSize.height/2); CGPoint goodPoint = ccp(x,y); CGPoint centerOfScreen = ccp(screenSize.width/2, screenSize.height/2); CGPoint difference = ccpSub(centerOfScreen, goodPoint); self.position = difference; } -(void) touchEnded:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint touchLoc = [touch locationInNode:self]; // NOT CALLED WHEN PLAYER REACHES TOP OR RIGHT OF MAP (INITIAL SCREEN LOAD PART OF MAP) // When clicking any furhter or top on the map, no touch event is fired and character (sprite) doesn't move NSLog(@"Touch fired"); CGPoint playerPos = mainChar.position; CGPoint diff = ccpSub(touchLoc, playerPos); // Move horizontal or vertical? if (abs(diff.x) > abs(diff.y)) { if (diff.x > 0) { playerPos.x += theMap.tileSize.width; } else { playerPos.x -= theMap.tileSize.width; } } else { if (diff.y > 0) { playerPos.y += theMap.tileSize.height; } else { playerPos.y -= theMap.tileSize.height; } } if (playerPos.x <= (theMap.mapSize.width * theMap.tileSize.width) && playerPos.y <= (theMap.mapSize.height * theMap.tileSize.height) && playerPos.y >= 0 && playerPos.x >= 0) { mainChar.position = playerPos; } [self setCenterOfScreen:mainChar.position]; } 

这很可能是你的场景的内容大小。 我相信你正在移动的场景(CCScene)与调用setCenterOfScreen移动播放器的权利? 如果我没有记错的话,场景的内容大小和边界框不会随着你添加小孩而改变。 如果是这种情况(或仍然如此),则需要将场景的内容大小设置为整个级别(地图)的大小。 您还需要确保左下angular或地图与(0,0)处场景的左下方alignment,以便场景的新边界框包围您的所有地图。

在设置场景CCLOG场景的内容大小之后检查。 如果它不是你的地图的大小,那么你知道这是问题,如果你确实移动场景调用setCenterOfScreen。

另一个解决scheme是创build一个添加到场景中的根内容节点,然后将您的游戏元素添加到该内容节点。 这样触摸就发生在场景中,但是当你做移动的时候,你移动的是内容节点,而不是场景。 例如:

 @implementation YourScene - (void)onEnter { [super onEnter]; self.contentNode = [CCNode node]; [self addChild:self.contentNode]; CCSprite* bg = ...; CCSprite* player = ...; [self.contentNode addChild:bg]; [self.contentNode addChild:player]; ... } - (void)setCenterOfScreen:(CGPoint)position { CGSize screenSize = [[CCDirector sharedDirector] viewSize]; int x = MAX(position.x, screenSize.width/2); int y = MAX(position.y, screenSize.height/2); x = MIN(x, theMap.mapSize.width * theMap.tileSize.width - screenSize.width/2); y = MIN(y, theMap.mapSize.height * theMap.tileSize.height - screenSize.height/2); CGPoint goodPoint = ccp(x,y); CGPoint centerOfScreen = ccp(screenSize.width/2, screenSize.height/2); CGPoint difference = ccpSub(centerOfScreen, goodPoint); self.contentNode.position = ccpAdd(self.contentNode.position, difference); } 

希望这可以帮助。