Jump to content

Random collisions for seemingly no reason


Recommended Posts



I'm a fairly decent PHP coder but have never really delved into Javascript before until I came across Phaser and had to give it a try (it's the most appealing game engine I've ever come across, it got me to write my first game!) so I've created a fairly basic Flappy Birds-style game that allows the main sprite to jump before falling and trying to avoid enemies that are coming in from the right.


Because I've never attempted any massive Javascript undertaking before, especially game-making, I've encountered a few problems but have generally been able to fix them.


However, for some reason, I've been having problems that seem to be coming from an overlap event. At random points in the game (sometimes when the player seemingly jumps too high), it will trigger the end_game_hit() function and end the game. This is frustrating because it seems to come from nowhere and the game keeps restarting. It works fine for when they actually hit but the random hits seem to come from nowhere and I can't figure out where.


I've got this set up for the collision event in update():

this.game.physics.overlap(this.main, this.enemies, this.end_game_hit, null, this);

And these are the other functions. (To avoid confusion, each time there's a collision, it chooses a random number and plays the appropriate sound file. This all works fine.)

    end_game_hit: function() {        console.log('Hit an enemy.');        this.kill_sound_no = random_number(5);        this.kill_sound[this.kill_sound_no].play();        this.end_game();    },    end_game: function() {        this.game.time.events.remove(this.timer);        this.game.time.events.remove(this.sound_timer);        this.game.score = this.score;        if(this.game.score > readCookie('highscore')) {            createCookie('highscore',this.game.score,30);        }        this.game.state.start('main');    }

It's probably something I've overlooked but I'd appreciate any help! Thanks in advance.

Link to comment
Share on other sites

Well, it's hard to say based on that code and i am assuming that "this.main" is the player.

There might be an invisible enemy somewhere or you are jumping to high beyond the screen and there is some enemy positioned there.


I think we would need to see how you are populating the enemies group/array

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...