【问题标题】:How i can place object in Scene? SpriteKit我如何在场景中放置对象?精灵套件
【发布时间】:2014-08-28 11:38:23
【问题描述】:

文件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,那么会放置应用启动圈。

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

文件游戏场景:

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

【问题讨论】:

    标签: xcode swift


    【解决方案1】:

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

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

    所以现在请尝试以下方法:

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

    或者也许将“let”改为“var”

    【讨论】:

    • 很遗憾,什么都没发生 :( 你能给出更详细的答案吗?请
    • 我会尽快重新创建它,并在几分钟内将示例代码发送给您
    • 顺便说一句。你为什么在场景中使用 let ?不是让不可变的对象吗?其中 var 是可变的
    • 我有问题 'SKScene' 在 'scene.circlePhysicsDefault()' 上没有名为 'circlePhysicsDefault' 的成员 :(
    • 对不起我的错,我已经更新了我的帖子。你必须把场景当成GameScene,因为SKScene没有这样的方法。
    猜你喜欢
    • 2014-03-02
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    相关资源
    最近更新 更多