我检查了 fireLaser() 函数是否被调用,因为它可以被访问并打印到控制台,但激光从未添加到场景中。如果我将代码复制并粘贴到 fireLaser() 函数中,它可以在 didMoveToView() 函数中使用。
let character = SKSpriteNode(imageNamed: "monkey")
let laser = SKSpriteNode(imageNamed: "bullet_yellow")
override func didMoveToView(view: SKView) {
var background : SKSpriteNode = SKSpriteNode (imageNamed: "space.png")
background.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2)
background.size = self.frame.size
self.addChild(background)
character.position = CGPoint(x: size.width * 0.5, y: size.height * 0.25)
addChild(character)
//Shoots Laser From Character
func fireLaser(){
laser.position = character.position
addChild(laser)
let laserDestination = CGPoint(x: character.position.x, y: self.size.height)
let actionMove = SKAction.moveTo(laserDestination, duration: 2.0)
let actionMoveDone = SKAction.removeFromParent()
laser.runAction(SKAction.sequence([actionMove, actionMoveDone]))
println ("check")
}
//从游戏视图控制器
@IBAction func firePressed(){
GameScene().fireLaser()
}