I have a character on screen that I’m trying to make it collect collectable items, however, with the if
statement in the for
loop present, it makes all the collectables disappear from screen; and without it, the collectables are drawn, but I’m only able to collect 1 and the other 2 disappear. The objective is the get the array to loop over and draw the collectables and have the collectables disappear as the game character walks over them.
var collectable
function setup(){
collectable = false
collectable = [
{ collectable_x_pos: 1220, collectable_y_pos: 100, isFound: false },
{ collectable_x_pos: 220, collectable_y_pos: 100, isFound: false },
{ collectable_x_pos: 1820, collectable_y_pos: 100, isFound: false },
];
}
function draw(){
if (collectable.isFound == false) {
for (var i = 0; i < collectable.length; i++) {
drawCollectable(collectable[i]);
checkCollectable(collectable[i]);
}
}
function drawCollectable(t_collectable) {
fill(0, 255, 255);
quad(
t_collectable.collectable_x_pos - 70,
t_collectable.collectable_y_pos + 277,
t_collectable.collectable_x_pos - 54,
t_collectable.collectable_y_pos + 331,
t_collectable.collectable_x_pos - 36,
t_collectable.collectable_y_pos + 277,
t_collectable.collectable_x_pos - 54,
t_collectable.collectable_y_pos + 222
);
}
function checkCollectable(t_collectable) {
if (
dist(gameChar_x, gameChar_y, t_collectable.x_pos, t_collectable.y_pos) < 20
) {
console.log("Collected");
collectable.isFound = true;
}
}