0

I'm trying to do something that seems straightforward, but I'm getting "use of unresolved identifier" instead of the variable I'm expecting.

In one class I'm instantiating an object:

let sprite = Hero(view: view)

View is a valid variable, and I can po it in this class.

In Hero, I have my init function setup as follows:

init(view: SKView) {
    ...
}

But when I try to po view from this initializer, I get the "use of unresolved identifier" error. Any idea why?

EDIT:

Here's all my relevant code:

GameScene.swift

import SpriteKit

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        let sprite = Hero(view: view)
        self.addChild(sprite)
    }
}

Hero.swift

import SpriteKit

class Hero: SKSpriteNode {
    init(view: SKView) {

        let texture = SKTexture(imageNamed:"Spaceship")
        let color = SKColor(white: 0, alpha: 0)
        let size = CGSizeMake(100, 100)

        super.init(texture:texture, color:color, size:size)

        self.xScale = 1.0
        self.yScale = 1.0
        self.position = CGPointMake((view.bounds.width / 2) - (size.width / 2), view.bounds.height - 200)

    }
}

Everything about this code works, the spaceship will show up if I set the position to, let's say CGPointMake(200,200), only view is unresolved in Hero. I'm passing view so that Hero knows the bounds of the view it's being added to and can center itself.

3
  • 2
    did you try let sprite = Hero( view)? Commented Jun 30, 2014 at 2:00
  • 1
    can we see some more of what is in the unit method? (up to the point where you entry to po view) Commented Jun 30, 2014 at 2:32
  • 1
    Why are you using the debugger (which is pretty much broken in Swift)? Does the code compile? Does it run? What's the actual problem? Commented Jun 30, 2014 at 4:32

1 Answer 1

1

So, I got the code to perform the way I was expecting. I ended up changing Hero's init definition to look like this:

init(addToView view: SKView)

And then initialized a new object by doing:

let hero = Hero(addToView: view)

Now, everything works as I expect, view is passed to init as a resolved variable. But what's funny is @connor's recommendation to use let sprite = Hero(view) should have worked when init was defined as init(view: SKView). "view:" in that case was a local parameter name and should not have been required in the function call. At least, that's what I'm lead to believe based on the Functions chapter of The Swift Programming Language book. I have no idea why Xcode complained about a missing "view:" in the function call, which would have been an external parameter name never listed in the init definition.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.