1

我无法理解以下教程中的一些数学:

雪碧套件教程

我不确定如何理解偏移量。在教程进行到一半时,Ray 使用了以下代码:

UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];

// 2 - Set up initial location of projectile
SKSpriteNode * projectile = [SKSpriteNode spriteNodeWithImageNamed:@"projectile"];
projectile.position = self.player.position;

// 3- Determine offset of location to projectile
CGPoint offset = rwSub(location, projectile.position);

其中 rwSub 是

static inline CGPoint rwSub(CGPoint a, CGPoint b) {
    return CGPointMake(a.x - b.x, a.y - b.y);
}

我知道这段代码有效,但我不明白。我尝试了 NSLogging 触摸点和偏移点,它们并没有形成如图所示的三角形:

图像
(来源:raywenderlich.com

这是我从输出中得到的:

Touch Location
 X: 549.000000 Y: 154.000000
Offset
 X: 535.500000 Y: -6.000000

这不会形成正确方向的矢量..但它仍然有效吗?有人能解释一下偏移是如何工作的吗?

4

2 回答 2

2

偏移量是与忍者的差异,以及您所触及的点。因此,您记录的触摸是向右 535 分,向下 6 分 (-6)。

所以它朝着正确的方向前进,相对于玩家。

该教程还强制忍者之星离开屏幕,通过

 // 6 - Get the direction of where to shoot
CGPoint direction = rwNormalize(offset);

// 7 - Make it shoot far enough to be guaranteed off screen
CGPoint shootAmount = rwMult(direction, 1000);

// 8 - Add the shoot amount to the current position       
CGPoint realDest = rwAdd(shootAmount, projectile.position);

画一些图片,它会帮助你理解。

于 2013-10-03T12:45:44.957 回答
1

在这种情况下,偏移量仅表示与角色相关的触摸位置,并让您知道弹丸将瞄准的位置。

在本教程中,您可以在接下来的几行中看到:

// 4 - Bail out if you are shooting down or backwards
    if (offset.x <= 0) return;

在这个例子offset.x < 0中,意味着射弹在 x 轴上瞄准忍者后面的东西,其中 0 是角色的 x 坐标。

这里的想法是在角色自己的参考中转换射弹的目标坐标,以更好地了解它们彼此的位置。

于 2013-09-26T09:21:01.673 回答