onTouch事件不被调用,当sprite移动地图的初始部分时? [英] onTouch Events not being called, when sprite moves off initial part of map?

查看:139
本文介绍了onTouch事件不被调用,当sprite移动地图的初始部分时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的播放器移动到地图的右上方时,没有调用onTouch事件(将NSLog语句放入确认)。地图足够大,当我到达顶部或右侧时,您仍然可以看到地图,因为图层按照预期移动,但是当点击任何进一步,然后最初加载到屏幕上时,没有触发事件被触发,char卡住。任何想法?

It appears no onTouch events are being called (put NSLog statements in to confirm) when my player is moved above or to the right of my map. Map is big enough, and you can still see the map slightly when I get to the top or right, as the layer shifts as intended, but when clicking any further then what's initially loaded on screen, no touch events are being fired and char is stuck. Any ideas?

- (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,当你移动播放器对吗?如果我记得正确的场景的内容大小和边框不会改变,因为你添加了孩子。如果是这种情况(或情况),那么您将需要将场景的内容大小设置为整个级别(地图)的大小。您还需要确保左下角或地图在(0,0)处与场景的左下角对齐,以便场景的新边界框包含您的所有地图。

It is most likely your scene's content size. I believe you are moving the scene (CCScene) with the call to setCenterOfScreen as you move the player right? If I remember correctly the content size and bounding box for the scene doesn't change as you add children to it. If that is the case (or still the case) then you'll need to set the content size of the scene to the size of your entire level (map). You'll also want to ensure that the lower-left or your map aligns with the lower-left of the scene at (0,0) so that your scene's new bounding box encloses all of your map.

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

To check, after you've setup the scene CCLOG the scene's content size. If it is not the size of your map then you know that is the problem if you are indeed moving the scene with the call to setCenterOfScreen.

另一个解决方案是创建一个根内容节点,然后将您的游戏元素添加到该内容节点。这样,触摸发生在场景上,但是当您执行移动时,您移动内容节点,而不是场景。例如:

Another solution is to create a root content node that you add to the scene, then you add your game elements to that content node. That way the touch occurs on the scene but when you do the movements you move the content node, not the scene. For example:

@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);
}

希望这有帮助。

这篇关于onTouch事件不被调用,当sprite移动地图的初始部分时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆