My SKTexture is size wrong for my SKSpriteNode

373 Views Asked by At

I have a 200 x 100 box-like menu button SKSpriteNode declared below.

menuBtn.size = CGSize(width: scrWidth * 1.5, height: 100)
menuBtn.texture = SKTexture(image: UIImage(named: "menuBtn")!)

The problem is, the hitbox for the button is still right, and the width of the SKTexture is fine, but the height of the SKTexture is around 1/2 the size of the actual menuBtn. It is a png, and I've checked and there's no clear textures around the sprite (just the transparent png ones), what am I doing wrong?

Pic of button: https://i.stack.imgur.com/LJhYg.jpg

Pic of button in App: https://i.stack.imgur.com/z12eE.jpg

Pic of how I want it to look: https://i.stack.imgur.com/cQL8h.jpg

The texture's png image size is 1044x1044 if that has any impact.

2

There are 2 best solutions below

2
JustSomeoneWhoCodes On BEST ANSWER

The problem I had was that there were extra transparent pixels in my base image, all I had to do was remove them with GIMP.

4
Alexandru Vasiliu On

First of all, make sure you are sending the correct size to your scene from GameViewController to your scene, in this example GameScene.

// without .sks

class GameViewController: UIViewController 
{
    override func viewDidLoad() 
    {
        super.viewDidLoad()

        // ...        

        if let view = self.view as! SKView?
        {
            let scene = GameScene()

            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFit

            // Set anchorPoint and pass scene.size
            scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
            scene.size = view.bounds.size

            // Present the scene
            view.presentScene(scene)
        }

        // ...
}

// with .sks

class GameViewController: UIViewController 
{
    override func viewDidLoad() 
    {
        super.viewDidLoad()

        // ...   

        if let view = self.view as! SKView? 
        {
            if let scene = SKScene(fileNamed: "GameScene.sks") 
            {
                // Set the scale mode to scale to fit the window
                scene.scaleMode = .aspectFit

                // Pass scene.size
                scene.size = view.bounds.size

                // Present the scene
                view.presentScene(scene)
            } 
        }

        // ...
}

In your GameScene create the object. I recommend working in screenWidth and screenHeight when you create objects so they scale to all iOS devices.

class GameScene: SKScene 
{
    override func didMove(to view: SKView) 
    {
        // ...

        // scene width / height
        let sceneWidth = size.width
        let sceneHeight = size.height

        // object
        let menu : SKSpriteNode = SKSpriteNode()
        menu.texture = SKTexture(imageNamed: "menu")
        menu.size = CGSize(width: sceneWidth * 0.75, height: 100)
        let y_pos : CGFloat = -sceneHeight * 0.5 + menu.size.height * 0.5
        menu.position = CGPoint(x: 0, y: y_pos)
        menu.zPosition = 1
        addChild(menu)

        // ...
   }
}