We are about to switch to a new forum software. Until then we have removed the registration on this forum.
ArrayList bales;
Bala bala1;
ArrayList soldats;
Soldat soldat1;
boolean matar= false;
void setup() {
size(600, 500);
stroke(255);
fill(255, 255, 255);
bales = new ArrayList();
bala1 = new Bala();
soldats= new ArrayList();
soldat1 = new Soldat();
bales.add( bala1 );
soldats.add (soldat1);
}
void draw() {
background(51);
line(300, 500, mouseX, 2 * mouseY);
strokeWeight(mouseY/10);
for ( int i = 0; i < bales.size(); i++) {
bala1 = (Bala)bales.get(i);
bala1.balesmoviment();
for ( int i = 0; i < bales.size(); i++) {
soldat1 = (Soldat)soldats.get(i);
soldat1.soldatsmoviment();
}
}
if (balapx = soldatpx) {
matar=true; // matar is kill, when bala collision rectangle (soldat=soldier) soldier die (dissapear)
class Bala {
float balapx, balapy;
float balavx, balavy;
Bala() {//creamos una bala
balapx = width/2.0;
balapy = height;
balavx = map(mouseX, 0, width, -10, 10);
balavy = -10;
}
void balesmoviment() {
balapx += balavx;
balapy += balavy;
ellipse( balapx, balapy, 10, 10);
}
//================================================================
class Soldat { // Here the problem, how to make rectangles that move and avance in the map
float soldatpx, soldatpy;
float i;
Soldat() {//creamos unsoldat
soldatpx = width+i;
soldatpy = 0;
}
void soldatsmoviment() {
soldatpx +=i;
rect( soldatpx, balapy, 10, 20);
}
}
}
}
void mousePressed() {
if ( bales.size() > 9 ) bales.remove(0);
bales.add( new Bala() );
}
Don't do pro things
Answers
You'll have to be more specific with what you're asking.
http://Studio.ProcessingTogether.com/sp/pad/export/ro.9qPrLGrYGkr2o
when 1 array touch another array the rect dissaper
i wanna to shoot rectangles and when the balls(bullets) impact on rectangles, the rect die
http://JeffreyThompson.org/collision-detection/point-rect.php
http://JeffreyThompson.org/collision-detection/circle-rect.php
Loop over all the objects in one ArrayList.
Within that loop, loop over all the objects in the other ArrayList.
Within both of those loops, you have two objects that you are working with. Using the above links, determine if they collide. If they do, whatever happens when they collide can happen to both of the objects you are currently working with.
If you're going to remove objects from their ArrayList, the trick is to loop over the objects in reverse.
STOP VANDALISING YOUR PREVIOUS QUESTIONS
https://forum.processing.org/two/discussion/21148/do-not-delete-your-posts
please tf guy 44, is the final question.
How I can change to make it work
Your code is a mess. You need to learn how to pair up curly brackets (
{}
) properly. Right now you are defining your Bala class inside your draw() function. What worries me the most is that you don't realize this. It shows me you have a basic lack of understanding about code and functions. Do you really need a lesson in syntax?Use command-T in PDE to auto-indent and clean up code -- this will make it obvious that, for example, your class is inside draw.
Reading the error messages in the errors tab will also be helpful in many cases if your nesting is off....
Ok, im gonna to correct them, I wrote this so fast.
Li8ke this???
sorry, here
I can do it in this way
Look at this code you have in draw():
It's getting close. Except there are three problems.
1) You iterate over the Soldats inside the loop for the Balas - essentially, you move each Soldat as many times as you have Balas. For example, if you have ten Balaes, then each time this runs, each Soldat will move ten times! You need to have ONE loop that moves all the Balas. Then, AFTER that loop, you need a SECOND loop that moves all your Soldats.
2) This is why you were close: You do have two loops where one is inside the other. But you don't need nestled loops for movement - you need them to check for collisions. The first loop starts, and you are looking at the first Bala. You know which Bala it is because you are get()ting it from the ArrayList of them and storing it in the bala1 variable. Next, you do need to loop over every Soldat. In side that second loop, you will (on the first time it loops) have the first Soldat stored in the soldat1 variable. So now, inside both loops, you need to see if bala1 and soldat1 collide...
3) You have totally ignored all attempts to point you at examples and explanations that demonstrate rect-circle collision, and you still don't seem to understand how to access class member variables for objects.
Here's what you should do:
Remember: If you've put an object in my_object:
Then you can access that object's class members directly:
Also, checking that two objects have the same X position is not a super great way to know if they are colliding. Especially not if you use an assignment operator (
=
) instead of a equality comparison operator (==
). Read the links provided about rect-circle collision. Your rect should have an x position, y position, width, and length. Your ellipse should have an x position, y position, and radius. You will have to check multiple conditions to see if they collide - not just one check that their x positions are EXACTLY the same (which is super unlikely anyway, since they're float variables!).This is all super good advice. Here's some more: No one is going to do it for you. You will get the grade you deserve. And this is coming from the person on these forums who is the most likely to give you a full-code solution (Teachers hate him! This one weird trick will amaze you!). You haven't convinced me you know your shit.
And if you're removing items from your ArrayList, as I've said before, iterate over the objects in that ArrayList in reverse.
Vandalise any more of your questions and you will end up being banned. That's three now and I've warned you before.
All the help all the people here have given you and you keep deleting your text.
Yea,yea, the code It s doid by me, under your instructions, and I don't do any copy paste, remember. Your explanation is perfect because you don't give me any example or solution, I liked and i can doid well under your instrictions and your knowledge.
"And if you're removing items from your ArrayList, as I've said before, iterate over the objects in that ArrayList in reverse." What Im removing?? where?
"You haven't convinced me you know your shit." What are you telling me?
Im not the only of the class that you help
I can do another way to do balavx ( the direction of the bullets) because I don't know why draw 2 ellipses, and when the mouse change the position, the bullet begins big. I know that is for the map
map(value, start1, stop1, start2, stop2)
better
but the bullet change of the posisition (I don't like map)
Better?
void draw() { background(51); image(img, 0, 0); line(300, 500, mouseX, 2 * mouseY); strokeWeight(mouseY/10);
This is correct, I want soldiers to appear on the right side of the screen in different positions and you have to shoot them.
???
Your thoughts, like your code, and a mess. They are all over the place. It is impossible to even follow along with what you are trying to do any more.
Let's try this one last time.
Software development is a process. Start simple.
We can start with a blank sketch that defines two kinds of objects:
Run this code. You get a blank sketch. While we have defined two new kinds of objects, we are not using them yet - and that's just fine. There are no details about them anyway.
We can add some details about our objects. Imagine that you are a Brick. What do you need to know about yourself to be a good brick?
You might need to know where in the world you are. That is your position.
You might need to know how big you are. That is your size.
You might need to know what color you are.
You might need to know if you exist at all!
We can now add these details about what a brick is to the Brick class:
Next, what do you need to know to be a good Ball? A position, yes. A color, sure. Size? A Ball's size is just one value - its radius. And again, a boolean to remember if it even exists.
So we can add those details to the Ball class:
Okay. Go back to thinking you are a Brick. Now that you know your position, size, color, and if you exist, what do you look like? Chances are good that you are some type of colored rectangle. So we should add a method to the Brick class that allows us to draw a Brick.
Notice that I am keeping the indenting nice and making sure that my curly brackets all match up.
Back to the Ball. Imagine you are a ball. What do you look like? An ellipse, yes. So add a function that draws the Ball.
Notice that the sketch still doesn't show any Bricks or Balls. This is because the main draw() function is not telling any Bricks or Balls to be drawn. Also, do not get confused by the fact that there are three draw() function! One is the main draw() function, which draws the entire sketch. The other two are inside classes, and allow objects of those classes to be drawn.
Let's focus on the Bricks again now, and see if we can actually get one to be drawn.
We should actually create a Brick.
Then we should draw that Brick.
Here goes:
Where is the brick? if you debug this sketch, you will discover that the brick doesn't appear because both it's width and length are 0 (and so it is not drawn). We should make sure we set the values for out brick a little better.
Behold:
Once we tell out Brick where it is, how big it is, that it exists, and is red, it appears!
You may have noticed that I had to type "a_brick" like six times to set all the values for the brick. Since I do no want to have to type the name of every brick I want to make, I can simply pass parameters to the Brick directly when I call it's constructor function (which we'll add to the Brick class now):
Great, we still can see our brick!
It's just that I've become a mess, but I know all of thiiiisss!!! Why you are teeling me that .from the respect
AAAAA, I understand
yes
Now let's have lots of Bricks. Since I may eventually want to add or remove many bricks, I will store my bricks in an ArrayList(). Let's switch from having one Brick to many Bricks now.
Super. As you can see, I can now have as many Bricks as I like.
Now I take a break, and you bring the Ball class up to speed. Follow the same steps as we did above with the bricks, but for the Ball class. The goal is to be able to have and see many Balls. Post your attempt at doing this for more help.
why do you in my code??
And now I have to do the velocity of the ball, but we can do in my code please??
You just posted this code:
Did you even try to run it? It has several serious errors. For a start, the parameters to the Ball() constructor aren't all floats, and you don't need that many of them. Then you have duplicate setup() and draw() functions. Plus you should be adding Balls to the
balls
ArrayList, not Bricks, and you need to pass a matching list of parameters.Always make sure that the code you have runs without errors! It's an important property to maintain in your code... that way you know things remain okay!
yea yea, but i have to do my game, so i cannot spend time with another stuff. No? please helps me, what can i do
Fine. Here, write this function:
You need it.
ok I did
Let's see it then.
ok I m doing
Now I havee to change the boolean, but i can go in this direction. I can put inseide the draw?
http://www.jeffreythompson.org/collision-detection/circle-rect.php
I'm done.