2
嗨,我是新来的精灵套件,并且由于某些原因,使用self.frame.size.width或.height将按钮或子视图定位在场景中时,它们未居中而是有时候完全脱离屏幕。有谁知道如何将框架大小设置为iPhone/iPad的使用大小?将场景的框架设置为iPhone屏幕的大小
这里是我的代码呈现子视图时,当球员失去
func presentGameOverView() {
if firstTime == true {
let gameOverView = UIView()
gameOverView.frame = CGRectMake(self.frame.size.width/2.0, self.frame.size.height/2.0, 200, 200) //should be center of screen
gameOverView.backgroundColor = UIColor.redColor()
playGameOverSound()
self.view?.addSubview(gameOverView)
firstTime = false
}
}
这里是gameViewController.swift
extension SKNode {
class func unarchiveFromFile(file : NSString) -> SKNode? {
if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)
archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene
archiver.finishDecoding()
return scene
} else {
return nil
}
}
}
类GameViewController的一部分:UIViewController中{
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// 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 */
scene.scaleMode = .AspectFill
//display banner Ads
//self.canDisplayBannerAds = true
skView.presentScene(scene)
}
}
是啊,但由于某种原因未在gamescene出现。我不知道为什么,但框架是比屏幕尺寸大得多从未有过这种情况发生之前,当我println(self.frame.size.width) println(self.frame.size.height) 我得到1024和768 – 2015-02-10 00:34:22
你使用的是sks文件吗? – rakeshbs 2015-02-10 00:34:59
当我创建一个精灵场景GameScene.sks是在项目文件夹...但我所有的代码是在GameScene.swift – 2015-02-10 00:37:58