All of the assets required can be downloaded here.
The sprites for the boxes are in the Traps --> Trampoline
Create any level you like.
You may copy my level layout.
Note that I have hidden (visible = false)
A secret Strawberry behind the box
Create a new scene for the Box
Use an StaticBody2D as the root node
Add a CollisionShape2D as a child
Add an Area2D as a child of Box
Add an AnimatedSprite2D as a child of Area2D
Add a CollisionShape2D as a child of Area2D
Select the AnimatedSprite2D node
In the Sprite Frames property
Rename the default animation to idle
Use the idle frame of the Box
Add a new animation name it hit
To add frames from a sprite sheet
Set Horizontal frames to 3
Select the CollisionShape2D node which is a child of Box
Add a new RectangleShape2D
This should fit the Box exactly
Select the CollisionShape2D node which is a child of Area2D
Add a new RectangleShape2D
This should extend out beyond the image of the box
Create a new scene with RigidBody2D as the root
Add an AnimatedSprite2D as a child
Add a CollisionShape2D as a child
Add an AnimationPlayer as a child
Select the AnimatedSprite2D node
In the Sprite Frames property
Rename the default animation to break
To add frames from a sprite sheet
Select the Break.png image
Set Horizontal frames to 4
Select the CollisionShape2D node
In the Shape property choose a new RectangleShape2D
Select the AnimationPlayer node
Click the Animation Button
Name the new animation flash
In the Visibility section
Click the key next to Visible
Click Create to create the Animation track for visible
Ensure Create RESET Track(s) is enabled
You now have one keyframe on the flash track
Move the timeline, disable the visible property and click the key
Then repeat this process, enabling and disabling the property
I made the flashing effect speed up towards the end
Disable Autoplay and set duration to 0.5
Attach a script to the BoxPiece node
add ready variables to access:
the AnimatedSprite2D node and the AnimationPlayer node
Each time a BoxPiece is instantiated in a scene:
We will choose a random frame from the AnimatedSprite2D
This will give us some variety in the shape of the pieces
It could be one of four different types
Connect the timeout signal
Now code it to play the flash animation
Select the AnimationPlayer node
Connect the animation finished signal
Now code it to delete the BoxPiece
when it has stopped flashing
Attach a script to the Box
Add ready variables to access:
Add a variable for box durability
This is how many hits before the
Connect the body entered signal
Code the body entered function
if the body is in the player group
Select the AnimatedSprite2D node
Connect the animation finished signal
Code the animation finished signal
if the hit animation is finished
reset to the idle animation
Create a Strawberry variable
Set it to appear when the box breaks
Repeat the next code 4 times:
Offset its position so they don't
spawn it where the box is
Add the broken piece to the scene
Update the _physics_process function
Check if durability is 0 or below
remove the box from the scene