We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey guys, I'm an amateur programmer and I totally don't get classes when it comes to arrays, or arrays in array classes in sub classes,.. It totally blows my mind.
I made a code and I have so much difficulties with placing it in a class. would someone of you guys help me learn the workflow by the hand of my code/? I even get stuck with raising or reducing my angle of the first array.
It's just a small code with some trigonometry for a plane character (based on MineStorm II 1983):
int fadeAmount=3;
int fadeCountAllow;
boolean move=false;
float oldX, oldY;
float angle;
float[] middlePlayerX=new float[fadeAmount]; float[] middlePlayerY=new float[fadeAmount];
float gunRadius=50;
float[] gunPointX=new float[fadeAmount]; float[] gunPointY=new float [fadeAmount];
float sideRadius=19.3;
float[] side1X=new float[fadeAmount]; float[] side1Y=new float[fadeAmount]; float[]side2X=new float[fadeAmount]; float[]side2Y=new float[fadeAmount];
float middleRadius=10.7;
float[] middle1X=new float[fadeAmount]; float[]middle1Y=new float[fadeAmount]; float[] middle2X=new float[fadeAmount]; float[] middle2Y=new float[fadeAmount];
float endRadius=35.7;
float[] end1X=new float[fadeAmount]; float[] end1Y=new float[fadeAmount]; float[] end2X=new float[fadeAmount]; float[] end2Y=new float[fadeAmount];
float speed=8;
void setup()
{
size(900,900);
strokeWeight(4);
stroke(255);
middlePlayerX[0]=width/2; middlePlayerY[0]=height/2;
}
void draw()
{
background(50);
gunPointX[0]=gunRadius*cos(angle);
gunPointY[0]=gunRadius*sin(angle);
middle1X[0]=middleRadius*cos(angle-HALF_PI);
middle1Y[0]=middleRadius*sin(angle-HALF_PI);
middle2X[0]=middleRadius*cos(angle+HALF_PI);
middle2Y[0]=middleRadius*sin(angle+HALF_PI);
end1X[0]=endRadius*cos(angle-HALF_PI-THIRD_PI);
end1Y[0]=endRadius*sin(angle-HALF_PI-THIRD_PI);
end2X[0]=endRadius*cos(angle+HALF_PI+THIRD_PI);
end2Y[0]=endRadius*sin(angle+HALF_PI+THIRD_PI);
side1X[0]=sideRadius*cos(angle-THIRD_PI-THIRD_PI);
side1Y[0]=sideRadius*sin(angle-THIRD_PI-THIRD_PI);
side2X[0]=sideRadius*cos(angle+THIRD_PI+THIRD_PI);
side2Y[0]=sideRadius*sin(angle+THIRD_PI+THIRD_PI);
if(move)
{
middlePlayerX[0]=middlePlayerX[0]+cos(angle)*speed;
middlePlayerY[0]=middlePlayerY[0]+sin(angle)*speed;
}
for(int i=0; i<fadeAmount; i++)
{
stroke(255,255-i/float(fadeAmount)*255);
if(i==0)
{
line(middlePlayerX[i]+middle1X[i],middlePlayerY[i]+middle1Y[i],middlePlayerX[i]+gunPointX[i],middlePlayerY[i]+gunPointY[i]);
line(middlePlayerX[i]+middle2X[i],middlePlayerY[i]+middle2Y[i],middlePlayerX[i]+gunPointX[i],middlePlayerY[i]+gunPointY[i]);
}
line(middlePlayerX[i]+end1X[i],middlePlayerY[i]+end1Y[i],middlePlayerX[i]+middle2X[i],middlePlayerY[i]+middle2Y[i]);
line(middlePlayerX[i]+end2X[i],middlePlayerY[i]+end2Y[i],middlePlayerX[i]+middle1X[i],middlePlayerY[i]+middle1Y[i]);
line(middlePlayerX[i]+side1X[i],middlePlayerY[i]+side1Y[i],middlePlayerX[i]+end1X[i],middlePlayerY[i]+end1Y[i]);
line(middlePlayerX[i]+side2X[i],middlePlayerY[i]+side2Y[i],middlePlayerX[i]+end2X[i],middlePlayerY[i]+end2Y[i]);
}
fadeCountAllow++;
if(fadeCountAllow==4){
for(int i=fadeAmount-1; i>0; i--)
{
middlePlayerX[i]=middlePlayerX[i-1];
middlePlayerY[i]=middlePlayerY[i-1];
gunPointX[i]=gunPointX[i-1];
gunPointY[i]=gunPointY[i-1];
middle1X[i]=middle1X[i-1];
middle1Y[i]=middle1Y[i-1];
middle2X[i]=middle2X[i-1];
middle2Y[i]=middle2Y[i-1];
end1X[i]=end1X[i-1];
end1Y[i]=end1Y[i-1];
end2X[i]=end2X[i-1];
end2Y[i]=end2Y[i-1];
side1X[i]=side1X[i-1];
side1Y[i]=side1Y[i-1];
side2X[i]=side2X[i-1];
side2Y[i]=side2Y[i-1];
}
fadeCountAllow=0;
}
}
void keyPressed()
{
if(keyPressed)
{
if(keyCode==UP) { angle+=0.5; }
if(keyCode==DOWN) { angle-=0.5; }
if(key==' ' )
{
move=true;
}
}
}
void keyReleased()
{
if(key==' ') { move=false; }
}
Answers
Take it slowly- learn about classes in Java, then read up the tutorials in the Processing website.
I would like to, but school is pushing me too hard, I'm yet one of the latest sheeps in programming. See, I have exams coming so all my time is being compressed by projects.
There is a tutorial on objects in the website
Remember to hit ctrl-t in procession to get auto-indent
To use objects, make the smallest entity: when you have a grid of cells make a class Cell (not class Grid)
When you have multiple players make a class Player
I am not sure what your code does of why middlePlayerX is an array. Do you have multiple players?
no it is to leave a trail behind my plane thanks anyway it seems to help a little bit
do this
https://www.processing.org/tutorials/objects/
SO
all those are arrays of length
fadeAmount
:So instead of using parallel arrays (where the same line of every array describes one player (fading)): use a class Player and an array of that class:
Now the class Player looks like this:
see
https://forum.processing.org/two/discussion/8081/from-several-arrays-to-classes
Got it! Thanks it does the job.
In my code I have an arrayList of a class, I can .add objects to it but I don't succeed at appending an array of a class. I did something like:
It shows me that it's wrong but I don't find a lot on the internet for how it works to append an array of a class, can you tell me what suppose to change on this line?
https://Processing.org/reference/append_.html
https://forum.Processing.org/two/discussion/8080/why-use-arraylist-instead-of-array-with-append
SomeClass[] items = (SomeClass[]) append(originalArray, element)
Thanks for answering guys it helps me alot for my exams! I have yet seen the reference page but I don't get what i suppose to fill in items? something random? But than it says that its not being used?
Given the function append() creates a clone of the passed original array, we need to assign it to some variable. Generally the very same variable pointing to the original array:
Again, arrays aren't meant to have its length changed all the time. It's an awkward procedure! :-&
For that lists, like the famous ArrayList, are more apt for the task: *-:)
its okay I'm sorry guys, thanks for the help! enemyArr= (Enemy[])append(enemyArr,enemy);