简体   繁体   中英

swift background color + button/image bug

I have just encountered a really odd bug when testing my app.

import Foundation
import SpriteKit
import AVFoundation

class EndScene : SKScene {

var Highscore : Int!
var ScoreLabel : UILabel!
var HighscoreLabel : UILabel!
var AudioPlayer = AVAudioPlayer()

override func didMoveToView(view: SKView) {

    scene?.backgroundColor = UIColor.grayColor()

    var EndGameMusic = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("endSong", ofType: "wav")!)
    AudioPlayer = AVAudioPlayer(contentsOfURL: EndGameMusic, error: nil)
    AudioPlayer.prepareToPlay()
    AudioPlayer.play()

    var scoreDefault = NSUserDefaults.standardUserDefaults()
    var Score = scoreDefault.valueForKey("Score") as! NSInteger

    var HighscoreDefault = NSUserDefaults.standardUserDefaults()
    Highscore = HighscoreDefault.valueForKey("Highscore") as! NSInteger

    NSLog("\(Highscore)")

    ScoreLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 30))
    ScoreLabel.center = CGPoint(x: view.frame.size.width, y: view.frame.size.width/4)
    ScoreLabel.text = "Last Game:\(Score)"
    self.view?.addSubview(ScoreLabel)

    HighscoreLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 30))
    HighscoreLabel.center = CGPoint(x: view.frame.size.width, y: view.frame.size.width/2)
    HighscoreLabel.text = "Highscore: \(Highscore)"
    self.view?.addSubview(HighscoreLabel)

    let RestartBtn = SKSpriteNode(imageNamed: "RestartBtn")
    RestartBtn.position = CGPointMake(size.width/2, size.height/2)
    RestartBtn.name = "Restart"
    addChild(RestartBtn)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */
        for touch: AnyObject in touches {
            let touchLocation = touch.locationInNode(self)
            let touchedNode = self.nodeAtPoint(touchLocation)
            if(touchedNode.name == "Restart"){
                Restart()
            }
        }
}
func Restart(){
    self.view?.presentScene(InGame(), transition: SKTransition.crossFadeWithDuration(0.3))
    HighscoreLabel.removeFromSuperview()
    ScoreLabel.removeFromSuperview()
}
}

what happens is that when this scene is called, the two labels, highscore and score appear, but my background goes a yellow color and my image i'm using as a button, isn't present, yet if i touch anywhere on the screen (regardless of bounds, I've tried) it will activate the button and exit.

if i remove this code:

let RestartBtn = SKSpriteNode(imageNamed: "RestartBtn")
RestartBtn.position = CGPointMake(size.width/2, size.height/2)
RestartBtn.name = "Restart"
addChild(RestartBtn)

Then the bug doesn't occur and the background sets normally, so i know there is an issue with this code, and assumed that for some reason it was zoomed in on my image (cant post without 10 reputation...)

However, after all attempts at resizing and fixing this issue, nothing actually helped, but i still cant help thinking that this has a relatively simple solution, its just that this code was used in my previous scenes without any issue.

Any Suggestions?

EDIT: i am certain that its taken the color of my image and used that as the background, and made the entire background the restart button, i just tried it with another image styled differently.

EDIT 2: image size = 200x80, the image i used in my first edit i scaled down to 40x80

This is not a bug, so basically the way SKSpriteNode's work is they take on the size of your image file since you did not explicitly set the size anywhere else. From the code you have, I'm guessing your image is over 1000 by 1000 pixels, so it is taking up the entire screen. The iPhone 6's size for example, is something like 667 by 375 pixels within xCode so in a sense your restart button is actually larger than the screen. To fix this, set the xScale and yScale values low and see what works(makes the size of the sprite smaller)

RestartBtn.xScale = 0.01
RestartBtn.yScale = 0.01

That would make the button 100x smaller(or 1/100 the previous size), just play around with the values until you get a size you like.

Edit: So I had this issue before as well in spriteKit. For some reasons the size of the screen gets messed up unless you set its size where you declare the scene.

(What you called your screen).size = skView.bounds.size

For example if you did like

let skView = self.view as SKView!
myScene = GameScene()
myScene.size = skView.bounds.size
skView.presentScene(myScene)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM