我尝试将 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);
}
但是,GameObject
在let player = PlayerShip(100, 100, "PlayerShip")
addChild(player)
之外无效。我的目标是从addChild()
类实例化子弹,但我无法弄清楚如何。有没有人有建议?
答案 0 :(得分:1)
如果你想在GameScene之外做这件事,你需要一个go world全局属性,它引用了GameScene。然后你可以打电话:
myGameScene.AddChild(player)