gpt4 book ai didi

ios - SpriteKit 中的坐标系

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:13:28 25 4
gpt4 key购买 nike

我对 spritekit 坐标系很困惑。我需要澄清两个案例。

案例 1:

当 Sprite 节点作为子节点添加到场景时:

SKSpriteNode * blueBall = [SKSpriteNode spriteNodeWithImageNamed:@"hRedBall.png"];
blueBall.name=@"blueball";
blueBall.size=CGSizeMake(75, 75);
blueBall.position=point; //I am varying this point
[self addChild:blueBall];

我每次都在不同的点添加节点。这些是我添加节点的点:(100x100)(200x200)(300x300)(400x400)(900,600)

在我的 iphone 5 设备中,它们看起来像这样:

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

如果你查看 100x100,当我在设备上的点 (100,100) 添加 Sprite 时,当我记录节点位置时,它显示 (99.9,138.17) 与点 (100,100) 相比有很大差异。

尽管其他点也有变化,但我忽略了它们,因为对于其他点来说差异太小了。

-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
[self bounceTheBlueBallAtPoint:CGPointMake(100, 100)];

}

-(void)bounceTheBlueBallAtPoint:(CGPoint)point{


SKSpriteNode * blueBall = [SKSpriteNode spriteNodeWithImageNamed:@"hRedBall.png"];
blueBall.name=@"blueball";
blueBall.size=CGSizeMake(75, 75);

blueBall.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:blueBall.frame.size.width/2];


blueBall.physicsBody.categoryBitMask=blueBallHitCategory;
blueBall.physicsBody.collisionBitMask=blueBallHitCategory|pinkBallHitCategory|wallHitCategory;
blueBall.physicsBody.contactTestBitMask=blueBallHitCategory|pinkBallHitCategory|wallHitCategory;

blueBall.physicsBody.dynamic=YES;
blueBall.physicsBody.affectedByGravity=NO;
blueBall.physicsBody.restitution=1.0;
blueBall.physicsBody.friction=0.0;
blueBall.physicsBody.linearDamping=0.0;
blueBall.physicsBody.angularDamping=0.0;

blueBall.position=point;

[self addChild:blueBall];


//applying impulse to bounce off
// CGVector impulse = CGVectorMake(30.0,60.0);
// [blueBall.physicsBody applyImpulse:impulse];
//

}

我正在用这种方法检查球的位置:

      -(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */

[self checkSpriteMovement];

}

-(void)checkSpriteMovement{

[self enumerateChildNodesWithName:@"blueball" usingBlock:^(SKNode * _Nonnull node, BOOL * _Nonnull stop) {

SKSpriteNode *ball=(SKSpriteNode *)node;

if (ball.physicsBody.resting) {

NSLog(@"The ball position is %@",NSStringFromCGPoint(ball.position));
}

}];

}

案例 2:

假设我有一个背景 Sprite 节点,我将球节点添加到这个背景节点,而不是将球节点添加到场景。

 _background = [SKSpriteNode spriteNodeWithImageNamed:@"parkImage.jpg"];
_background.size = self.frame.size; //frame size is (1024x768)
_background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:_background];

然后我将球节点添加到这个背景中:

[_background addChild:blueBall];

这是添加的后台节点:

enter image description here

现在我尝试将球添加到添加的背景中:

在点:(100,100):

enter image description here

点:(-280,-150)

enter image description here

我还想讨论一下第三种情况:

案例三:

我想要另一个节点来调用 ring 的 sprite backgorund。后来我想在背景中添加球。所以会有2个子节点。

   _ringNode=[SKSpriteNode spriteNodeWithImageNamed:@"hue_ring.png"];
_ringNode.size=CGSizeMake(80, 80);
_ringNode.position=CGPointMake(0, 0); //center point of background in this case
_ringNode.anchorPoint=CGPointMake(0.5,0.5);
_ringNode.name=kRingNodeName;
[_background addChild:_ringNode];

所以戒指会在这里:

enter image description here

现在我想将球节点添加到背景节点:

我使用相同的代码在 (-280,-150) 点添加球:

enter image description here

为什么场景和 Sprite 节点的坐标系会发生变化?

我面临的另一个问题是:

节点未正确添加到场景中。即使场景已启动,用户也看不到节点,但我可以看到节点数,表明屏幕上有节点。我需要运行代码再次查看正确添加到场景中的节点。这种情况每 4 次发生一次。我只需要停止代码并再次运行它就可以看到屏幕上的球,否则我只会看到没有任何球的背景,尽管我正在将它们添加到背景中。

为什么会这样?

我在 didMoveToView 中添加所有节点

最佳答案

好的,所以很难回答这个问题,因为你没有一个问题,但我会尽力解释为什么事情会这样发生。

首先要考虑的是。你的场景是 1024 x 768(猜测是因为你的评论之一和你的问题)现在这会导致一些问题。首先是你的手机不是那个纵横比,其次你的手机不是那个尺寸。如果您查看 ViewController,您可能会看到设置了一些 AspectFill 或 AspectFit。这会缩小你的场景。当涉及到您在代码中看到的内容和您所做的事情时,定位不再是 1:1。

让我们走得更远。场景的原点位于左下角,根据您的场景大小和缩放比例,它可能会在屏幕外。这是 0,0 所在的位置。因此,您添加的每个 child 都将从那里开始,并根据位置正确向上工作。 SKSpriteNode 的原点在中心。例如,您的背景原点位于中心。请注意如何将位置设置为框架宽度和高度的一半。它的原点在中间,你把它移到右边,从左下角向上移动到正确的距离。现在,当您向该节点添加子节点时,它会从其父节点的原点开始。在这个例子中的中心。所以这就是为什么当你向背景添加一个球时你必须给它负值才能下降到左下角。

至于节点没有出现。如果您查看 ViewController,您可能会看到 ignoresSibling order 或类似性质的东西。这意味着您必须设置节点的 zPosition。较低的 zPositions 首先被渲染,因此背景通常为 0,所有显示在背景之上的节点的 zPosition 为 1 或更高。

希望对您有所帮助。尽管您的问题有详细记录,但很难在一个答案中回答所有问题。将来我会建议您在第一个问题得到解答后将您的问题分解并提出一个新问题,因为您甚至可能在有机会提问之前就知道了答案 =)

关于ios - SpriteKit 中的坐标系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34076632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com