Oren Edrich
Oren Edrich

Reputation: 674

How to call an action from the game scene

In my Sprite-kit game scene I try to add a pinking/pulsing action. I don't know how to call the game view controller. In a simple UIViewController I can simply launch:

viewController.addPulse    

however in a Sprite-kit game scene I'm not sure how to call it. Here Below there is the code that I tried and it gives me an error on the line of:

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(GameViewController.addPulse))    



 if defaults1.integer(forKey: "Sphere") == 13 {
        Ghost = SKSpriteNode(imageNamed: "moon")
        Ghost.size = CGSize(width: 50, height: 50)
        Ghost.position = CGPoint(x: self.frame.width / 2 - Ghost.frame.width, y: self.frame.height / 2)
        Ghost.physicsBody = SKPhysicsBody(circleOfRadius: Ghost.frame.height / 1.4)
        Ghost.physicsBody?.categoryBitMask = PhysicsCatagory.Ghost
        Ghost.physicsBody?.collisionBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall
        Ghost.physicsBody?.contactTestBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall | PhysicsCatagory.Score
        Ghost.physicsBody?.affectedByGravity = false
        Ghost.physicsBody?.isDynamic = true
        Ghost.zPosition = 2
        self.addChild(Ghost)
        Ghost.isUserInteractionEnabled = true
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(GameViewController.addPulse))
        tapGestureRecognizer.numberOfTapsRequired = 1
        Ghost.addGestureRecognizer(tapGestureRecognizer)

Upvotes: 1

Views: 82

Answers (1)

Alessandro Ornano
Alessandro Ornano

Reputation: 35392

I'm afraid you mix UIKit elements with Sprite-kit elements, you confusing the container who hosts your scene with the scene itself.

The SKView is a UIView subclass. It wraps up Sprite-kit content in a view that can be used like any other UIView, in fact it usually has an associated view controller.

The scene is a SKScene class. It provides callbacks (physics, SKNode subclassed elements, actions, update..) needed to implement a game.

Usually the view stay behind as is while you present scenes to swap out game content.

So I have serious doubts about what do you want to achieve with your code, if you want to make an action that show a pulse, you should build and call this method in your scene, not in the UIViewController that hosts your game..

Upvotes: 4

Related Questions