Jump to content

onOutOfBounds


Matt
 Share

Recommended Posts

Hi everyone,

 

I'm new to phaser and to this forum :)

google couldn't find me an answer so i had to make an account to post.

 

I am having an issue with onOutOfBounds being triggered when a sprite is entering.

This happens at random and tends to happen several times to the one sprite in a row

 

just wondering what could cause this and how exactly does onOutOfBounds work?

Link to comment
Share on other sites

Hey, thanks for the reply,

I did already understand that much about the event,

still not sure why the event is being triggered several times in one frame.

 

here is the code that i'm using;
I may have altered it a little by now but any feedback will help

 

//spawns enemies on a timer
enemiesCtrl: function(){
//need to edit this to do something different down the track
   if(this.enemyTimer >= 50 && this.enemyCounter <= 15){
       this.enemyTimer = 0;
       this.enemySpawner();
       this.enemyCounter ++;
   }else{
       this.enemyTimer ++;
   }
},
//creates new enemy within enemies group
enemySpawner: function(){
        var enemy = this.enemies.create(-30, -30, 'red');
        enemy.scale.setTo(0.5, 0.5);
        enemy.checkWorldBounds = true;
enemy.events.onOutOfBounds.add(this.enemyOob, this);
        this.enemyReset(enemy);
    },
//repositions the enemy and resets direction
enemyReset: function(enemy){
var side = Math.floor((Math.random()*2)+1);
  if(side==1){
        enemy.x = -30;
        enemy.y = Math.floor((Math.random() * 800) + 1);
 
        enemy.body.velocity.x = 100;
  }else{
        enemy.x = 800;
        enemy.y = Math.floor((Math.random() * 800) + 1);
 
        enemy.body.velocity.x = -100;
  }
},
//triggers reset function when enemy leaves the screen
enemyOob: function(enemy){
if((enemy.y >750 && enemy.body.velocity > 0)||(enemy.y <50 && enemy.body.velocity < 0)){
this.enemyReset(enemy);;
}
}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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