SKNode 位置不工作,总是默认 0,0 [英] SKNode position not working, always going to default 0,0

查看:19
本文介绍了SKNode 位置不工作,总是默认 0,0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发相当完善的 SpriteKit 游戏,添加了一些代码以将新的 SKSpriteNode 添加到场景中,(与我在代码中的其他几十个地方所做的相同,但出于某种原因,这次是只是忽略了我为节点设置的位置,我已经通过显式尝试了 .position = CGPointMake(x,y) 并尝试了 [node setPosition:CGPointMake(x,y)]

I have fairly well developed SpriteKit game I'm working on, added some code to add a new SKSpriteNode to the Scene, (same as I have done dozens of other places in the code, but for some reason this time it is just ignoring the position I have set for the node, I have tried it by explicit .position = CGPointMake(x,y) and trying the [node setPosition:CGPointMake(x,y)]

都不工作...节点确实被添加到场景中,但总是在 0,0,无论我做什么,都检查了 x 和 y 以确保它们有效并且在场景的范围内, 并尝试使用显式浮点数等,不管我做什么,这个节点总是显示在 0,0,这绝对没有意义.在玩游戏的过程中,我所做的一切都与我对数十个其他节点所做的一样,但是无论我做什么,这个节点都不会出现在任何地方,除了 0,0:

Neither work... the node does get added to the scene, but ALWAYS at 0,0, no matter what I do, have checked both the x and y to make sure they are valid and in the range of the scene, and tried to use explicit floats etc, and it doesn't matter what I do, this node always shows up at 0,0 and it makes absolutely no sense. I am doing everything the same I am doing with dozens of other nodes over the course of game play but this node just won't show up anywhere but 0,0 no matter what I do:

int width = 64;
SKSpriteNode *node = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(width,25)];
node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:node.size];
node.physicsBody.dynamic = NO;
node.physicsBody.categoryBitMask=nodeCategory;
node.name=@"node";
node.position = CGPointMake(200,250);
//Have also tried [node setPosition:CGPointMake(200,250);
[self addChild:node];

我在这里完全不知所措,我尝试将 .0 添加到 CGPointMake 的输入中,以确保它们被处理为浮点数...无论我如何处理这个节点,当它被添加到场景它总是默认为 0,0 位置,我很沮丧,我添加了各种节点而没有发生任何事件,只是添加了一些关于这个节点的东西,它只是不工作,无法弄清楚有什么不同或为什么.

I am completely at a loss here, I've tried adding .0 to the inputs to the CGPointMake to ensure they are handled as floats nothing... No matter what I do with this node, when it is added to the scene it always defaults to the 0,0 position, and I am frustrated, I am adding all sorts of nodes without incident, just something about this node add that is just not working and can't figure out what is different or why.

推荐答案

我也遇到了同样的问题.我有一个 SKSpriteNode 的子类,我的大多数游戏内节点都将它用作超类.我添加了这个方法,每当我遇到定位问题时都会使用它:

I've been having this same issue. I have a subclass of SKSpriteNode that most of my in-game nodes use as a superclass. I've added this method, which I use whenever I run into the positioning problem:

- (void)refreshPhysicsBodyAndSetPosition:(CGPoint)position {

    /*
     * Weird thing here: if I just set the position of these nodes, they
     * end up at position (0,0). However, if I remove the physics body, set
     * the position, and then re-add the physics body, the nodes are
     * placed correctly.
     */

    SKPhysicsBody *tempPhysicsBody = self.physicsBody;
    self.physicsBody = nil;

    // Position and re-add physics body
    self.position = position;
    self.physicsBody = tempPhysicsBody;
}

显然,这不是问题的真正答案,但它是一种可能对你们中的一些人有所帮助的解决方法.我在 Apple 开发者论坛上提出了一个问题,您可以在此处查看:

Obviously, this isn't a real answer to the issue, but it's a workaround that may help some of you. I have a question opened on the Apple Developer forum, which you can view here:

https://devforums.apple.com/thread/222332

希望我能尽快收到回复.

Hoping I hear back soon.

这篇关于SKNode 位置不工作,总是默认 0,0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆