Trying to spawn a sprite on to an skscene from button press {SpriteKit}

I am having an issue where when I press a UIButton from the Controller which is a uiviewcontroller the sprite will not show up on the skscene.

When I call the function "display Pokemon" from the touch began function inside the GameScene class the sprite gets displayed. But when I click the UIButton and call the same function nothing appears..I console logged it and it picks up the Pokemon name and location but it does not add it to the view

Thanks for checking out my post here are the images bellow showing the code

The problem lies in initializing a new GameScene each time you press a button. Furthermore, that initialized scene isn't being associated with the controller whatsoever. It simply deallocates when you return from asdf. Meanwhile, the scene currently associated with your controller (the one I see on the picture I assume) remains unaffected.


Please note that the Swift forums are for topics about the language itself and related tools rather than development with the language.

Thanks so much for the reply.I adjusted my code so I do not create another instance of GameScene...at least I think so..

Do I have no idea what I am doing or something?

I suggest taking the question over to the Sprite Kit section of forums.developer.apple.com, where we can straighten out what's going wrong.

does my code look right at all though? why is this acting so difficult lol

Nope, it's wrong, :slight_smile:

The problem is that there's an existing instance of GameScene which you're not finding. Creating new instances doesn't help, nor does making your GameScene members static.

Most likely what you need is an @IBOutlet from the view controller connected to the scene in your storyboard, but it's hard to be certain without more context.

how do I connect a view controller to an skscene?
I really just want to place the button Action inside of GameScene but I need a ViewController to do that

Create a reference to your GameScene in the controller using an @IBOutlet, and use it to refer to the currently presented scene instead of doing so through static methods. Understand that static methods are not called on instances, but on the type itself. You need to add a pokemon to the scene instance that is currently presented.

So either drag it as an outlet from the interface builder or reach it through (self.view as! SKView).scene where self is the controller that is presenting the scene. To add a node to the scene you call scene.addChild(node). I hope you figure out the rest.

1 Like

I figured it out...it was so obvious..after 12hours nonstop

This is the programmatic approach though, without interface builder. Don't forget to mention next time which approach you want to use, it would've been much faster if we knew you want it programmatically :)

Please ask questions about Apple-specific frameworks on the Apple Developer Forums.

Right, this is off-topic here. Closing this thread.

1 Like