0
我試圖將SKSpriteNode
轉換爲GameObject
,我想實例化遊戲場景類以外的對象。下面是從SKSpriteNode
衍生我GameObject
代碼:如何在Swift中添加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()
不向外gameScene
工作。我的目標是從PlayerShip
類實例化子彈,但我無法弄清楚如何。有沒有人有建議?
從問題中不清楚你想要做什麼。你在什麼時候調用這個方法'addChild'?還有更多的代碼在你稱之爲的地方。更詳細地描述創建對象的目的。所以你不是在場景和另一個物體上創建它們,然後將它們添加到場景中? –