How to call method from ViewController in GameScene
the reason this doesnt work is that you are creating a NEW instance of GameViewController
and then you're calling gameOver
on that. What you really want to do is reference your existing GameViewController
theres a few ways to do this, I'll give you one example.
add a viewController property to your GameScene class
class GameScene {
// we need to make sure to set this when we create our GameScene
var viewController: GameViewController!
in your GameViewController file
// after GameScene is instantiated
gameScene.viewController = self
now we have a reference to viewController, lets use it in our GameScene class
// somewhere in GameScene
self.viewController.gameOver()
How to call a function in viewController from GameScene
First of all, you don't need the question mark after scene.viewController
.
Second, scene.viewController = self
should come before skView.presentScene(scene)
. This will probably fix your problem.
Lastly, it's considered bad design (or at least sloppy) to make an SKScene have a property that is a UIViewController. The scene class is now tied to using a UIViewController, and if you want to extend your code to something that doesn't use UIViewController to control views (e.g. you want to make a Mac version of your game), it won't work right away because it's hard-coded to work with UIViewController.
The "pure" way to do this would be a technique called "delegation" by iOS programmers. You create a protocol that will be your delegate, and make your view controller implement that protocol. Then the SKScene uses the protocol, not UIViewController.
All that said, you might want to leave out this complexity.
How to call methods from viewcontroller onto skscene
It's a question of referencing your viewController in your GameScene.
There are many approaches, one is to use this in the GameScene:
let controller = self.view?.window?.rootViewController as GameViewController
Now this will return the viewController that is showing the scene at that moment. So if you have more then one controller, you should take care with this.
Call a function of GameScene in GameViewController
GameScene().revivePlayer()
is calling the revivePlayer
function on a new instance of GameScene
, not the game scene that is presented in your view controller.
To resolve this issue, keep a reference to the game scene that is presented and call revivePlayer
on that instance.
Something like this should work, although it depends on exactly how you set up your scene.
class GameViewController: UIViewController {
@IBOutlet weak var skView: SKView!
var gameScene = GameScene()
override func viewDidLoad() {
super.viewDidLoad()
skView.presentScene(gameScene)
}
func myFunction() {
gameScene.revivePlayer()
}
}
Edit #1
One solution to the comment below is to use SKView
's scene
property to get a reference to the currently presented scene.
if let gameScene = skView.scene as? GameScene {
gameScene.revivePlayer()
}
Edit #2
You'll also need to keep a reference to your SKView
.
class GameViewController: UIViewController, ... {
var skView: SKView! // declare a property to keep a reference to the SKView
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
skView = view // assign the SKView to our new property
...
}
}
...
func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
videoAd = createVideoAd()
if let gameScene = skView.scene as? GameScene { // check to see if the current scene is the game scene
gameScene.revivePlayer() // revive the player
}
}
}
How do I call a function from GameScene in GameViewController using SpriteKit in Swift?
If I'm not mistaken main role of GameViewController
is just to present SKScene
, while the game logic should be in SKScene
. There can be of course exceptions, but I don't think resetBalloon() is one of them.
So your GameViewController
could be just this:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = GameScene(size: view.bounds.size)
let skView = view as! SKView
...
skView.presentScene(scene)
}
}
And everything else(even switching to another scene) should be inside GameScene
.
How can I call a method from ViewController?
try this.
in ViewController class
#import "Gamescene.h"
Gamescene *obj = [[Gamescene alloc] init];
[Gamescene methodName];
and don't forgot to add method name in Gamescene.h file..
in Swift
class SomeClass {
class func someTypeMethod() {
// type method implementation goes here
}
}
SomeClass.someTypeMethod()
you can learn here apple Documentation.
swift: How to call an ad made in ViewController in game scene
Use notifictionCenter to call the function
Add the first function in the viewDidLoad() and the second in the adButton()
In Swift 3
//In the gameviewcontroller
NotificationCenter.default.addObserver(self, selector: #selector(YourFunctionNameHere), name: "NSNotificationCreate" as NSNotification.Name, object: nil)
//In the gameScene use this to call the ad
NotificationCenter.default.post(name: "NSNotificationCreate" as NSNotification.Name, object: nil)
In Swift 2
//In the gameviewcontroller
NSNotificationCenter.defaultCenter().addObserver(self, selector: "YourFunctionNameHere", name: "NSNotificationCreate", object: nil)
//In the gameScene use this to call the ad
NSNotificationCenter.defaultCenter().postNotificationName("NSNotificationCreate", object: nil)
Communication from SKScene to UIViewController
You can check my answer which describes how to communicate SKScene with UIViewController using delegate pattern.
Call GameScene method from AppDelegate (Swift 3, SpriteKit, Xcode 8)
You are creating a new instance of your GameScene.
To pause the existing instance you need to add a reference to it in the AppDelegate.
The better solution it to register the GameScene class to receive notifications when the app goes into the background. This is a good alternative to coupling these classes with the AppDelegate.
In your GameScene class add this in the viewDidLoad() function:
let app = UIApplication.shared
//Register for the applicationWillResignActive anywhere in your app.
NotificationCenter.default.addObserver(self, selector: #selector(GameScene.applicationWillResignActive(notification:)), name: NSNotification.Name.UIApplicationWillResignActive, object: app)
Add this function to the GameScene class to react to the received notification:
func applicationWillResignActive(notification: NSNotification) {
pauseGame()
}
Related Topics
Swiftui - Animations Triggered Inside a View That's in a List Doesn't Animate the List as Well
Uitextfield in Uialertcontroller (Border, Backgroundcolor)
Xcode 6 Gm - Cllocationmanager
Tableview Cell How Do We Resize Cell in Swift Along with Image and Label
iOS 7 Uisearchdisplaycontroller Search Bar Overlaps Status Bar While Searching
iOS Horizontal Slideview with Vertical Menu
Alamofire Download in Background Session
Layout Attributes Relative to the Layout Margin on iOS Versions Prior to 8.0
iOS 11 Large-Title Navigation Bar Not Collapsing
Swift - Compressing Video Files
Google Places Autocomplete on iOS - Can't Load Search Results - Try Again
How to Save List of Object in User Default
Access Files in /Var/Mobile/Containers/Data/Application Without Jailbreaking Iphone
Front Facing Camera in Uiimagepickercontroller