Drawing order again
in
Programming Questions
•
3 years ago
I'm trying to make a chain reaction system. But i'm stuck much earlier in the code...
I've built a grid of "squares objects" to begin. Then i'm adding movement.
So, for now, there is one "test square" that moves, following mouse clicks.
The goal is to make all the squares moving ones, triggered by others.
But, before that, i realized that, if one clicks to the left and/or to the bottom of the screen, my square goes under the others, and disappears.
I've being reading other topic about similar issue. But i think my problem goes a little different.
I'm using a
bi-dimensional Array of objects, and their index (array[index][index2]) are, how i handle them.
I have tried to use the suggestion in the other topic to sort them in an ArrayList by Y position.
I'm not sure if just because i don't really know how to use ArrayList properly or because the approach is not suitable for that case. But is not working so far.
My first problem with sorting is the bi-dimension, An ArrayList can't have 2 dimensions, can it?
I understood that it can't so i'm having some trouble to go from one to two dimension...
But i did it, not sure if it's really correct, but it runs.
And then as the, for instance, square[1][2] moves, it is sorted and then it is not the square[1][2] anymore!!!
Hummm... Don't know...lots of ways to go...
So this are the stuff that crosses my mind about all that:
I can keep trying the sort approach. But i need to sort by Y and X positions (how?) and also i need to have a different way to "refer" to each square than their index. Maybe a kind of an ID for each one so i can interact with them besides their sort order. Also i need to improve mine 2d to 1d conversion (i've done this once, iknow it can work ok).
Also as i said ArrayList is hard for me (not much in processing reference and the java reference is harder to understand),
so i can't call an object method straight to an ArrayList element (i can't figure the proper grammar there is that "casting" and i get confused). For instance arrayList.get(i).method() is not working properly. So i'm sorting and sending them back to an array to call the methods. This could not be the only way and probably is an estupid detail i'm missing.
Other thought is to use PGraphics, to draw everything that moves... But again, i remember to have difficulties to cal custom methods in PGraphics. Other day, before i start this, i saw a topic about this, but can't find it again.
That is it. Any help will be very appreciated.
Here is the code WITHOUT the sort try( no ArrayList), cause it was TO quick and TO dirt to post. I'm improving it and posting later.
Also, I'm an apprentice and a hobbyist, all i learned was by myself digging in the internet. So ANY kind of comments about my code are very welcome.
the sketch (and code) is also @:
- Quad[][]all = new Quad[80][80];
- void setup()
- {
- size (800,800);
- for (int i=0;i<80;i+=1)// populate the array
- {
- for (int a=0;a<80;a+=1)
- {
- all[i][a]=new Quad(i*10,a*10);
- }
- }
- background (0);
- }
- void draw()
- {
- for (int i=0;i<80;i+=1)
- {
- for (int a=0;a<80;a+=1)
- {
- all[i][a].q();
- all[i][a].calcVel();
- }
- }
- }//<<< end of Draw loop
- void mousePressed()
- {
- all[20][30].setDest(mouseX,mouseY);
- }
- //----
- class Quad
- {
- float posx, posy;
- float destx, desty;
- int velx, vely;
- boolean chegou;
- Quad()// constructor vazio
- {
- }
- Quad(float inp_x, float inp_y)// constructor
- {
- this.posx =inp_x;
- this.posy =inp_y;
- this.destx=this.posx;
- this.desty=this.posy;
- }
- void q() // draws
- {
- rect(this.posx,this.posy,10,10);
- }
- float qPosx()// ret posX
- {
- return this.posx;
- }
- float qPosy()//ret posY
- {
- return this.posy;
- }
- void calcVel()
- {
- if (this.posx > this.destx)
- {
- this.velx= -1;
- }
- else
- if (this.posx < this.destx)
- {
- this.velx= 1;
- }
- else
- if(this.posx == this.destx)
- {
- this.velx=0;
- }
- if (this.posy > this.desty)
- {
- this.vely= -1;
- }
- else
- if (this.posy < this.desty)
- {
- this.vely= 1;
- }
- else
- if(this.posy == this.desty)
- {
- this.vely=0;
- }
- if (this.destx != this.posx || this.desty != this.posy)
- {
- this.posx = this.posx+this.velx;
- this.posy = this.posy+this.vely;
- this.chegou = false;
- }else{this.chegou=true;}
- }
- void setDest (float inp_destx, float inp_desty)
- {
- this.destx =inp_destx;
- this.desty =inp_desty;
- }
- }//<<<< FIM class Qad
1