We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm sure this question has been asked and answered billions of times but I can't find anything that works for me. i am making a Break Out type game and I have the bricks setup in arrays, and when the ball hits a brick I want the brick to stop being drawn so I want to take it out of the array. If there is any other way to solve this problem that would be helpful too, thanks!
Oh and here is my code (at least that part of it):
for (var i = 0; i < 8; i++) {
brickX = i * 50;
bricks1[i] = brickX + (i + 1) * 11;
bricks2[i] = brickX + (i + 1) * 11;
bricks3[i] = brickX + (i + 1) * 11;
bricks4[i] = brickX + (i + 1) * 11;
fill(0, 0, 255);
rect(bricks1[i], 10, 50, 10);
fill(255, 0, 0);
rect(bricks2[i], 30, 50, 10);
fill(0, 255, 0);
rect(bricks3[i], 50, 50, 10);
fill(100, 0, 100);
rect(bricks4[i], 70, 50, 10);
if (ball.x >= bricks1[i] && ball.x <= bricks1[i] + 50 && ball.y >= 10 && ball.y <= 10 + 10) {
bricks1[i] = 800;
ball.yDir *= -1;
} else if (ball.x >= bricks2[i] && ball.x <= bricks2[i] + 50 && ball.y >= 10 && ball.y <= 30 + 10) {
bricks2[i] = 800;
ball.yDir *= -1;
} else if (ball.x >= bricks3[i] && ball.x <= bricks3[i] + 50 && ball.y >= 10 && ball.y <= 50 + 10) {
bricks3[i] = 800;
ball.yDir *= -1;
} else if (ball.x >= bricks4[i] && ball.x <= bricks4[i] + 50 && ball.y >= 10 && ball.y <= 70 + 10) {
bricks4[i] = 800;
ball.yDir *= -1;
}
}
Answers
Edit your post (gear icon in the top right corner of your post), select your code and hit ctrl+o to format it. Make sure there is an empty line above and below your code.
Kf
oh thanks
https://Developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
https://Forum.Processing.org/two/discussions/tagged?Tag=splice()
I thought this would work but then I noticed my real problem which was even after deleting spots they would get put back the next time the for loop runs
Arkanoid!!! I love it.
You could define your bricks in
setup
and display them indraw()
.You need to change your data structure I am afraid - you need to store each brick separately and not column wise.
You could make parallel arrays for x position for y position and for color and for isAlive.
So you have arrays brickX, brickY, brickColor, brickIsAlive
Each the same size.
Element 0 in each array gives you the distributed data for one brick (number 0).
To delete one brick just set
brickIsAlive[i]
tofalse
and don't display it (and don't check it for collisions).Discussion:
https://forum.processing.org/two/discussion/8082/from-several-variables-to-arrays
object oriented programming
Later check the technical faq for getting from here to object oriented programming:
https://forum.processing.org/two/discussion/8081/from-several-arrays-to-classes
thanks!!
You're welcome!
Here is a version with object oriented programming.
Click on the bricks to delete them.
Best, Chrisir ;-)
more fancy ideas here:
https://forum.processing.org/two/discussion/comment/100139/#Comment_100139