将精灵宽度缩放到屏幕宽度的问题 [英] Issues scaling sprite width to screen width

查看:25
本文介绍了将精灵宽度缩放到屏幕宽度的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我试图实现与 这个问题(缩放精灵,不管它在屏幕上的位置,直到它填满屏幕宽度).请看我下面的代码:

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

        }
     }

精灵会展开,但并不总是填满整个屏幕.根据我的观察,当精灵靠近屏幕的左端或右端时,这种情况发生得更频繁.在记录延迟前后的精灵位置并意识到它们不同之后,我禁用了用户交互,但这并没有改变任何东西.延迟确保 physicsBody 也缩放到全屏宽度.

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?

推荐答案

如果您想要不成比例的缩放:

If you want a disproportionate scaling:

SKAction.scaleX(to: self.size.width / mySprite!.sprite.size.width, duration: 0.1)
SKAction.scaleY(to: self.size.height / mySprite!.sprite.size.height, duration: 0.1)

1 • 在你做任何其他事情之前分配你的physicsBody.2 • 锚点应为 CGPoint(0.5, 0.5)3 • 您还需要运行 SKAction 将图像移动到屏幕中央

1 • Assign your physicsBody before you do anything else. 2 • Anchor point should be CGPoint(0.5, 0.5) 3 • You also need to run an SKAction moving your image to the center of the screen

如果您想要按比例缩放,以便图像在碰到任何边缘时停止,您需要跟踪屏幕比例和图像比例.

If you want proportionate scaling, so the image stops when it hits any edge, you'll want to keep track of the screen ratio and image ratio.

这篇关于将精灵宽度缩放到屏幕宽度的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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