The bullet flies towards player using a class
This commit is contained in:
@@ -8,8 +8,7 @@ class bullet {
|
||||
this.x = 500;
|
||||
this.y = 100;
|
||||
this.radius = 10;
|
||||
this.speed = speed;
|
||||
|
||||
this.speed = 5;
|
||||
|
||||
|
||||
this.directionX = null;
|
||||
@@ -21,7 +20,7 @@ class bullet {
|
||||
|
||||
this.projectile = createVector(600, 100);
|
||||
// Calculate the initial direction
|
||||
this.direction = createVector(this.targetX - this.projectile.x, this.targetY - this.projectile.y);
|
||||
this.direction = createVector(this.targetx - this.projectile.x, this.targety - this.projectile.y);
|
||||
this.direction.normalize();
|
||||
|
||||
}
|
||||
@@ -33,15 +32,25 @@ class bullet {
|
||||
//push en pop zorgen er voor dat de code tussen push en pop een eigen canvas heeft. Dus dat er meerdere projectielen tegelijk kunnen zijn.
|
||||
push();
|
||||
fill(0, 255, 0);
|
||||
circle(500, 500, this.radius);
|
||||
circle(this.projectile.x, this.projectile.y, this.radius);
|
||||
pop();
|
||||
|
||||
// Set the initial position of the projectile to the player's position
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// // Move the projectile towards the movable circle
|
||||
update(targetx, targety) {
|
||||
this.targetx = targetx;
|
||||
this.targety = targety;
|
||||
this.projectile.add(p5.Vector.mult(this.direction, this.speed));
|
||||
|
||||
|
||||
if (dist(this.projectile.x, this.projectile.y, this.directionX, this.directionY) <= this.radius) {
|
||||
if (dist(this.projectile.x, this.projectile.y, this.targetx, this.targety) <= this.radius) {
|
||||
hit = true;
|
||||
}
|
||||
else if (this.projectile.x < 0 - 10 || this.projectile.x > width + 10 || this.projectile.y < 0 - 10 || this.projectile.y > height + 10) {
|
||||
@@ -49,12 +58,6 @@ class bullet {
|
||||
bounceY = this.projectile.y;
|
||||
shot = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// // Move the projectile towards the movable circle
|
||||
update() {
|
||||
this.projectile.add(p5.Vector.mult(this.direction, this.speed));
|
||||
}
|
||||
|
||||
|
||||
|
@@ -115,9 +115,9 @@ function setup(){
|
||||
function draw(){
|
||||
keyPressed()
|
||||
// clear the background with a transparent black color
|
||||
background(0,0,0,10);
|
||||
background(0,0,0,100);
|
||||
myBullet.draw();
|
||||
myBullet.update();
|
||||
myBullet.update(playerPosX, playerPosY);
|
||||
// draw a circle at the mouse position
|
||||
circle(playerPosX, playerPosY, radius);
|
||||
}
|
||||
|
Reference in New Issue
Block a user