SpriteKit moveByX一半有效

时间:2013-10-16 03:48:09

标签: ios sprite-kit

我有一个SKSpriteNode作为SKScene的孩子,但是sprite节点比场景大得多,我需要用户能够手动滚动sprite节点以便查看所有这些(它是一张巨大的地图)。我正在使用SKAction moveByX:y:Duration:方法,moveByX部分正在按预期工作。尽管如此,Y拍的移动会导致地图以非常快的速度从屏幕底部射出。这是我在touchesMoved中的代码:

    CGFloat xDiff = location.x - prevLocation.x;
    CGFloat yDiff = location.y - prevLocation.y;
    SKAction *moveBy = [SKAction moveByX:xDiff y:yDiff duration:0];
    if (YES) NSLog(@"%f, %f", xDiff, yDiff);
    [map runAction:moveBy];

因此,这个逻辑是针对x轴的点,但对于y轴则不是这样。此外,如果我将yDiff更改为反向计算(prevLocation.y - location.y),我会在NSLog输出中注意到yDiff在单个平移动作中是累积的,但xDiff不是。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

我试过你的代码,它做了你所期望的。只需仔细检查移动的触摸效果如何。

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint currentPoint = [[touches anyObject] locationInNode:self];

    CGPoint previousPoint = [[touches anyObject] previousLocationInNode:self];

    CGFloat xDiff = currentPoint.x - previousPoint.x;
    CGFloat yDiff = currentPoint.y - previousPoint.y;

    NSLog(@"%f, %f", xDiff, yDiff);
    SKAction *moveBy = [SKAction moveByX:xDiff y:yDiff duration:0];
    [_bg runAction:moveBy];
}

我也像这样设置了bg。

    _bg = [SKSpriteNode spriteNodeWithImageNamed:@"background"];
    _bg.anchorPoint = CGPointZero;
    _bg.position = CGPointZero;
    [self addChild:_bg];