game - checking "hitted" / multiple shots.

Hi. Ive been creating a game. And im in this situation, where I have a target (the player), wich is getting shoted. It works, his points lifes goes down with every taken damage. But, I added a visual effect too, changing the color of the player to white, when the player is reached for a bullet. And It works too, but only with the last projectile type. For the player I created a class. There is 3 type of bullets, the first kind are stored in an array, and variables controling the position of a line for the last two types.
The player "checker"

    check(px, py){
            if(px < this.x + this.r && px > this.x - this.r && py < this.y + this.r && py > this.y - this.r){
                this.vidas--;
                this.colorChange = true;
                return true;
            } else { this.colorChange = false; }
        }
    show(){
            push();
            if(this.colorChange){
                fill(255);
            } else {
                fill(250, 50, 100);
            }
            rect(this.x, this.y, this.r * 2, this.r * 2);
            pop();

        }

And then in the sketch

for(var i = 0; i < ataques.length; i++){
        if(karla.check(ataques[i].ptx, ataques[i].pty)){
            ataques.splice(i, 1);
            ataques.push(new Tela(karla.x));
        }
    }

if(karla.check(xa, yarañitas)|| xa < -500){
        xa = 500;
      }

if(karla.check(xb, yarañitas) || xb > 500){
        xb = -500;
      }

As I said, the points life thing works fine with all the bullet. But the changing color does not. Only with the last one. I tried using different function for each bullet type. No change..
Hope you can help me with the given code, otherwise let me know, and I'll share the needed part. Thanks !!

Tagged:

Answers

  • Answer ✓

    this is the problem:

    } else { this.colorChange = false; }
    

    it resets the variable colorChange

    instead try to get rid of this else statement and instead before all tests say once: this.colorChange = false;

    then all tests have the change to say this.colorChange = true; without it can be overwritten by the next test with this.colorChange = false;

    Chrisir ;-)

Sign In or Register to comment.