Still working on my space Invaders clone and I want to add the destructible bases at the bottom of the screen :
I've worked out how I can modify the appearance of the shield by having the bombs and the shield contact each other and then in didBegincontact, create an SKCropNode from a mask for the bomb's explosion and the current texture of the shield and then take the rendered texture with view's texture(from: node)
method and updating the shield's texture & physicsBody from the new texture.
However - when there is a hole blown through the center of the shield, such that the shield's texture is no longer contiguous (e.g. the left-most shield in the picture), the generated physicsBody only covers the left-hand portion of the texture. Thus any bombs hitting the right-hand part of the shield won't be detected.
How can I have my shield break into multiple pieces and keep contact detection working?
Possible solutions:
- Somehow scan the new texture and create multiple physicsBoies which
are either assigned to a single SKSpriteNode using
SKPhysicsbody(bodies: [SKPhysicsBody])
. If, as it appears, the SKPhysicsBody is generated from the top-left of the texture, then maybe rotate the texture through 90 degrees 4 times to generate 4 physicsBodies? Use some sort of alpha-masking (so that the destroyed parts of the
shiled are clear) and keep the physicsBody of the shield in the
original shape and when dBC is called, check the colour of the pixels at the contact point/under the bomb - if they are 'clear', then take no action and allow the bomb to fall through unimpeded until
didEndContact is called indicating that the bomb is clear of the
shield.Use a simple
intersectsRect
between the bomb and the shield and then check the pixel colour under the bomb - if clear, don't do anything.
===================================================
Update : See below for sprite texture damage working when not split. The brown base is the original one showing the first damage (physicsBody shows the original base, the texture shows the dame). The green base has had it's physics body recalculated:
Update: See below for the physics body when the texture is split. The brown is the original shield, the small indent is the 'new' damage. The physicsBody only maps to the left-hand portion of the image.