CodingTrain / Suggestion-Box

A repo to track ideas for topics
571 stars 86 forks source link

my flower aint deleting #979

Open ctrembla opened 6 years ago

ctrembla commented 6 years ago

So I'm trying to create a javascript space invaders game I got flowers, a spaceship & some bullet, but for some reason 2 of my 6 flowers refuse to be deleted no matter how many bullets i send into them. It starts off with 6 flowers and only 4 of them will allow me to delete them. space

here the flower code:

`function Flower(x,y) { this.x = x; this.y = y; this.r = 15; this.life = 1; this.toDelete = false; this.xdir = 1; this.grow = function(){ this.life -= 1; if (this.life <= 0){ this.toDelete = true; } } this.shiftDown = function(){ this.xdir = -1; this.y += this.r2; } this.move = function() { this.x = this.x + this.xdir; } this.show = function(){ fill(255,0,200); ellipse(this.x,this.y,this.r2,this.r2); }

}`

here the place where it is shown and deleted on the main
`var edge = false;

for (var i = 0; i < flowers.length; i++){
    flowers[i].show();
    flowers[i].move();
    if (flowers[i].x > width || flowers[i].x < 0){
        edge = true;
    }
}

if (edge) {
    for (var i = 0; i < flowers.length; i++){
        flowers[i].shiftDown();
    }
}
for (var i = drops.length-1; i > 1; i--){
    if (drops[i].toDelete){
        drops.splice(i,1);
    }
}

for (var i = flowers.length-1; i > 1; i--){
    if (flowers[i].toDelete){
        flowers.splice(i,1);
    }
}`

can anyone tell me why the flowers wont delete like the other 4? if need to see anymore just asked I copied it from space invaders coding train with an added life.

JohnyDL commented 6 years ago

Where do you actually do the check for "toDelete" and collision detection? I don't see it

Also you should do things Update, Colision Detection, Show or the flowers end up being a little ahead of where they are on screen

Also if you start your code blocks with `JavaScript it'll colour code it for you

ctrembla commented 6 years ago

you mean like javascript like I used html? I took a html class and thought I'd try turning my java code into js, since I dont know how to place java on html. here the bullet code that makes the collisions

`function Drop(x,y) { this.x = x; this.y = y; this.r = 4; this.life = 1; this.toDelete = false;

this.show = function(){
    fill(0,0,250);
    ellipse(this.x,this.y,this.r*2,this.r*2);
}
this.evaporate = function(){
    this.toDelete = true;
}
this.hits = function(flower){
    var d = dist(this.x,this.y,flower.x,flower.y);
    if (d < this.r + flower.r){
        return true;
    } else{
        return false;
    }
}
this.move = function(){
    this.y = this.y - 5;
}

}`