Issues scaling sprite width to screen width

2019-06-22 07:35发布

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?

0条回答
登录 后发表回答