我如何在Scene中放置对象? SpriteKit

时间:2014-08-28 11:38:23

标签: 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 :(得分:0)

您正在按键创建新实例,存储对场景的弱引用并从所希望的实例调用该方法。

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

现在请尝试以下方法:

@IBAction func addOneCircle(sender: AnyObject) {
    let scene = ((self.view as SKScene).scene as GameScene)
    scene.circlePhysicsDefault()
}

或者可能改变"让"到" var"