0
votes

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://imgur.com/a/8BtXGLC

Pic of button in App: https://imgur.com/a/ZfW3xDL

Pic of how I want it to look: https://imgur.com/a/2zlSv8y

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

2
Can you show a picture of everything you mentioned?Knight0fDragon
What are the dimensions of the png?Lou Franco
@Knight0fDragon sure i'll add an imgur linkSizzyl
@LouFranco Idk, how do I check that?Sizzyl
What is scrWidthKnight0fDragon

2 Answers

0
votes

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)

        // ...
   }
}
0
votes

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.