# How do I create a 4 frame destructible wall sequence?

**URL:** https://discourse.processing.org/t/how-do-i-create-a-4-frame-destructible-wall-sequence/39284
**Category:** Coding Questions
**Created:** [October 15, 2022, 1:13pm UTC](https://discourse.processing.org/t/how-do-i-create-a-4-frame-destructible-wall-sequence/39284 "2022-10-15T13:13:43Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Marfmamow](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@Marfmamow](https://discourse.processing.org/u/Marfmamow)
#### Post date: [October 15, 2022, 1:13pm UTC](https://discourse.processing.org/t/how-do-i-create-a-4-frame-destructible-wall-sequence/39284/1 "2022-10-15T13:13:43Z")

</div>

I’m making a 2d game, and some of the walls are destructible.

There are 5 sprites for that type of wall.

1. Normal wall sprite
2. Destroyed frame 1
3. Destroyed frame 2
4. Destroyed frame 3
5. Destroyed frame 4

All 5 of these sprites are loaded into a PImage array in setup.

The draw method in my wall class is like this: `image(sprites[currentFrame], x, y)`

I have a collided flag that is set to true once the player projectile hits it.

What I want it to do is once hit, trigger a 4 frame destruction sequence, where each of the destruction sprites are shown for only one frame

---

<div class="post-metadata">

### Author: ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)
#### Post date: [October 15, 2022, 1:41pm UTC](https://discourse.processing.org/t/how-do-i-create-a-4-frame-destructible-wall-sequence/39284/2 "2022-10-15T13:41:35Z")

</div>

I can give you a system I usually implement when working with sprites:

MainSketch.pde

```auto
SpriteHandler standardHandler=new SpriteHandler();
void setup(){}
void draw(){
   standardHandler.run();
}

```

SpriteHandler

```auto
class SpriteHandler {
  ArrayList<Sprite> sprites=new ArrayList<Sprite>();
  SpriteHandler(){
  }
  void register(Sprite s){
    sprites.add(s);
  }
  void run(){
    for(Sprite s:sprites) s.run(s.currframe++);
    for(int i=sprites.size()-1;i>=0;i--) if(sprites.get(i).done()) sprites.remove(i);
  }
}

```

Sprite.pde

```auto
abstract class Sprite{
  int duration=0;
  int currframe=0;
  boolean forceQuit=false;
  boolean done(){
    return duration<=(currframe&&duration>0)||forceQuit;
  }
  void register(){
     standardHandler.register(this);
  }
  abstract void run(int frame);
}

```

You can make your class extend the Sprite class and implement the run method to draw your sprite.  
You just have to set super.duration=4 to signal that your sprite has 4 frames.  
Now once you have a collision trigger you can create a new instance of your Sprite and  
call the register method on it.  
If you want I could send some shorter code that is more inefficent though.

---

<div class="post-metadata">

### Author: ![Marfmamow](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@Marfmamow](https://discourse.processing.org/u/Marfmamow)
#### Post date: [October 16, 2022, 2:35am UTC](https://discourse.processing.org/t/how-do-i-create-a-4-frame-destructible-wall-sequence/39284/3 "2022-10-16T02:35:50Z")

</div>

Could you please send me some shorter code? I just wanna get it working before implementing the longer method you provided

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [October 16, 2022, 3:34am UTC](https://discourse.processing.org/t/how-do-i-create-a-4-frame-destructible-wall-sequence/39284/4 "2022-10-16T03:34:03Z")

</div>

> [@Marfmamow](#):
>
> I have a collided flag that is set to true once the player projectile hits it.

Good starting point!

```auto
if(collided){
   image(sprites[currentFrame], x, y);
   currentFrame++;
   if(currentFrame>4) currentFrame=4;
}else{
   image(wall,x,y);
}

```

---

<div class="post-metadata">

### Author: ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)
#### Post date: [October 16, 2022, 5:36am UTC](https://discourse.processing.org/t/how-do-i-create-a-4-frame-destructible-wall-sequence/39284/5 "2022-10-16T05:36:51Z")

</div>

> [@Marfmamow](#):
>
> Could you please send me some shorter code? I just wanna get it working before implementing the longer method you provided

This would be a much shorter class that plays the sprite once the Constructor is called

```auto
final PApplet enclosing_instance=this;

public class ImageSprite {
  int x, y;
  ImageSprite(int x, int y) {
    this.x=x;
    this.y=y;
    register();
  }

  int currentFrame=0;
  int duration=4;
  void register() {
    enclosing_instance.registerMethod("draw", this);
  }
  public void draw() {
    if (currentFrame<duration) run(currentFrame);
    else enclosing_instance.unregisterMethod("draw", this);
    currentFrame++;
  }
  void run(int frame) {
    //paint your image here
    rect(x, y, 10, 10);
  }
}

```
