Issues scaling sprite width to screen width

天大地大妈咪最大 提交于 2019-12-23 10:06:45

问题


Practically I am trying to achieve not too different from what was done in this question (scale a sprite regardless of it's position on the screen till it fills the screen width). Please see my code below:

      if touch?.tapCount == 2  {  
        self.view?.isUserInteractionEnabled = false

        mySprite?.sprite.anchorPoint = CGPoint(x: (mySprite?.sprite.position.x)! / self.size.width, y: (mySprite?.sprite.anchorPoint.y)!)
        let scaleSpriteAction = SKAction.scaleX(to: self.size.width / (mySprite?.sprite.size.width)!, duration: 0.1)
        mySprite!.sprite.run(scaleSpriteAction)

        delay(0.1)  {
            centerPoint = CGPoint(x: mySprite!.sprite.size.width / 2 - (mySprite!.sprite.size.width * mySprite!.sprite.anchorPoint.x), y: mySprite!.sprite.size.height / 2 - (mySprite!.sprite.size.height * mySprite!.sprite.anchorPoint.y))

            mySprite!.sprite.physicsBody = SKPhysicsBody(rectangleOf:    mySprite!.sprite.size, center: centerPoint)
            mySprite!.sprite.physicsBody!.categoryBitMask = mySpriteCategory
            mySprite!.sprite.physicsBody!.isDynamic = false

        }
     }

The sprite expands but it doesn't always fill the whole screen. From my observation it happens more frequently when the sprite is closer to the left or right ends of the screen. After logging the sprite positions before and after the delay and realizing they were different, I disabled user interaction but that didn't change the anything. The delay ensures that the physicsBody also scales to the full screen width.

What could be causing the sprite expansion inconsistency (not always filling the screen width) and how can I get it to fill the whole width every time?

来源:https://stackoverflow.com/questions/40458673/issues-scaling-sprite-width-to-screen-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!