问题
Recently I have been creating a game where I put circles on the screen. However, I noticed that the circulars were not circles, but in fact ovals. The y axis had been stretched of the x axis had been shrunk. In order to figure out the issue, I am now trying to debug with a rectangle. I made the rectangle have the same height and width (square) however, when turned into an skshapenode it is no longer the correct shape. The code:
var testSquare = CGRect(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame), width: 1, height: 1)
var squareNode = SKShapeNode(rect: testSquare)
println(testSquare.width)
println(testSquare.height)
println(squareNode.frame.width)
println(squareNode.frame.height)
self.addChild(squareNode)
Here is what gets printed from this:
1.0
1.0
513.5
385.5
If I change the testSquare's height and width both to 100 this is what gets printed:
100.0
100.0
612.5
484.5
For some reason the frame of the squarenode in the x adds 512.5 and in the y adds 384.5. The rectangle that is created is longer in the Y for some reason on the screen though. Maybe the frame is not the number I should be printing, but regardless the shape on the screen is NOT a square. Does anyone know why that is? Any help is greatly appreciated!
回答1:
are you using the sks file to make your SKView? Or are you instantiating it in your viewcontroller? Important to get the dimensions for your scene correct. I know this isn't directly answering your question, but if you're getting weird stretching and stuff, maybe you should start with this and debug from there..
this gives me the correct scene dimensions.
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
let scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
来源:https://stackoverflow.com/questions/27725318/skshapenode-using-wrong-width-and-height