将对象放在场景中,SpriteKit

时间:2014-08-27 20:56:36

标签: xcode swift

文件GameScene有类GameScene,类GameScene有CirclePhysicsDefault:

func circlePhysicsDefault() {
var Circle = SKShapeNode(circleOfRadius: 40)
Circle.position = CGPointMake(500, 500)
Circle.name = "defaultCircle"
Circle.strokeColor = SKColor.blackColor()
Circle.glowWidth = 10.0
Circle.fillColor = SKColor.yellowColor()
Circle.physicsBody = SKPhysicsBody(circleOfRadius: 40)
Circle.physicsBody.dynamic = true
self.addChild(Circle)
}

在文件GameViewController中输入:

@IBAction func addOneCircle(sender: AnyObject) {
GameScene().circlePhysicsDefault()
}

我将此链接到按钮。

运行应用程序,按下按钮 - 没有任何反应。

如果在GameScene函数中,didMoveToView我调用函数circlePhysicsDefault,那么将放置app启动圈。

File GameViewController:

import UIKit
import SpriteKit

class GameViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

/* Pick a size for the scene */
let scene = GameScene(fileNamed:"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

skView.presentScene(scene)
}

@IBAction func addOneCircle(sender: AnyObject) {
GameScene(fileNamed:"GameScene").circlePhysicsDefault()
}

override func shouldAutorotate() -> Bool {
return true
}

override func supportedInterfaceOrientations() -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.AllButUpsideDown.toRaw())
} else {
return Int(UIInterfaceOrientationMask.All.toRaw())
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}




override func prefersStatusBarHidden() -> Bool {
return false }
}

File GameScene:

import SpriteKit

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        circlePhysicsDefault()
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }

    func sceneSetting() {
        self.backgroundColor = SKColor.grayColor()
        self.physicsWorld.gravity = CGVectorMake(0.01, -2)
        }
    func circlePhysicsDefault() {
        var Circle = SKShapeNode(circleOfRadius: 40)
        Circle.position = CGPointMake(500, 500)
        Circle.name = "defaultCircle"
        Circle.strokeColor = SKColor.blackColor()
        Circle.glowWidth = 10.0
        Circle.fillColor = SKColor.yellowColor()
        Circle.physicsBody = SKPhysicsBody(circleOfRadius: 40)
        Circle.physicsBody.dynamic = true
        self.addChild(Circle)
    }
}

1 个答案:

答案 0 :(得分:2)

按下按钮的方法:

@IBAction func addOneCircle(sender: AnyObject) {
GameScene(fileNamed:"GameScene").circlePhysicsDefault()
}

对您之前创建的场景没有任何作用。从另一个GameScene类对象调用的方法circlePhysicsDefault()。这就是为什么你的场景没有对象。要解决此问题,您可以将指针存储到scen对象并使用它调用方法。

在这种情况下,你的GameViewController将如下所示:

class GameViewController: UIViewController {

    var myScene: GameScene?

    override func viewDidLoad() {
        super.viewDidLoad()

        let scene = GameScene(fileNamed:"GameScene")

        self.myScene = scene

        let skView = self.view as SKView
        skView.showsFPS = true
        skView.showsNodeCount = true
        skView.ignoresSiblingOrder = true
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)
    }

    @IBAction func addOneCircle(sender: AnyObject) {
        if self.myScene != nil {
            self.myScene!.circlePhysicsDefault()
        }
    }
.......
.......
.......
}