We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, my current code is a racing game, the problem is detecting the collision with rockets. Currently there are problems
When an object is deleted, the main car also gets deleted
When an object and rocket collide, nothing happens
The rocket array doesn't append
The code doesn't run when I have a number in the rocket array
Can anyone please help, I've been trying to figure this out for a while now with no avail.
Rocket(float tempx, float tempy, float tempspeed, float tempsizex, float tempsizey, float tempcolor) {
x = tempx;
y = tempy;
sizeX = tempsizex;
sizeY = tempsizey;
speed = tempspeed;
rocketColor = tempcolor;
}
//If launched is false, then the rocket will continue moving down the screen, however if it is true
//then it will not
void update() {
if (!launched) {
y += speed;
}
}
//If powerup is false, and launched is true, then the rocket will fire by holding the mouse key
void launchspeed() {
if (!powerup) {
if(launched) {
y -= speed*2;
}
}
}
void display() {
fill(255);
rectMode(CENTER);
rect(x, y, sizeX, sizeY);
if (y >= height+50) {
// y = -50;
}
}
//If your car comes into contact with the rocket, it will pick it up
//the boolean powerup becomes true, activating the new location for the rocket, it now follows the car
void collected(Car car) {
boolean leftP = (x + sizeX/2 > car.x - car.sizeX/2);
boolean rightP = (x - sizeX/2 < car.x + car.sizeX/2);
boolean topP = (y + sizeY/2 > car.y - car.sizeY/2);
boolean bottomP = (y - sizeY/2 < car.y + car.sizeY/2);
if (leftP && rightP &&topP && bottomP) {
powerup = true;
}
}
//Follows the car, however, like mentioned in the Car class, it follows the car but not its true Y location to avoid moving the car when shooting the rocket
void follow(Car car) {
if (powerup) {
x = car.p;
y = car.c;
}
}
//When the boolean is true and the mouse is held, then powerup becomes false adn launched becomes true.
//When powerup becomes false, the rocket shoots upwards and hits an obstacle
//When launched becomes true, the original update loop doesnt run, but the launchspeed loop does, causing the rocket to fly upwards
void shooting() {
if (mousePressed && powerup) {
powerup = false;
launched = true;
}
}
void hit(Obstacle obstacle) {
boolean leftH = (x + sizeX/2 > obstacle.x - obstacle.sizeX/2);
boolean rightH = (x - sizeX/2 < obstacle.x + obstacle.sizeX/2);
boolean topH = (y + sizeY/2 > obstacle.y - obstacle.sizeY/2);
boolean bottomH = (y - sizeY/2 < obstacle.y + obstacle.sizeY/2);
if (leftH && rightH && topH && bottomH) {
obstacle.sizeX = 0;
obstacle.sizeY = 0;
}
}
void timerRocket() {
//Converts milliseconds to actual seconds
//int converts millis to integers, minus temp time
tC = intervalC+int(millis()/1000)-tempTimeC;
////nf formats the numbers into strings, so time = 00, it'll show the string time, and adds 2 zeros
timeC = nf(tC, 2);
////if the seconds equal 6 + car add, then the array appends and another car appears onscreen
//carAdd starts at 0, and when the first timer reaches 6, it adds another six, so when the timer reaches 12, it adds a car, and the variable carAdd goes to 18
if (tC == 6 + rocketAdd) {
//The new object being added to the array, spawns on a random lane
Rocket o = new Rocket(50 + b*floor(random(0, 5)), -80, speed, 10, 20, color(255, 0, 0));
rocket = (Rocket[]) append(rocket, o);
//Timer that adds the cars every six seconds
rocketAdd +=5;
}
}
}
Answers
Can you please edit your post to fix your formatting? While you're at it, can you narrow your problem down to a smaller example program instead of posting your full program?
Sorry I thought I fixed it, I edited some code out, but this is only two of the 7 classes I have.
That is a ton of code to ask us to debug for you. Please narrow the problem down to a smaller example.
done
No, what you've done is delete code that we needed to run it, that's more work for us.
post the longer version below please and tell us which line numbers we have to look at (this means, post the code, and post your comment, then write down the line numbers, then edit the post and write your line numbers and tell us where to look (or make a 2nd post with the line numbers from the first))
thank you!
Chrisir ;-)
The main part is void hit() On some objects it works and some it doesn't, despite being the same object.
this line:
the && means AND; but they can't all be true; what you want is || which means OR
Makes no difference in this case, and I tried || in other instances and had weird results. The obstacles which are being appended dont register as being hit, that is the problem
This is where I append the obstacles
Did you check whether the
if
clause ever gets true?to test this insert
println("append");
afterif (t == 6 + carAdd) {
I'd use something with
>=
like(without understanding what you mean here)
Yes it appends, I had println the location too on my updated code. I use the same code for the car, which it works without problem.
This is in another class.
What I found out though, is that when I call the function into another loop, an error occurs
rocket[i].hit(obstacle[i]); works fine here, but if I add it in
my obstacle code, I get an error "arrayindexoutofboundsexception 1"
which I find odd considering I have a similar instance in another piece of code.
in the above code, error "arrayindexoutofboundsexception 1" would occur
because of this line when there are more rockets than obstacles :
rocket[i].hit(obstacle[i]);
because you use i for both, and i being
i = 0; i < rocket.length;
this is still valid:
Remark
to check rockets against cars you need a nested for loop to check every rocket against every car:
Or every obstacle against every car:
Remark
To get real help either post your entire code OR an MCVE: https://stackoverflow.com/help/mcve
I don't have time, I am sorry.
Bye.
Here's the working code, problem still persists. Warning, its long