How to interact with a SKSpriteNode after another one of the same var name has been created?

I appologise for the confusing question title, but I wasn't quite sure how to phrase my question. My ultimate goal is to remove an SKSpriteNode a certain amount of time after it's creation. However, there is a small complication. Using the following code

func remove() {


I'm able to remove the SKSpriteNode I named 'laser'. When I call my function once, fireLasers(), everything runs smoothly and the laser disappears after 5 seconds. The problem is when I call it twice within a period of 5 seconds. When this happens, the first laser will stick around indefinitely and the second disappears earlier than intended. I understand why this happens, but would like to know if there's a way around it. Here's the code for the GameScene which creates the world, background image, player Sprite, and defines the fireLasers function. There's a lot of stuff that goes on in the ViewController that works with the velocities and directions of the sprites, but I hope this is sufficient to find a solution.

import SpriteKit

class GameScene: SKScene {

var player = SKSpriteNode()
var world = SKShapeNode()
var worldTexture = SKSpriteNode()
var laser = SKShapeNode()

override func didMoveToView(view: SKView) {
    self.anchorPoint = CGPointMake(0.5, 0.5)
    self.size = CGSizeMake(view.bounds.size.width, view.bounds.size.height)

    // Add world
    world = SKShapeNode(rectOfSize: CGSizeMake(7000, 7000))
    world.physicsBody = SKPhysicsBody(edgeLoopFromPath: world.path!)
    world.fillColor = SKColor.blackColor()

    // Add Background Image
    worldTexture = SKSpriteNode(imageNamed: "grid")
    worldTexture.size = CGSize(width: 7000, height: 7000)


    // Add player
    player = SKSpriteNode(imageNamed: "Spaceship")
    player.size = CGSize(width: 50, height: 50)
    player.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 50, height: 50))
    player.physicsBody?.affectedByGravity  = false
    player.physicsBody?.dynamic = true


override func update(currentTime: CFTimeInterval) {
    world.position.x = -player.position.x
    world.position.y = -player.position.y

func fireLasers() {
    laser = SKShapeNode(rectOfSize: CGSizeMake(10, 50))
    laser.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 10, height: 50))
    laser.position.x = player.position.x
    laser.position.y = player.position.y + CGFloat(50)
    laser.fillColor = SKColor.redColor()
    laser.physicsBody?.dynamic = true
    laser.physicsBody?.affectedByGravity  = false

    func remove() {




Instead of keeping a reference to the laser (which will, as you noticed, force you to only have one around at a time), why not run an action on the created laser nodes themselves? This will allow you to use the handy SKAction class method removeFromParent():

let newLaser = makeNewLaser()
let laserAction = SKAction.sequence([SKAction.waitForDuration(5), SKAction.removeFromParent()])

Need Your Help

NHibernate: best moment to store GUI changes

c# nhibernate user-interface

I am a little bit confused with NHibernate and Session. As I understood, I am supposed to use sessions "as quickly as possible", meaning that two different interactions get their own session.

SQL query from php not use mysql cache

php mysql caching

The Mysql server is set to cache:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.