+ 14

Game Dev Support

I have 2 issues with my code at the moment. 1. How can I make the button re-pressable. It only works once 2. How can I access enemies[i] and bullets[i] in the same function. In order for me to code collision I need to loop through BOTH arrays and program it with an index of [i]. I have tried nested loops but it doesn't work. In other words. How can I implement collision between the bullets and the enemy ships Those 2 issues are the ones I've saved till last because I have no idea what to do. Does anyone know how to do these? https://code.sololearn.com/WtAirtYvU5tg/?ref=app

21st Jun 2019, 8:21 PM
Clueless Coder
Clueless Coder - avatar
12 ответов
+ 14
The buttons in the screen I can press multiple times to shoot out dots that are orange. Are you trying to move the shooter instead? EDIT: I got to go, but I can do the buttons tommorow. The buttons don't work at all yet, its just design. EDIT: Dang it. Someone beat me to it. Well, I hope you found what you needed. Good luck!
21st Jun 2019, 8:35 PM
Green Ghost
Green Ghost - avatar
+ 7
The button doesn't work. It can't even be pressed once. You could just use <button> tags instead, and label them with arrows.
21st Jun 2019, 8:48 PM
ᗰᗩᔕ丅ᗴᖇ
ᗰᗩᔕ丅ᗴᖇ - avatar
+ 4
Anton Böhler The buttons won't respond at all anymore unfortunately.
22nd Jun 2019, 6:54 AM
Clueless Coder
Clueless Coder - avatar
22nd Jun 2019, 6:55 PM
Anton Böhler
Anton Böhler - avatar
+ 3
part1: let bullets = []; let enemies = []; class Bullet{ constructor(x, y, w, h, ySpeed){ this.x = x; this.y = y; this.w = w; this.h = h; this.r = this.w/2; this.ySpeed = ySpeed; this.d; } show(){ fill("orange"); ellipse(this.x, this.y, this.w, this.h); //console.log("hit") } move(){ this.y -= this.ySpeed; } } class Ship{ constructor(x, y, w, h, xSpeed, ySpeed, col){ this.x = x this.y = y; this.w = w; this.h = h; this.xSpeed = 0; this.ySpeed = 0; this.maxXSpeed = xSpeed; this.maxYSpeed = ySpeed; this.r = this.w / 2; } show(){ fill("white"); rect(this.x, this.y, this.w, this.h); } move(){ this.x += this.xSpeed; this.y += this.ySpeed; } } class Enemy{ constructor(x, y, w, h){ this.x = x; this.y = y; this.w = w; this.h = h; } show(){ fill("red"); ellipse(this.x, this.y, this.w, this.h); //console.log("test"); } intersectsBullet(bullet){ return Math.sqrt(Math.pow(this.x - bullet.x, 2) + Math.pow(this.y - bullet.y, 2)) < this.w/2 + bullet.r; } } function setup(){ createCanvas(335, 390); rectMode(CENTER); angleMode(DEGREES); player = new Ship(width/2, 375, 10, 30, 5, 5, 0); //buttonL = createButton("<").id("btnL"); //buttonL.touchStarted(left) //buttonL.touchEnded(leftEnded); //buttonR = createButton(">").id("btnR"); //buttonR.touchStarted(right); //buttonR.touchEnded(rightEnded); for (let j = 0; j < 5; j++){ enemies.push(new Enemy(j * 70, 30, 30, 30)); enemies[j].show(); } }
21st Jun 2019, 9:06 PM
Anton Böhler
Anton Böhler - avatar
+ 3
part2: function left(){ //console.log("left"); player.xSpeed = -player.maxXSpeed; } function right(){ //console.log("right"); player.xSpeed = player.maxXSpeed; } function stop(){ //console.log("stop"); player.xSpeed = 0; } function mouseClicked(){ bullets.push(new Bullet(player.x, player.y, 10, 10, 4)); } function bulletCollide(){ //console.log() for(var i=enemies.length-1;i>=0;i--){ for(var j=bullets.length-1;j>=0;j--){ if(enemies[i].intersectsBullet(bullets[j])){ console.log(enemies.length) enemies.splice(i, 1); bullets.splice(j, 1); } } } } function draw(){ background(0); player.show(); player.move(); for (let i in bullets){ bullets[i].show(); bullets[i].move(); } for(let i in enemies){ enemies[i].show(); } bulletCollide(); /* for (let j = 0; j < 5; j++){ enemies.push(new Enemy(j * 70, 30, 30, 30)); enemies[j].show(); } */ }
21st Jun 2019, 9:07 PM
Anton Böhler
Anton Böhler - avatar
+ 3
html: <!DOCTYPE html> <html> <head> <title>Page Title</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/p5.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/addons/p5.dom.min.js"></script> </head> <body onCopy="return false" onpaste="return false" onselectstart="return false" onDrag="return false"> <button onmousedown="left()" onmouseup="stop()" ontouchend="stop()"><</button> <button onmousedown="right()" onmouseup="stop()" ontouchend="stop()">></button> </body> </html>
21st Jun 2019, 9:07 PM
Anton Böhler
Anton Böhler - avatar
+ 2
Coder It still isn't working. I had some saving issues so I might have missed something important. It has no response what so ever. edit: Yes I found the error, I will replace the move() method
22nd Jun 2019, 9:39 AM
Clueless Coder
Clueless Coder - avatar
+ 2
Coder Sorry, this is confusing me. One moment it's there, the next moment it's not. The whole code is glitching out for me. Don't know if it's network or something
22nd Jun 2019, 9:41 AM
Clueless Coder
Clueless Coder - avatar
+ 2
Coder Well, I've done something. It kinda works.
22nd Jun 2019, 9:53 AM
Clueless Coder
Clueless Coder - avatar
+ 2
Coder I have successfully completed collision! 😂🎊 All that's left is the buttons. I'll pvt message you later as I now need to do something. Thank you and happy coding!
22nd Jun 2019, 10:01 AM
Clueless Coder
Clueless Coder - avatar
+ 2
Felix Cuevas About 3 maybe 4 hours
22nd Jun 2019, 2:47 PM
Clueless Coder
Clueless Coder - avatar