2014-08-27 71 views
0

文件GameScene上課GameScene,類GameScene有CirclePhysicsDefault:廣場對象,SpriteKit

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 I型:

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

我聯繫這按鈕。

運行應用程序,按下按鈕 - 沒有任何反應。

如果在GameScene函數didMoveToView中我調用函數circlePhysicsDefault,那麼將放置應用程序啓動圓。

文件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 } 
} 

文件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) 
    } 
} 

回答

2

你按下按鈕的方法:

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

不跟你前面創建的場景東西。方法circlePhysicsDefault()從另一個GameScene類對象中調用。這就是爲什麼你的場景中沒有任何物體。爲了解決這個問題,你可以將指針存儲到你的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() 
     } 
    } 
....... 
....... 
....... 
}