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

查看:115
本文介绍了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天全站免登陆