Swift SpriteKit: Use of unresolved identifier "player"?

swift sprite kit documentation
spritekit games
spritekit platformer
spritekit view
spritekit object
gameviewcontroller spritekit
spritekit ui
spritekit gesture recognizer

When I try to run an action on a sprite I've set a constant for in didMoveToView, I use the same name for it in the touchesBegan function and get a "use of unresolved identifier: "player" error. I have another game where I so the exact same thing and it runs perfectly. Need help! Here is my code:

import SpriteKit

class GameScene: SKScene {

    var movingGround : MCTGround!
    var fruitGenerator : MCTFruitGen!
    var cloudGenerator: MCTCloudGen!

    var isStarted = false

    override func didMoveToView(view: SKView) {

        let player = SKSpriteNode(imageNamed: "koala_idle")
        player.position = CGPointMake(95, 150)
        addChild(player)

        backgroundColor = UIColor(red: 159.0/255.0, green: 201.0/255.0, blue: 244.0/255.0, alpha: 1.0)
        movingGround = MCTGround(size: CGSizeMake(view.frame.width, 20))

        movingGround.position = CGPointMake(0, view.frame.size.height / 4)

        addChild(movingGround)

        fruitGenerator = MCTFruitGen(color: UIColor.clearColor(), size: view.frame.size)
        fruitGenerator.position = view.center

        addChild(fruitGenerator)

        let frames = [
            SKTexture(imageNamed: "koala_idle"),
            SKTexture(imageNamed: "koala_walk01"),
            SKTexture(imageNamed: "koala_walk02"),


        ]

        let duration = 1.5 + drand48() * 1.0

        let move = SKAction.animateWithTextures(frames, timePerFrame:0.10)
        let wait = SKAction.waitForDuration(duration)
        let rest = SKAction.setTexture(frames[0])
        let sequence = SKAction.sequence([move, rest])

        player.runAction(SKAction.repeatActionForever(sequence))

        cloudGenerator = MCTCloudGen(color: UIColor.clearColor(), size: view.frame.size)
        cloudGenerator.position = view.center
        addChild(cloudGenerator)
        cloudGenerator.populate(7)
        cloudGenerator.startGeneratingWithSpawnTime(1)
    }


    func start() {
        isStarted = true

        cloudGenerator.startGeneratingWithSpawnTime(1)
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event:   UIEvent) {
        jumpPlayer()
        movingGround.start()

        fruitGenerator.startGeneratingFruitEvery(1)
    }

    override func update(currentTime: CFTimeInterval) {

    }


    func jumpPlayer() {
        let jumpUpAction = SKAction.moveByX(0, y: 40, duration: 0.5)
        let jumpDownAction = SKAction.moveByX(0, y: -40, duration: 0.5)

        let jumpSequence = SKAction.sequence([jumpUpAction, jumpDownAction])
        player.runAction(jumpSequence) // This is where my error is
    }
}

Make player a class scope variable (property). Declare it not in a function, but a level above.

var player: SKSpriteNode?

override func didMoveToView(view: SKView) {

    player = SKSpriteNode ...

then access it via player?. The safest way.

SpriteKit Introduction Part 1: Scenes, Nodes, Labels and Points , Fire up Xcode and choose the Game template from the iOS project error at this point: Use of unresolved identifier “GameScene” because you  A.swift. class A { class ARelated { } } calling var b = ARelated() will give ‘Use of unresolved identifier: ARelated’. You can either: 1) separate the classes if wanted on the same file: A.swift. class A { } class ARelated { } 2) Maintain your same structure and use the enclosing class to get to the subclass: var b = A.ARelated

Your problem is that you are declaring player inside didMoveToView. This makes it private to the didMoveToView method. All you need to do is move let player = SKSpriteNode(imageNamed: "koala_idle") to where you declare movingGround and the other variables with it.

The beginning your code should look like this:

import SpriteKit

class GameScene: SKScene {

var movingGround : MCTGround!
var fruitGenerator : MCTFruitGen!
var cloudGenerator: MCTCloudGen!
let player = SKSpriteNode(imageNamed: "koala_idle")

var isStarted = false

override func didMoveToView(view: SKView) {

    player.position = CGPointMake(95, 150)
    addChild(player)

I am stuck, I need help, in xcode, it is saying use of unresolved identifier 'zombie', and also overri… import SpriteKit class GameScene: SKScene { var zombie:  ios,swift. It's crashing because your handler object is getting released and deallocated right after the call to handler.sendMessage(), and then a delegate callback is attempted on that now-deallocated object when you try to send or hit cancel. The object is getting released and deallocated because nothing is holding a strong reference

Go to File Inspector (right click on the file referenced in the error and select Show File Inspector). In Target Membership make sure your actual app is selected.

In my case only the Test modules were selected.

So as far as I can tell (I'm new to all this), the referenced file was hidden/unregistered with the ViewController.

Sprite Kit Swift 2 Tutorial for Beginners, Learn how to make your first iOS game in this Sprite Kit Swift 2 tutorial for complete I get an error of “Use of unresolved identifier 'size'”. SDKApplicationDelegate Use of unresolved identifier. Ask Question Asked 11 months ago. Unresolved identifier “PFFacebookUtils” in swift in Xcode6. 0.

swift, Use of unresolved identifier 'size'. Cannot invoke 'max' with an argument list of type '(CGFloat)'. Use of unresolved identifier 'size. Expected ',' separator. The Swift 3+ way to create Data (not NSData) from a CGPoint is let dataSend = Data(bytes: &positionToSend, count: MemoryLayout.size(ofValue: positionToSend)) share | improve this answer | follow | | | |

SKView, iOS 7.0+; macOS 10.9+; Mac Catalyst 13.0+; tvOS 9.0+. Framework iOS, Mac Catalyst, tvOS The animation frame rate that the view uses to render its scene. Typos aren’t the only cause of the Use of unresolved identifier error! Xcode indicates it can’t find the property or variable you’re using, but that doesn’t mean you made a typo. Xcode indicates it can’t find the property or variable you’re using, but that doesn’t mean you made a typo.

Understanding "Use of Unresolved Identifier" Error In Xcode, It's a common error in Xcode: Use of unresolved identifier. It simply means Xcode uses the Swift compiler to statically analyze your Swift code. 02/12/2019 How To Fix Warning Attempt To Present Uialertcontroller On Viewcontroller Whose View Is Not In The Window Hierarchy

Comments
  • Welcome to SO. This is not an error dump. Please ask a specific question.
  • Im asking how to fix this.
  • If you google "use of unresolved identifer swift", you get 47,300 results. If you click on the very first result, it will take you to the answer to your question.
  • Every time I declare it outside of a function, my game crashes and I get the EXC_BAD_INSTRUCTION error.
  • Ive tried this and I still get the same error. In my last game, I put it in didMoveToView and it worked in every function I put it in.
  • It works for me. I am adding what you need to do to my post.