当我的播放器移动到地图上方或右侧时,似乎没有调用onTouch事件(将NSLog语句放入确认)。地图足够大,当我到达顶部或右边时,您仍然可以稍微看到地图,因为图层会按预期移动,但是当点击任何进一步的内容时,屏幕上最初加载的内容,没有触发事件,而且char是卡住。有什么想法吗?
- (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];
}
答案 0 :(得分:0)
这很可能是您的场景的内容大小。当你向右移动玩家时,我相信你正在调用setCenterOfScreen来移动场景(CCScene)?如果我没记错的话,当您向其中添加子项时,场景的内容大小和边界框不会发生变化。如果是这种情况(或者仍然如此),那么您需要将场景的内容大小设置为整个级别(地图)的大小。您还需要确保左下角或地图与(0,0)场景的左下角对齐,以便您的场景的新边界框包含您的所有地图。 / p>
在您设置场景CCLOG后,检查场景的内容大小。如果它不是地图的大小,那么你知道如果你确实通过调用setCenterOfScreen来移动场景就会出现问题。
另一种解决方案是创建添加到场景中的根内容节点,然后将游戏元素添加到该内容节点。这样,触摸就会发生在场景上,但是当你进行动作时,你会移动内容节点,而不是场景。例如:
@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);
}
希望这有帮助。