1

I have a simple app where I'm creating a shape dynamically. This shape has physics, but starts out with it's dynamics set to false (as intended).

var dot = SKSpriteNode(imageNamed: "ShapeDot.png");

override func sceneDidLoad() {
    dot.name = "MyShapeDot";
    dot.size = CGSize(width: 10,height: 10);
    dot.position = CGPoint(x: 0, y: 0);
    dot.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(dot.size.width/2))
    dot.physicsBody?.isDynamic = false;
    dot.physicsBody?.allowsRotation = false;
    dot.physicsBody?.pinned = false;
    dot.physicsBody?.affectedByGravity = true;
    //add to spritekit scene     
    self.addChild(dot)
}

The shape is successfully added to the .sks and the controller (I see it on the screen). Then on a tap gesture I'm calling a function to turn on dynamics for the physics sprite node.

func MyTapGesture(){
   dot.physicsBody?.isDynamic = true;
}

The MyTapGesture is being called (I debugged that it triggers), but the shape doesn't become dynamic and start using gravity... Does anyone know what I'm missing???

I'm calling the MyTapGesture from my interfaceController... It's wired up as so

let gameScene = GameScene();

@IBOutlet weak var spriteTapGestures: WKTapGestureRecognizer!

@IBAction func onSpriteTap(_ sender: Any) {
    NSLog("tap")    
    gameScene. MyTapGesture()
}

Within the MyTapGesture I've also tried print(dot) and it outputs the following:

name:'MyShapeDot' texture:[<SKTexture> 'ShapeDot.png' (128 x 128)] position:{0, 0} scale:{1.00, 1.00} size:{10, 10} anchor:{0.5, 0.5} rotation:0.00

This leads me to believe it should work and I'm calling the right reference of the class that's attached to the object. But it doesn't work. If I call MyTapGesture() within the update func of the SpriteKit class where my dot was created

override func update(_ currentTime: TimeInterval) {
   MyTapGesture()
}

It works and the dynamics update! ...so for some reason my tap gesture must be calling a wrong reference or something??? So confused since the debug shows the correct data printed for the shape that I created...

Robbie
  • 563
  • 7
  • 19
  • after reading this stackoverflow: https://stackoverflow.com/questions/24333142/access-variable-in-different-class-swift – Robbie Jan 18 '20 at 16:12
  • I think doing let 'gameScene = GameScene()' is creating a new instance... rather than referencing the old one... but still not sure how to make the class itself a global var (the example only shows how to create a global string) – Robbie Jan 18 '20 at 16:13
  • It's hard to determine without more code on how you present/manage your gamescene in the viewcontroller class. Perhaps add a print in your sceneDidLoad method of gamescene class to ensure you're only creating 1 instance of GameScene? It could be the GameScene instance your calling MyTapGesture() on is not the instance that is presented in your SKView. – KissTheCoder Jan 23 '20 at 01:43

1 Answers1

0

To solve this - I realized that my gameScene var in my interface controller didn't have the correct reference. So I instantiated it as nil:

var gameScene : GameScene?;

And then assigned the variable in the interface controllers awake func

if let scene = GameScene(fileNamed: "GameScene") {
    gameScene = scene
}
Robbie
  • 563
  • 7
  • 19