根据用户点击的位置,Sprite没有移动到适当的位置

时间:2014-06-10 19:50:31

标签: ios objective-c cocos2d-iphone

当我点击向右或向左移动时,精灵按预期执行。然而,当我点击底部希望它下降时,它实际上会上升。当我做同样的事情反之亦然时,它会下降。我只是想制作它,以便用户可以根据它们在屏幕上的触摸位置正确地浏览精灵。

代码:

- (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) setPlayerPosition:(CGPoint)position
{
    mainChar.position = position;
}
-(void) touchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchLoc = [touch locationInNode:self];
    touchLoc = [[CCDirector sharedDirector] convertToGL:touchLoc];
    touchLoc = [self convertToNodeSpace:touchLoc];

    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)
    {
        [self setPlayerPosition:playerPos];
    }
    [self setCenterOfScreen:mainChar.position];
}

2 个答案:

答案 0 :(得分:0)

应该向下移动的是什么?它是精灵还是层?如果它是图层并且是由setCenterOfScreen方法引起的,那么......

你在哪里:

CGPoint difference = ccpSub(centerOfScreen, goodPoint);

尝试:

CGPoint difference = ccpSub(goodPoint, centerOfScreen);

为了便于观察,图像中心自动屏幕的Y为10,而goodPoint为5(意味着触摸位于中心)。 10 - 5 = 5.因此,如果您要将该增量添加到图层/场景的当前Y(最初从0开始),它会上升而不会像您预期的那样下降只是基本上使Y = 5,这将一切都向上移动而不是向下移动。切换它们。

更新(已编辑答案):

根据您的评论,问题是玩家。你在用cocos v3吗?为什么你没有:

CGPoint touchLoc = [touch locationInNode:self];

留下它?回到像v2这样的版本,我会做类似的事情:

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];

    CGPoint touchPos = [touch locationInView:[touch view]];
    touchPos = [[CCDirector sharedDirector] convertToGL:touchPos];

    // Do somthing...
}

但是在v3中你不会这样做。方法locationInNode看起来像这样......

- (CGPoint) locationInNode:(CCNode*) node
{
    CCDirector* dir = [CCDirector sharedDirector];

    CGPoint touchLocation = [self locationInView: [self view]];
    touchLocation = [dir convertToGL: touchLocation];
    return [node convertToNodeSpace:touchLocation];
}

因此,当您在locationInNode中调用convertToGL和convertToNodeSpace方法之后再次调用convertToGL和convertToNodeSpace方法时,您所做的就是搞砸了这些值。 locationInNode已经为您完成了这项工作。

您的问题的答案,删除:

touchLoc = [[CCDirector sharedDirector] convertToGL:touchLoc];
touchLoc = [self convertToNodeSpace:touchLoc];

希望这有帮助。

答案 1 :(得分:0)

显而易见的解决方案是在更新y时反转信号:

if (diff.y > 0)
   playerPos.y -= theMap.tileSize.height;
else
   playerPos.y += theMap.tileSize.height;

您还可以更改mainChar位置的引用:

mainChar.positionType = CCPositionTypeMake(CCPositionUnitPoints, CCPositionUnitPoints, CCPositionReferenceCornerTopLeft);

mainChar.positionType = CCPositionTypeMake(CCPositionUnitPoints, CCPositionUnitPoints, CCPositionReferenceCornerBottomLeft);

取决于当前的参考。