如何在Swift中向场景中添加SKSpriteNode?

时间:2015-11-24 12:15:19

标签: swift sprite-kit subclass skspritenode addchild

我尝试将 await cc.Entry<Product>(product).ReloadAsync(); 子类化为SKSpriteNode,我想在游戏场景类之外实例化对象。以下是源自GameObject的{​​{1}}代码:

GameObject

要实例化从SKSpriteNode派生的玩家,我必须写:

import SpriteKit

public class GameObject: SKSpriteNode {

    init( texture: SKTexture?, color: UIColor, size: CGSize, position:CGPoint, name:String ) 
    {   
        objectSize = size;
        objectName = name;
        objectSprite = texture;
        //call superclass here
        super.init(texture: texture, color: color, size: size);
        self.position = position;
    }

    convenience init(_ _x:CGFloat, _ _y:CGFloat, _ _object:String )// Default initializer
    {
        let texture = SKTexture(imageNamed: _object);
        let position = CGPoint(x:_x, y:_y);
        self.init(  texture: texture,color: UIColor(),size: texture.size(), position: position, name: _object);
    }

    //Overloaded initializer with size as extra argument
    convenience init(_ _x:CGFloat, _ _y:CGFloat, _ _size:Int, _ _object:String)
    {
        //size for the SKSpriteNode.
        let texture = SKTexture(imageNamed: _object);
        let position = CGPoint(x:_x, y:_y);
        self.init(  texture: texture, color: UIColor(), size: CGSize(width: _size, height: _size),position: position, name: _object);
    }

但是,GameObjectlet player = PlayerShip(100, 100, "PlayerShip") addChild(player) 之外无效。我的目标是从addChild()类实例化子弹,但我无法弄清楚如何。有没有人有建议?

1 个答案:

答案 0 :(得分:1)

如果你想在GameScene之外做这件事,你需要一个go world全局属性,它引用了GameScene。然后你可以打电话:

myGameScene.AddChild(player)