Giter Club home page Giter Club logo

Comments (6)

straker avatar straker commented on June 5, 2024

My guess is that you're performing the splice inside the update enemies loop? If you splice during that loop, you'll miss updating one of the enemies in the list. That might be causing the flicker.

enemies = [0,1,2]

for (var i = 0; i < enemies.length; i++) {
  console.log(enemies[i]);   

  if (enemies[i] === 0) enemies.splice(i,1);
}

// => output will be "0" "2"

from endless-runner-html5-game.

dsyenket avatar dsyenket commented on June 5, 2024

Any idea on how to make the enemy disappear when the player hits the enemy but will not cause it to flicker?

from endless-runner-html5-game.

straker avatar straker commented on June 5, 2024

If that's the cause of the flicker, you can manually increment i when you don't splice instead of letting the loop do it.

for (var i = 0; i < enemies.length; ) {
  console.log(enemies[i]);

  if (enemies[i] === 0) enemies.splice(i,1);
  else i++;
}

from endless-runner-html5-game.

dsyenket avatar dsyenket commented on June 5, 2024

This is the function that was used to update enemies position and draw.
function updateEnemies() {
// animate enemies
for (var i = 0; i < enemies.length; i++) {
enemies[i].update();
enemies[i].draw();

// player ran into enemy
if (player.minDist(enemies[i]) <= player.width - platformWidth/2) {
  //gameOver();
  if (enemies[i] === 0)
{
	enemies.splice(i, 1);
}
else
{
	i++;
}
}

}

// remove enemies that have gone off screen
if (enemies[0] && enemies[0].x < -platformWidth) {
enemies.splice(0, 1);
}
}

It's not removing the enemy as it should.

from endless-runner-html5-game.

straker avatar straker commented on June 5, 2024

remove the if (enemies[i] === 0) and just leave the enemies.splice(i,1).

from endless-runner-html5-game.

dsyenket avatar dsyenket commented on June 5, 2024

Thank you very much, it works.

from endless-runner-html5-game.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.