Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to manage several Explosions Drawing/Happening ?  (Read 554 times)
0 Members and 1 Guest are viewing this topic.
Offline Andre Lopes
« Posted 2013-05-18 22:42:42 »

Heya.
I kept thinking and thinking and thinking but i still dont know the better way to create a class/method/functions to manage several explosions happening.
I mean the drawing and some kind of ArrayList to control them, also, idk how to create a flag using this class here, to know when the drawing finished....

soo, if anyone could at least tell me what i need to research , i dont want to abuse anyone to give me the answer, but if you can atleast point me to the right direction, i would appreciate a looooot.

Heres the class that im using, i got here from a friend, i think from 65k user?

Anyway, here it is :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
package Atlas;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import java.util.ArrayList;

public class Explosions {

    private static final int FRAME_COLS = 8;
    private static final int FRAME_ROWS = 6;
    Animation walkAnimation;
    Texture walkSheet;
    TextureRegion[] walkFrames;
    SpriteBatch spriteBatch;
    TextureRegion currentFrame;
    float stateTime;

    private ArrayList<Explosions> explosionsHappening;
    private int posx,posy;
    private int TEXTURE_WIDTH,TEXTURE_HEIGHT;
   
    public Explosions() {
        explosionsHappening = new ArrayList<>();
        walkSheet = new Texture(Gdx.files.internal("Explosion2spritesheet.png"));

        TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight() / FRAME_ROWS);

        walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < FRAME_COLS; j++) {
                walkFrames[index++] = tmp[i][j];
            }
        }

        walkAnimation = new Animation(0.025f, walkFrames);
        spriteBatch = new SpriteBatch();
        stateTime = 0f;
       
       
        //Width ¨¨ Height
       TEXTURE_WIDTH = (walkSheet.getWidth() / FRAME_COLS);  
        TEXTURE_HEIGHT = (walkSheet.getHeight() / FRAME_ROWS);
       
    }
   
   
    public TextureRegion getCurrentFrame()
    {
        stateTime += Gdx.graphics.getDeltaTime();                  
        currentFrame = walkAnimation.getKeyFrame(stateTime,false);  
       
        return currentFrame;
    }

    public ArrayList<Explosions> getExplosionsHappening() {
        return explosionsHappening;
    }

    public void addExplosionsHappening(Explosions exp,int posX,int posY) {
        exp.posx = posX;
        exp.posy = posY;
        explosionsHappening.add(exp);
       
        System.out.println("Explosion [x] :" + exp.posx);
        System.out.println("Explosion [y] :" + exp.posy);
    }

    public int getPosx() {
        return posx;
    }

    public int getPosy() {
        return posy;
    }

    public int getTEXTURE_WIDTH()
    {
     return TEXTURE_WIDTH;
    }

    public int getTEXTURE_HEIGHT()
    {
     return TEXTURE_HEIGHT;
    }
   
   
   
   
   
}
Offline Andre Lopes
« Reply #1 - Posted 2013-05-18 23:42:55 »

I cleaned the class a little, to go back to the original.

Heres the result :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
package Atlas;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Explosions {

    private static final int FRAME_COLS = 8;
    private static final int FRAME_ROWS = 6;
    Animation walkAnimation;
    Texture walkSheet;
    TextureRegion[] walkFrames;
    SpriteBatch spriteBatch;
    TextureRegion currentFrame;
    float stateTime;

    private int textureWidth,textureHeight;
   
    public Explosions() {
     
        walkSheet = new Texture(Gdx.files.internal("Explosion2spritesheet.png"));

        TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight() / FRAME_ROWS);

        walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < FRAME_COLS; j++) {
                walkFrames[index++] = tmp[i][j];
            }
        }

        walkAnimation = new Animation(0.025f, walkFrames);
        spriteBatch = new SpriteBatch();
        stateTime = 0f;
       
        //Width ¨¨ Height
       textureWidth = (walkSheet.getWidth() / FRAME_COLS);  
        textureHeight = (walkSheet.getHeight() / FRAME_ROWS);
       
    }
   
   
    public TextureRegion getCurrentFrame()
    {
        stateTime += Gdx.graphics.getDeltaTime();                  
        currentFrame = walkAnimation.getKeyFrame(stateTime,false);  
       
        return currentFrame;
    }


    public int getTextureWidth()
    {
     return textureWidth;
    }

    public int getTextureHeight()
    {
     return textureHeight;
    }
   
   
   
   
   
}



So , i have some ideas...
One would make this class as a model, and create an explosion class and create an arrayList at gameupdate with the state and explosions positions, and keep drawing....

Well, i guess thats the only idea.

Btw, should i make an Atlas for just this img ?
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #2 - Posted 2013-05-19 00:46:41 »

I don't think I fully understand your question but here goes:

1. You need a class a class for your entity that is set up generally like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public Explosion (double x, double y){
this.x = x;
this.y = y;
}

public void update(){
//make a timer so the explosion will fade away or you can make it move... whatever you need
}

public void render(){
drawImage()//the function that actually draws your object
}


Then you need a class like a map or something, where you update and render all your entities. You will need a way to store your entities, most common is an arraylist for the obvious reasons. And you will need two main methods, one for drawing and one for updating. It will look something like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
ArrayList<//your entity> entityList;

public Game(){
//init all your objects
  entityList = new ArrayList<//your entity>();
}

public void update(){
for (int i = 0; i < entityList.size(); i++){
   entityList.get(i).update();
     }
}

public void render(){
for (int i = 0; i < entityList.size(); i++){
   entityList.get(i).render();
     }
}

}

Now, again I have no idea if this is what you mean, so sorry if it isn't!

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2013-05-19 03:05:53 »

Have you thought about using particles? Smiley
https://code.google.com/p/libgdx/wiki/ParticleEditor

Offline Andre Lopes
« Reply #4 - Posted 2013-05-19 03:44:51 »

Thats exactly what i needed.
I will think about that in "tha" morning. And lets see what the sun will bring Tongue

TY !!!!!
Offline Andre Lopes
« Reply #5 - Posted 2013-05-19 14:03:02 »

Alright, i followed your instructions and it worked like a charm.
Its way faster now, and it isnt bugging. Also, i figured it out how to check that is the last animation frame, so i can remove from the list.


1  
2  
3  
4  
 public boolean isFinished()
    {
       return explosionAnimation.isAnimationFinished(stateTime);
    }



TY Cheesy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (20 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!