My implementation

I’ve made my own sprites for this course just because I wanted the finished thing to feel like it’s my own and I’m not just filling in the numbers so-to-speak. For the blocks I’ve made a spritesheet named “block.png” and using the Unity’s Multiple sprite mode
image
and sprite editor


I’ve made three sprites and called them block_0, block_1, block_2
Then I stored all the sprites in the Assets/Resources/Sprites directory so that I may fetch them in the script, which I do in the Block script in Awake method

    private void Awake()
    {
        sprites = Resources.LoadAll<Sprite>("Sprites/block");
    }

So now my block has an array of all the sprites from the block spritesheet.
Then in the method where I process hits I do this:

    private void ProcessHit()
    {
        int hitsRemaining = maxHits - hitCount;
        renderer.sprite = findBlockSpriteById(sprites.Length - hitsRemaining);        
    }

What this does is it calculates how many hits are remaining and then finds the appropriate sprite going from the back so that before the last hit, no matter how many hits the block allows, the block will always look the most damaged.

This should allow me to add more sprites to the spritesheet down the line, without having to change anything in the code

What do you think of my solution?

1 Like

It’s a really cool solution but I also think you over-engineered this.

A serialized array could do the same trick and it would allow you to add different sprite sheets for multiple types of blocks if needed, it also prevents the use of strings, which is always a good thing: The main issue with this particular string is that if you for some reason change the name or even just the place the block sprites in another folder you’ll have to go back to your code.

Overall you did a great job, the code could be more flexible and less prone to errors, but it’s great that you came with your own solution.

1 Like

Privacy & Terms