Reputation: 1768
I am new to swift and SpriteKit.
I loaded a image and i am trying to position the image on the top, but the image is "cutted" on the top
As you can see on the image, the window maxed, so it dont seems to be scaling problem. It seems something "eats" the first 40 pixels, i am using the Iphone 4s Emulator. If i change to the iphone 6 it "eats" more pixels on the top, is there something that "uses the top pixels like the info bar" ?
The code i am using is, something like that, on my code i loop on the images:
options.append(SKSpriteNode(imageNamed:"help"))
//Reload anchor point to top up
options[0].anchorPoint=CGPoint(x:0, y:1)
//try to position the image on the top... BUT IT CUTS MY IMAGE ON THE TOP
options[0].position = CGPoint(x: 0, y: 0)
self.addChild(options[0])
//Set the scene configuration
self.backgroundColor=UIColor(hue: 0.33, saturation: 0.47, brightness: 0.91, alpha: 1)
self.anchorPoint=CGPoint(x: 0, y: 1)
Upvotes: 0
Views: 147
Reputation: 8130
i think you're making things more difficult by changing the anchorpoint of everything around. It makes it harder to conceptualize the coordinate system. You should try to avoid changing the anchor point for the scene itself if you're going to be placing sprites directly on it.
Usually you'll change the anchor point when you want to make the sprite rotate around it's corner instead of it's center. Things like that. If you're just positioning things it makes more sense to keep the default.
This is the easiset way to make sure our scene has the correct coordinates
GameViewController.swift
import UIKit
import SpriteKit
class GameViewController: UIViewController {
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)
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
} else {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
GameScene.swift
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
self.backgroundColor = SKColor.clearColor()
let sprite = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 20, height: 20))
// position sprite on top left corner of screen
sprite.position = CGPoint(x: sprite.size.width/2, y: self.size.height - sprite.size.height/2)
self.addChild(sprite)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
try using this as a starting point. By default spritekit is setup to use an sks file (level editor file) as a template for your scene. I don't think you're using that. With this code we make sure our scene is initialized with correct dimensions when its created.
Upvotes: 1