Básicamente, tengo un código que dispara círculos al mouse de un jugador (también un círculo) cuando se presiona el mouse. Estoy tratando de crear otro círculo que también dispare al mouse (idealmente desde una tecla en lugar de presionar el mouse), pero me cuesta hacerlo. He pegado mi código p5.js. ¡Agradecería mucho cualquier sugerencia!
var bullets = []; function setup() { createCanvas(600, 600); player = new Player(); } function draw() { clear(); background(51); player.show(); for (let i = 0; i < bullets.length; ++i) { bullets[i].toMouse(); bullets[i].show(); } } function mousePressed() { if (mouseX != player.x || mouseY != player.y) { bullets.push(new Bullet(mouseX, mouseY, player.x, player.y)) } } function Bullet(X, Y, PX, PY) { this.speed = 5; this.x = PX; this.y = PY; this.dir = createVector(X - PX, Y - PY).normalize() this.r = 7; this.show = function() { fill(255, 255, 0); stroke(128, 128, 0); circle(this.x, this.y, this.r); circle(this.x + 50, this.y, this.r); } this.toMouse = function() { this.x += this.dir.x * this.speed; this.y += this.dir.y * this.speed; } } function Player() { this.x = width / 2; this.y = 450; this.r = 20; this.speed = 4; this.show = function() { fill(0, 255, 0); stroke(0, 128, 0); circle(this.x, this.y, this.r); circle(this.x + 50, this.y, this.r); } } }
Soy nuevo en p5 pero creo que puedo ayudar un poco.
No pude ejecutar su código, pero estoy bastante seguro de que funcionaría si hace que la viñeta calcule la diferencia en los valores x e y entre ella y el reproductor, y luego los agrega a su posición actual.
Entonces, si la posición de las balas es [3,3] y la posición de los jugadores es [10,10], el vector de la dirección en la que debe viajar la pelota es [10-3,10-3]. Luego agrega procesalmente esos valores a la posición actual de las bolas. dividir los valores debería hacer que las bolas no se teletransporten instantáneamente.
Disculpe si me equivoco.