mutliple nodes in GameScene.sks with same name - swift

I'm trying to make a simple 2D Platformer game. Instead of importing the sprites, I decided to use the ones the SpriteKit engine provides: in GameScene.sks I dragged and dropped a sprite (simple, red square) which I called "wall". I copied and pasted it to have 2 walls the player could jump on. In the code part I wrote the following lines:
var wall = SKSpriteNode()
and then, in the "didMoveToView" function:
wall = self.childNodeWithName("wall") as! SKSpriteNode
wall.physicsBody = SKPhysicsBody(rectangleOfSize: wall.size)
wall.physicsBody?.affectedByGravity = false
wall.physicsBody?.dynamic = true
wall.physicsBody?.allowsRotation = false
wall.physicsBody?.pinned = true
wall.physicsBody?.categoryBitMask = physicsCategories.wall
wall.physicsBody?.contactTestBitMask = physicsCategories.player
since I've created a struct outside the class ("physicsCategories") holding the various categories bit masks, when I check for contacts it works just fine:
//CONTACT
func didBeginContact(contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case physicsCategories.player | physicsCategories.wall:
isGrounded = true
default:
print("Some other contact")
}
}
My problem is that this works only with one of the 2 sprites with the name "wall". Is there a way to reference in code ALL the sprites in the scene which have that same name? Like a tag or something like "self.childrenNodesWithName(...)", which I know doesn't exist unluckly.
Or maybe am I doing something wrong?
Thank you in advance.

I've not my mac with me, this is a screenshot from the web:

Related

Swift - How to add scene boundaries for specific nodes?

I'm trying to develop an IOS game using SpriteKit, and I want to add a Physics body to the scene so that the player won't be able to go through the edges of the screen. At the same time, I want some nodes (for example - bombs that fall from the sky) to be able to go through the edges of the screen.
I know that I can use the following line to add a physics body to the scene:
self.physicsBody = SKPhysicsBody (edgeLoopFrom: self.frame)
My question is how can I allow a "bomb" object to go through such body while having a "player" object obligated to those boundaries.
The answer is relative to categoryBitMask and collisionBitMask of the involved physic bodies.
For example, for the scene:
if let scenePB = scene.physicsBody {
scenePB.categoryBitMask = 1
scenePB.collisionBitMask = 2 // collides with player
}
For the player:
if let playerPB = player.physicsBody {
playerPB.categoryBitMask = 2
playerPB.collisionBitMask = 1+4 // collides with scene and bombs
}
For any bomb:
if let bombPB = bomb.physicsBody {
bombPB.categoryBitMask = 4
bombPB.collisionBitMask = 2 // collides with player
}

SKSpriteNode physics body created from texture results in nil value

I'm currently working on a small iOS game. In its current iteration, 20 targets spawn and move across the screen space-invaders style, and you control a little ship to shoot and destroy them. The code for my targets, the player ship's bullets, and a simple collision detection function I've written in the interim are as follows:
class Red_Target: SKSpriteNode{
var game_scene: GameScene!
private var ship_texture: SKTexture!
convenience init(scale: CGFloat, game_world: GameScene){
self.init(texture: SKTexture(imageNamed: "Proto Target"))
self.ship_texture = SKTexture(imageNamed: "Proto Target")
self.setScale(scale)
game_scene = game_world
game_scene.addChild(self)
self.position = CGPoint(x: game_scene.view!.bounds.width/10, y: 9 * game_scene.view!.bounds.height/10)
//self.physicsBody = SKPhysicsBody(texture: ship_texture, size: self.size)
self.physicsBody = SKPhysicsBody(circleOfRadius: 13)
self.physicsBody!.affectedByGravity = false
self.physicsBody!.collisionBitMask = 0x0
self.physicsBody!.categoryBitMask = CollisionType.Enemy.rawValue
self.physicsBody!.contactTestBitMask = CollisionType.Player_Bullet.rawValue
}
func move() {
self.run(space_invaders(scene: game_scene))
}
}
class PC_Bullet: SKSpriteNode{
convenience init(scale: CGFloat){
self.init(imageNamed: "Goodbullet")
self.setScale(scale)
self.physicsBody = SKPhysicsBody(circleOfRadius: 3)
self.physicsBody!.affectedByGravity = false
self.physicsBody!.categoryBitMask = CollisionType.Player_Bullet.rawValue
self.physicsBody!.collisionBitMask = 0x0
self.physicsBody!.contactTestBitMask = CollisionType.Enemy.rawValue
}
}
func didBegin(_ contact: SKPhysicsContact) {
contact.bodyA.node!.removeFromParent()
contact.bodyB.node!.removeFromParent()
}
}
This code, in its current iteration, works just fine. However, if the line defining the target's physicsbody as its texture is uncommented and the line defining physicsbody as circleOfRadius is removed, the game will consistently crash after the 5th target is destroyed, claiming that didBegin unwraps a nil value. Why does this only happen from physics bodies with textures? Is there any way I could change the code for this to work? I would love to be able to use the physics body from texture function later on, when working with more irregular shapes.
You are pulling a classic nooby mistake. You are removing your bodies too early. If you browse Stackoverflow you will find a plethera of ways to solve it.
The basic idea is do not remove your sprites until the end of the physics phase because your 1 sprite could have multiple contact points to handle. So come up with a way to flag sprites that need to be deleted, and remove them during the didSimulatePhysics function.

Swift Spritekit physicsBodies do collision bit masks in the SKScene editor

I have two balls. I don't want them to collide with each other. I want them to collide with everything else. I made the two balls have different category bit masks of 2 and 3. All the other objects have the same category bit mask of 1. I have tried setting the collision bit masks of both balls to 1. I also read something that said that an operation is done to the collision bit mask and the category bit mask and if it is nonzero then there is a collision, so I made the balls have the collision bit mask as the category bit mask of the other. So they would be 0 and everything else would be nonzero and collide. None of this works, so how do I actually make them so it works the way I want them to.
These are some of the masks I tried
Do it in code, it's so much easier to read.
class GameScene: SKScene, SKPhysicsContactDelegate {
weak var ball1: SKSpriteNode?
weak var ball2: SKSpriteNode?
let noCategory:UInt32 = 0
let ball1Category:UInt32 = 0b1
let ball2Category:UInt32 = 0b1 << 1
let wall1Category:UInt32 = 0b1 << 2
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
ball1 = self.childNode(withName: "ball1") as? SKSpriteNode
ball2 = self.childNode(withName: "ball2") as? SKSpriteNode
wall1 = self.childNode(withName: "wall1") as? SKSpriteNode
ball1?.physicsBody?.categoryBitMask = ball1Category
ball1?.physicsBody?.collisionBitMask = noCategory
ball2?.physicsBody?.categoryBitMask = ball2Category
ball2?.physicsBody?.collisionBitMask = noCategory
wall1?.physicsBody?.categoryBitMask = wall1Category
wall1?.physicsBody?.collisionBitMask = ball1Category | ball2Category
}
}
Now you can easily see that wall1 collides with the ball1 and ball2 but the balls themselves don't collide with anything. With collisions only one of the physicsbodies needs to have the collisionbitmask set. Therefore you could add the ball1 and ball2Category to all physicsbodies collisionbitmasks except the opposite ball and the balls with collide with those physicsbodies but not each other.

SKSpriteNode uses up memory when physicsBody is created

I have a SKSpriteNode that uses no memory when sitting there. Once I specify the physicsBody, the memory goes up at about 0.5MB per second. I have other physics bodies but when they are added, they work normally. There is a sprite called "player" which i use to display the player. I made this function to make sure the problem was isolated. (IOS 10.3.3)
func createPlayerCollisionBox() {
let playerDummy = SKSpriteNode()
playerDummy.position = player.position
playerDummy.name = "dummy"
playerDummy.physicsBody = SKPhysicsBody.init(circleOfRadius: 40)
playerDummy.physicsBody?.isDynamic = false
playerDummy.physicsBody?.usesPreciseCollisionDetection = false
playerDummy.physicsBody?.categoryBitMask = playerCategory
playerDummy.physicsBody?.collisionBitMask = UInt32(8)
playerDummy.physicsBody?.contactTestBitMask = playerCategory | enemyCategory
self.addChild(playerDummy)
}
Note: The memory does not go back down when you leave the scene and come back.
The memory:

Unexpected repeated collisions in SpriteKit

I'm making my first game with Swift and SpriteKit and while I've had collisions working properly for some time now, I recently noticed a huge bug. My collisions can either be the user with and enemy (spaceship against alien) user shooting an enemy (laser against alien). The latter works fine, but recently the collision detection when the ship touches an alien hasn't been working-- normally the alien should be removed from the scene when it touches the so that only a single life (1 of 3 total) is removed. However, the ship looses 3-6 lives upon touch now. Here's the code and where I'd assume the problem is:
This is one of a few contact functions that is then called in the general contact method.
func alien_ship_contact(contact:SKPhysicsContact){
var alien:SKNode? = nil
if contact.bodyA.categoryBitMask == PhysicsCategory.Alien && contact.bodyB.categoryBitMask == PhysicsCategory.Ship{
alien = contact.bodyA.node
}
else if contact.bodyB.categoryBitMask == PhysicsCategory.Alien && contact.bodyA.categoryBitMask == PhysicsCategory.Ship{
alien = contact.bodyB.node
}
else{
return
}
killOffAlien((alien)!)
aliensKilled = aliensKilled + 1
shipLives = shipLives-1
aShip.lives = aShip.lives - 1
print("ship/alien contact")
}
Here is the killOffAlien function:
func killOffAlien(alien:SKNode){
print("Kill off")
//alien.removeFromParent()
func stopMotion(){
alien.physicsBody?.categoryBitMask = 0
alien.physicsBody?.collisionBitMask = 0
alien.physicsBody?.contactTestBitMask = 0
alien.physicsBody?.dynamic = false
alien.physicsBody?.velocity = CGVector(dx:0, dy:0)
alien.removeActionForKey("facialMotion")
}
func removeAlien(){
alien.removeFromParent()
}
let stopMoving = SKAction.runBlock(stopMotion)
let fadeOut = SKAction.fadeOutWithDuration(1)
let removeFromParent = SKAction.runBlock(removeAlien)
let die = SKAction.sequence([stopMoving, fadeOut, removeFromParent])
alien.runAction(die)
}
And here is the general contact method:
func didBeginContact(contact:SKPhysicsContact){
alien_laser_contact(contact)
alien_ship_contact(contact)
....
Any help would be awesome, I would think that upon initial contact the alien has it's bitmask set off from Alien so that in itself would prevent future collisions as every alien should only be able to remove at max one life from the ship.