在iOS上的SpriteKit中,缩放纹理精灵会产生错误的帧吗? [英] In SpriteKit on iOS, scaling a textured sprite produces an incorrect frame?

查看:109
本文介绍了在iOS上的SpriteKit中,缩放纹理精灵会产生错误的帧吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习SpriteKit游戏开发,以获取乐趣&我遇到了一个看似简单的问题,使我感到困惑.

I'm learning SpriteKit game development for the fun of it & I've run across a seemingly simple problem that has me stumped.

基本上,在缩放纹理化的SKSpriteNode之后,框架不是我期望的.我已经找到了一些将其强制执行到我想要的方法的方法,但是我试图了解正在发生的事情.任何想法表示赞赏!

Basically, after I scale a textured SKSpriteNode, the frame is NOT what I expect. I have figured out a few hacks to force it to what I want, but I'm trying to understand what is going on. Any ideas appreciated!

这是我的代码,没有缩放比例:

Here's my code WITHOUT SCALING:

func addSpaceship()
{
    let spaceship = SKSpriteNode.init(imageNamed: "rocketship.png")
    spaceship.name = "spaceship"
//  spaceship.setScale(0.50)

    let debugFrame = SKShapeNode.init(rect: spaceship.frame)
    debugFrame.strokeColor = SKColor.greenColor()
    spaceship.addChild(debugFrame)

    spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - 150)

    self.addChild(spaceship)


}

我的应用程序如下所示:

And my app looks like this:

现在,如果我在对其进行缩放的代码行中进行评论(spaceship.setScale(0.50)),我将得到:

Now, if I comment back in the line of code which scales it (spaceship.setScale(0.50)), I get this:

请注意,飞船在第二张图像中按比例缩小,但帧被缩小得更小.为什么?

Notice that the spaceship is scaled down in the second image, but the frame is scaled even smaller. Why?

如果将缩放线移至 之后,则将飞船添加到场景中,它可以达到我的期望,但这似乎是错误的:

If I move the scaling line to after I add the spaceship to the scene, it does what I expect, but that seems wrong:

这是在将飞船添加到场景之后调用setScale的代码:

Here's the code with setScale called after adding the spaceship to the scene:

func addSpaceship()
{
    let spaceship = SKSpriteNode.init(imageNamed: "rocketship.png")
    spaceship.name = "spaceship"

    let debugFrame = SKShapeNode.init(rect: spaceship.frame)
    debugFrame.strokeColor = SKColor.greenColor()
    spaceship.addChild(debugFrame)

    spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - 150)

    self.addChild(spaceship)
    spaceship.setScale(0.50)


}

这就是运行我的应用程序的样子:

And here is what running my app looks like then:

这行得通,但是为什么有必要呢?

So that works, but why is it necessary?

以下已建议,这是SKShapeNode的错误.但是,用SKLabelNode替换SKShapeNode具有相同的问题:

It has been suggested below, that this is a bug with SKShapeNode. But, replacing the SKShapeNode with an SKLabelNode has the same problem:

func addSpaceship()
{
    let spaceship = SKSpriteNode.init(imageNamed: "rocketship.png")
    spaceship.name = "spaceship"
    spaceship.setScale(0.50)

    let scoreNode = SKLabelNode(text: "100")
    scoreNode.position = CGPointMake(CGRectGetMidX(spaceship.frame), CGRectGetMaxY(spaceship.frame))

    scoreNode.fontColor = SKColor.redColor()
    scoreNode.fontSize = 15.0
    scoreNode.fontName = "Monaco"
    scoreNode.zPosition = 10.0
    spaceship.addChild(scoreNode)


    spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - 150)

    self.addChild(spaceship)
}

这给了我们

目的是将得分标签(scoreNode)居中放置在火箭上方 ,但是正如您所看到的,它位于顶部舷窗的顶部.在我调用spaceship.setScale之后,飞船的框架出了点问题.

The intent is to have the score label (scoreNode) centered above the rocket, but as you can see it is on top of the top porthole. There is just something wrong with the spaceship's frame after I call spaceship.setScale.

我还有一个发现:在将飞船添加到场景中之后,不需要setScale调用.仅在将debugFrame/scoreNode添加到飞船之后才需要.如果我在那一点之后设置了比例,一切都很好:

I have made one additional discovery: the setScale call does not need to be after I add the spaceship to the scene. It just needs to be after I add the debugFrame/scoreNode to the spaceship. If I setScale AFTER that point, all is well:

func addSpaceship()
{
    let spaceship = SKSpriteNode.init(imageNamed: "rocketship.png")
    spaceship.name = "spaceship"

    let scoreNode = SKLabelNode(text: "100")
    scoreNode.position = CGPointMake(CGRectGetMidX(spaceship.frame), CGRectGetMaxY(spaceship.frame))

    scoreNode.fontColor = SKColor.redColor()
    scoreNode.fontSize = 15.0
    scoreNode.fontName = "Monaco"
    scoreNode.zPosition = 10.0
    spaceship.addChild(scoreNode)

    spaceship.setScale(0.50)

    spaceship.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - 150)

    self.addChild(spaceship)
}

其结果是:

推荐答案

您的问题可能在以下两行中:

Your problem may be in the order of these two lines :

  spaceship.setScale(0.50)
  let debugFrame = SKShapeNode.init(rect: spaceship.frame)

您按比例缩小了太空船,然后计算了按比例缩放的太空船的矩形大小.然后,在渲染矩形时,将矩形缩小到其大小的一半,即原始飞船大小的四分之一.

You scaled down the spaceship and then calculate the size of the rectangle with the scaled spaceship. Then when rendering the rectangle is scaled down to half its size which is quarter of the original spaceship size.

如果交换线路,它应该可以正常工作.

If you swap the lines, it should work as expected.

通常,最好以 real 大小进行构图,然后在将其添加到场景之前缩放整体.

In general, it is better to make compose in the real size and then scale the whole just before adding it to the scene.

这篇关于在iOS上的SpriteKit中,缩放纹理精灵会产生错误的帧吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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