0
votes

I have a sprite that the user is able to move side to side by pressing on the left or right side of the screen. If you hold down on either side, the player sprite will leave the screen. I want to stop that from happening using physic bodies, but I can't seem to make it work.

To start, here are my categories.

//Categories for physics bodies
let sceneCategory:UInt32   = 0x1 << 0 // Equal to 1
let playerCategory:UInt32 = 0x1 << 1 // Equal to 2

Here is where I set the physics body of the scene itself.

self.physicsWorld.contactDelegate = self
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
self.physicsBody?.categoryBitMask = sceneCategory
self.physicsBody?.contactTestBitMask = playerCategory
self.physicsBody?.collisionBitMask = 0
self.physicsBody?.isDynamic = false

This is how I have the physics set for the player itself. I'm trying to set the physics body around the car itself. Is the way I have it setup the same as setting an alpha mask physics body?

let texture = SKTexture(imageNamed: "PorscheBlue")
player.physicsBody = SKPhysicsBody(texture: texture, size: player.size)
player.physicsBody?.isDynamic = false
player.physicsBody?.categoryBitMask = playerCategory
player.physicsBody?.contactTestBitMask = sceneCategory
player.physicsBody?.collisionBitMask = 0

Then, in the didBegin method, I just wanted it to print showing that the method was called. This isn't happening though for some reason.

func didBegin(_ contact: SKPhysicsContact)
{
    print("called")
}

Why isn't the didBegin method being called? Do I have the physics set up properly? How can I make it so the player isn't allowed to leave the screen when moving?

Thank you

EDIT: So when having the physics boundaries visible, it looks like the boundaries for the scene are only being drawn on the top and bottom. I can't see any lines being drawn on the sides. That may be the issue, but I can't get it to show on the sides.

1
So you move the sprite using touchmoved?willy wijaya
how do you create your scene? is it created in the scene editor? can you show the code where you transition to the scene (probably from GameViewController)Ron Myschuk
@RonMyschuk Yes the scene is created, and transitioned in the GameViewController. The code I posted is in GameScene.swift.Joseph Kessler

1 Answers

0
votes

I resolved my issue by changing the dynamic value. I had both set to false, which causes the didBegin function to not be called. At least one of the nodes needs to be dynamic.