lecture 3 Flashcards
(6 cards)
intersects(other) {
let d = dist(this.xPos, this.yPos, other.xPos, other.yPos);
if (d < this.radius + other.radius) {
return true;
} else {
return false;
}
}
what is the above code doing?
checking collision based on radius
if (missile1.intersects(enemy1)) {
enemy1.colour = ‘black’;
}
what will occur when the above code is called?
if a missile intersects with an enemy the colour of the enemy will be black
for (let i = 0; i < missiles.length; i++) {
for (let j = 0; j < numOfEnemies; j++) {
if (missiles[i].intersects(enemies[j]) || missiles[i].xPos > width) {
enemies[j].colour = ‘black’;
missiles.splice(i, 1);
What single piece of code would you add after splice to prevent accessing deleted missiles in a loop?
break;
for an enemy is it better to A. delete and create a new enemy or B. make the enemy invisible and reuse it after its traversed the screen?
B
for a missile is it better to reuse or recreate?
recreate as it is dynamic to the player and they are no longer required. e.g. after a collision or if they have traversed the screen (without a collision).
what would happen if you didn’t delete the missile after it traverses your screen?
it will continue moving and increase memory usage in your application.