0

is it somehow possible to destroy an object on contact? Like not just delete it from the screen with body.removeFromParent(), I would like to have an animation.

I have a player and walls, and when the player has a special powerup, I want it to be able to destroy the walls on contact. I could imagine that I have like the wall split up as many little physics bodies and they hold together on like an anchor point and when my player hits it, they get an impulse from the player (just set isDynamic to true I guess) and losen the anchor point so all the sprite Nodes will fly their way and so the wall will be destroyed.

Can you give me some help / advise of a good way of doing that?

Beginner
  • 67
  • 8

1 Answers1

1

You don't need to have the nodes making up the wall held together in any way - just place them on the screen. If the player doesn't have the power-up, turn off the bit for the player in the wall nodes' physicsBodies collisionBitMask so that the wall nodes do not collide with the player. Then when the player hits the wall, the player will be affected by the collision (and bounce off) but the wall nodes will be unaffected.

When the player has the power-up, make the wall nodes affected by the collision and also turn on contacts between the player and the wall (it's enough just to turn on the bit for the wall category in the player's contactTestBitMask). Then the wall nodes will be affected by the collision (and move or spin away) and your didBegin() will be called and you can run an action on each wall node comprising of the animation you want and ending with removeFromParent().

A guide to collision and contactTest bit masks: https://stackoverflow.com/a/40596890/1430420

Manipulating bit masks to turn collision & contacts off and on. https://stackoverflow.com/a/46495864/1430420

Edit: SK demo showing an object hitting a wall made up of blocks:

Create a new SK project and use this as the GameScene,swift:

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    override func didMove(to view: SKView) {

        physicsWorld.gravity = CGVector(dx:0, dy:0)

        let ball = SKSpriteNode.init(color: .red, size: CGSize(width: 50, height: 50))
        ball.physicsBody = SKPhysicsBody.init(circleOfRadius: ball.size.width/2)
        ball.position = CGPoint(x: 0, y: 0)

        buildWall()

        addChild(ball)

        ball.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 50))
    }

    func buildWall() {
        let xStart : CGFloat = ((scene?.size.width)!/2) * -0.9
        var brickPosition = CGPoint(x: xStart, y: 500)
        let brickSize = CGSize(width: 20, height:20)

        for wallRow in 1...10 {
            for wallColumn in 1...30 {
                let brick = SKSpriteNode(color: .yellow, size: brickSize)
                brick.physicsBody = SKPhysicsBody.init(rectangleOf: brick.size)

                brick.position = brickPosition

                addChild(brick)
                brickPosition.x += brickSize.width + 1
            }
            brickPosition.x = xStart
            brickPosition.y -= 11
        }
    }
}
Steve Ives
  • 7,894
  • 3
  • 24
  • 55
  • first, thanks for your reply, I am aware of the category / contact / collssion bitmasks. I am searching for the best way to destroy the walls into little pieces, my idea was making a lot of sprites and join them together and then collide with the wall, pushing it backwards and undo the join so the wall falls apart, but I dont know how to join sprites together – Beginner Dec 10 '18 at 19:22
  • like player hits wall -> wall gets pushed away (set bit at contactTestBitmask) -> wall falls apart into pieces (nodes disconnect from its anchor point and wall falls apart) – Beginner Dec 10 '18 at 19:25
  • 1
    I think you’re making it too complicated - the nodes making up the wall don’t need to be joined together because what’s going to make them move unless the player collides with them when powered-up? So the player hits the wall when collisions are enabled, and the wall nodes he hits get pushed away. These the hit other nodes sending them tumbling and spinning. – Steve Ives Dec 10 '18 at 19:32
  • 1
    I’m going to see if I can write a little demo - just to see if what I’m thinking, works. – Steve Ives Dec 10 '18 at 19:34
  • that would be great! cause if I have a wall and it collides with the player, I agree that it is moving then or being pushed away when i set the right bit, but it is not gonna like fall apart as it would be destroyed or something – Beginner Dec 10 '18 at 20:56
  • I have tried it now by just simply setting the bits, the walls on hit are tumbling and spinning away on collision, but they dont explode or fall apart as I would like them to – Beginner Dec 13 '18 at 13:02
  • like my player is super strong, he destroys the walls not kick em away – Beginner Dec 13 '18 at 13:02
  • 1
    To have the wall 'explode', you are going to have to give it an SKAction which will probably be a animation followed by a `removeFromParent` The SK engine will never destroy a node, as all node are effectively indestructible/ I;m still hoping to write that demo :-) – Steve Ives Dec 13 '18 at 13:05
  • okay thanks i hope you get to write that demo :) i tried rescaling / sizing with the actions but it looks kinda weird :D – Beginner Dec 13 '18 at 19:14
  • 1
    @Beginner - sorry for taking so long. I've added a code example showing a block citing a wall and breaking it apart. I s that anything like what you had in mind? (note that the 'ball' is a square, but it's physicsBody is a circle) – Steve Ives Dec 26 '18 at 11:41
  • I was pretty busy myself, thank you anyway for responding! I will have a look at it this week – Beginner Jan 07 '19 at 16:41