【问题标题】:SpriteKit moveByX half worksSpriteKit moveByX 一半有效
【发布时间】:2013-10-24 01:33:03
【问题描述】:

我有一个 SKSpriteNode 作为 SKScene 的子节点,但精灵节点比场景大得多,我需要用户能够手动滚动精灵节点以查看所有内容(它是一张巨大的地图)。我正在使用 SKAction moveByX: y: Duration: 方法,并且 moveByX 部分按预期工作。但是,Y pat 的移动会导致地图非常快地从屏幕底部射出。这是我在 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 不是。

我错过了什么?

【问题讨论】:

  • 你使用的是什么尺寸的精灵,你也在landscape中。只是这样我可以为你测试。
  • 精灵是 h, w: 728, 1024,我正在测试的 iPhone 是纵向的。
  • 好的,你也试过在 iOS 中使用原生滚动视图吗?效果很好.. 是否更适合您的需求?
  • 我想过我应该怎么做。我最终的目标是让场景与我所在的任何屏幕(iPad 或 iPhone)的大小完全相同,然后场景将巨大的地图作为子节点,它可以滚动,但也有一个数字具有更高 zPosition 的 labelNodes 将保持不变,即使用户正在移动其下方的地图。

标签: ios sprite-kit


【解决方案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 = [SKSpriteNode spriteNodeWithImageNamed:@"background"];
    _bg.anchorPoint = CGPointZero;
    _bg.position = CGPointZero;
    [self addChild:_bg];

【讨论】:

  • 天啊。我几乎不好意思显示我的错误....但是在这里:CGPoint location = [touch locationInNode:self]; CGPoint prevLocation = [touch previousLocationInView:self.view];
猜你喜欢
  • 1970-01-01
  • 2013-12-02
  • 1970-01-01
  • 2016-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多