设置我的球的位置

时间:2014-03-24 04:20:09

标签: ios iphone objective-c ios7 sprite-kit

我正在学习如何使用 SpriteKit

问题:

作为练习,我试图模仿乒乓球。我希望球的初始位置在中心位置,而对于我的生命,我无法确定其位置在中心位置。它总是最终出现在屏幕的左下角,我不知道为什么。我已将场景的缩放模式设置为SKSceneScaleModeAspectFill,并且我已在单独的试验中尝试了以下每行代码,以便将其位置设置为中心,但它始终保持在左下角。

    //Attempt #1
    self.ball.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)); //find the middle of each axis and put them together in one coordinate
    //Attempt #2
    self.ball.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); //find the width and height of the screen, divide each by 2 and assign them to an (x,y) coordinate together
    //Attempt #3
    self.ball.position = CGPointMake(160, 284) //Half the width and half the height
    //Attempt #4
    self.ball.position = CGPointMake(320, 568) //The width and the height (I'm desperately guessing at this point)
    //Attempt #5
    self.ball.position = CGPointMake(0,0) //I figured I'd give it a go

这个球是SKSpriteNode,就像我没有制作Ball的{​​{1}}子类或任何东西一样。关于这一点的奇怪之处在于,我能够在屏幕上调出其他精灵,比如拨片和得分,正好在我想要的地方。只是这个球没有合作。我究竟做错了什么?我如何在将来阻止这个问题?

1 个答案:

答案 0 :(得分:0)

如果你想让球的初始位置居中,那么写下以下代码:

 [self.ball setPosition:CGPointMake(self.size.width/2, self.size.height/2)];

我在我的代码中使用了上面的代码,它给了我完美的结果。

让我知道它有效吗?

快乐的编码!