Having problems with self

Hi everyone. I've been trying to convert a script I wrote in Lua to Swift, but it's been a pain since I'm having a lot of problems I can't find the solution to, mainly because I only started learning Swift yesterday.

The problem I have at the moment is with the following:

// There's more code above here but it works fine
var VIEW:SKView! // Because I have no clue how to get the view otherwise

func GENERATE(SCENE:SKScene) { // Function works
     //Contents of function don't matter, but the SKScene is needed
}


class GameScene: SKScene {
     // The sequence below is used later by a SKSpriteNode (node.runAction(SEQ))
     let SEQ = SKAction.sequence([SKAction.scaleTo(0,duration:0.2),SKAction.removeFromParent(),SKAction.runBlock({
          VIEW.backgroundColor = UIColor.blueColor()
          GENERATE(self) // I need the GameScene but it won't let me use it no matter what I try
    })])

     override func didMoveToView(view: SKView) {
        VIEW = view // I don't really like doing this but it seems to be the only way
        GENERATE(self) // This works fine
    }
// There's more code down here but it works fine

As you can see, I've tried a bit of dodgy stuff to get it to work, but it doesn't. Anyone know how to get this working?

The problem arises because you're using a closure (block of commands) in the SKAction that you're creating as the initial value of the SEQ instance variable. However, at the time this initial value is set, the value of 'self' is not yet available. (You can't refer to 'self' until the object has finished its initialization, and setting SEQ is part of that initialization. Catch-22.)


Your larger problem is that you're doing things kinda inside out. You're trying to pass the object instance (self) into functions, instead of writing the behavior as methods of the object. These contortions are probably as a result of trying to "convert" your Lua script, instead of thinking in terms of how the pieces of an actual app fit together.


I suggest you start with an Xcode Sprite Kit game template (for the appropriate platform), and examine the relationships it defines for you, between the view controller, the view, and the scene. The hierarchy it uses is what's natural for this kind of app.


In particular, it's likely to be a "code smell" (an indication that there's a mis-design somewhere) if your scene needs a direct reference to the view. Generally, behaviour flows from the view to the view controller to the scene. (For example, you should set the scene's background color, not the view's background color.)

Alright, thanks for the quick reply! I did actually start off with a Sprite Kit game template, but I removed a bunch of stuff from it... I'll try making the SKSpriteNodes I make later into a class to see if that works. I'm not used to classes, but whatever works.


Actually, now I'm having another problem. For some reason everything works now, except I have no clue how to set the background color from a class file. All I'm trying to do is make an SKSpriteNode change the background color when clicked on, but since it's in a class I can't find a way to access the 'view' variable without storing it once it gets given to the class. I've been trying to find an answer to what I'm supposed to do here, but whatever I search, nothing comes up. Even if the SKSpriteNode ISN'T a custom class, I still have the problem.


This is probably a really dumb question, but I seriously cannot figure out what to do

Accepted Answer

>> All I'm trying to do is


I don't really know how to give a useful reply. You're using a toolkit that has lots of available components and behaviors, but you're trying to avoid learning how to use the toolkit, and you seem to be trying to skip over the underlying object-oriented programming concepts that the toolkit is based on. That's surely going to make problem solving difficult.

Wow, I'm stupider than I thought, the solution was right in front of me. Thanks for that reply, I get it now, and I get why you couldn't answer

The bad news: there's no easy (nor single or even right) way to learn Swift, SpriteKit, OOP, UIKit, and POP, ECS and Xcode.


Apple doesn't spend money on documentation, examples, demos or manuals. They release samples once a year that are huge, unweildy and nearly never udpated. If you were to slog your way through their myriad of wordy marketing and promotion diatribes and try to rationally match them with their half baked references they call "documentation", you'd go insane before getting anywhere.


Sprite Kit used with Swift is based on three ideas, somewhat competing paradigms:


1. UIKit's OOP architecture and legacy graphic ideas and positional types.


2. Swift's looming Protocol ideas (somewhat, strong focus on value types, though)


3. GameplayKit's attempts at an Apple version of Entity/Component/System architecture


And there's a GUI to add to your pain...


And Storyboards integration.


And Metal Shaders, just to torment you with promise and potential...


And a texture packer and particle editor that are buggy.


It's a nightmare place to start learning how to do things on iOS.


I suggest King's Defold, instead, as a starting point. Lighter, and it's Lua, and probably better than Corona, where I suspect you've come from.

Having problems with self
 
 
Q