[英]How to add a SKSpriteNode to the scene in Swift?
我試圖繼承SKSpriteNode
到GameObject
,我想實例化的游戲場景類的外部對象。 這是我的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()
在gameScene
之外gameScene
。 我的目標是實例化PlayerShip
類的項目符號,但我不知道如何實現。 有人有建議嗎?
如果要在GameScene之外執行此操作,則需要一個go go全局屬性,該屬性具有對GameScene的引用。 然后,您可以致電:
myGameScene.AddChild(player)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.