We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi the current code about car and hammer, when running my code will display one car and when break the car will display car broke but my collision code doesn't produce new car automatically when breaking the car. where is my fault? any help Thanks
PImage car, crashed, hammer, broke;
float[] x, y; // car locations array
float[] shot; // car broke binary boolean array, 1 = broke, 0 = not broke
int score=0; // increments when broke.
void setup() {
size(800, 400);
x=new float[0];
y=new float[0];
shot=new float[0];
car =loadImage("car.png") ;
crashed =loadImage("crashed.png") ;
hammer =loadImage("hammer.png") ;
broke =loadImage("broke.png");
x =append(x, mouseX); // random location
y =append(y, mouseY);
shot =append(shot, 0); // used as a boolean and matches to each individual car, 0 = car not broke, 1 = broke.
}
void traffic() { // draw the cars in memory to the screen.
for (int i=0; i<x.length; i++) {
if (shot[i]==1) {
image(crashed, width/2, height/2);
} else {
image(car, width/2, height/2);
}
}
}
void collision() { // detect collision between hammer and car
for (int i=0; i<shot.length; i++) {
if (x[i]>=shot[i] && x[i]<=(shot[i]+30) && y[i]>=shot[i] && y[i]<=(shot[i]+10))
shot[i] = 1;
x =append(x, mouseX); // random location when old cars crashed.
y =append(y, mouseY);
shot =append(shot, 0);
score++; //increment score
}
}
}
void draw() {
background(255);
traffic();
fill(0);
textSize(20);
text("score: ", score, 50, 50);
if (mousePressed) { // image swap
collision();
image(hammer, mouseX, mouseY); //draw hammer image to around mouse locaiton
} else {
image(broke, mouseX, mouseY); // if not pressed then alternative image.
}
}
Answers
First suggestion:
x=new float[1];
Second suggestion: Move this next to
void mousePressed(){...}
instead.This doesn't solve the issue but it is a start. Why do you want to append to the arrays? It is possible ArrayList could more suitable for you:
https://processing.org/reference/ArrayList.html
Kf
the first suggestion will produce stop the program
The append seems ok (it's also legit to use append instead of ArrayList for a start), BUT:
you append to x and y but you show all images in the same spot?
here:
maybe
NEXT
this
should be RANDOM:
Same in setup() as in collision:
should be
I would use this
throughout, meaning outside the
if
clause right aftertraffic()
so the hammer is shown always.You can combine this with
noCursor();
in steup() to hide the mouse cursor. Thus only the hammer is displayed throughoutyou got this fancy line which is nonsense:
because
shot
is just telling you if the car number i is broken or notYou can't check position against it!!!!!!!!!!!!!!!!!!!!!!
instead you want to check mouseX and mouseY against all cars x[i] y[i]
BESIDES
BESIDES it's vital that the line ends with
{
to enclose the whole section into what happens whenif
clause is true.without
{
only shot[i] = 1; is in the if clause, see indents here:with {...}
(I think you had the bracket { once because you have the correspondent closing } )
you can get auto-indent to see those vital errors by hitting ctrl-t in processing
I use it all the time
Thanks Chrisir for all, but when running the code will display 1 car but I can't shot it because x,y and shot arrays=0(setup) whereas when change x,y and shot arrays to 1 (setup) will display 2 cars and I can shot one car only , why?
post your entire code
in setup
the current code will display 2 cars but only one shot :
if change x=new float[0], y=new float[0], shot=new float[0] will display 1 car but no shot
I understand hammer and car and crashed but what's the broke image??
as said this is so wrong
You want something like
(see dist() in the reference)
OR
Thanks for all, the broke image is an alternative image for hammer , and about your collision code if(dist(mouseX,mouseY,x[i],y[i]) < 50) and the second code will display 1 car and when shot it will display crashed car image with more than 10 new cars image, each shot will produce again 10 new cars. Thanks again I will try to fix it.
as for the ten times issue:
read again
above, by kfrajer.
do this, and it will be healed
Thanks, Chrisir and kfrajer
;-)
please post your entire code so we can see the result
thank you