Jump to content

overlap issue with group and object


erich
 Share

Recommended Posts

Hi All

Quick question, I am scratching my head on this for the last day - what am I doing wrong ?

I have created a basic car driving on a highway and to avoid the cars. I hae created an enemies grou fornthe car and my car is a player object

http://html5gamer.mobi/phaser2/carChase/index.html

sample of script :

in create I have :

  //player
    this.player = this.add.sprite(this.game.world.centerX, this.game.world.height - 200, 'player');
    this.player.anchor.setTo(0.5);
    this.game.physics.arcade.enable(this.player);
    this.player.body.collideWorldBounds = true;  
    this.player.customParams = {};

this.initEnemies();
this.scheduleEnemies = this.game.time.events.loop(Phaser.Timer.SECOND * 1, this.initEnemies, this);

in update I have :

// kill enemies when out of screen
if (this.enemies.y > this.game.height){
        this.enemies.kill();
 }

//player and enemy hit
this.game.physics.arcade.overlap(this.player, this.enemies, this.killPlayer, null, this);
 

rest of the functions :

 initEnemies: function(){
 
    this.enemies = this.add.group();
    this.enemies.enableBody = true;
    
    this.createEnemy();
  },
  createEnemy: function(){
  // create a random lane for the cars to drive down - 4 lanes
  var carRandom = Math.floor((Math.random() * 4) + 1); ;
 
    if (carRandom == 1){
      //console.log('1st lane');
      this.laneX = 50;
      this.laneY = -150;
      this.vel = 400;
      } else if (carRandom == 2){
      //console.log('2nd lane');
      this.laneX = 220;
      this.laneY = -150;
      this.vel = 400;
    } else if (carRandom == 3){
      //console.log('3rd lane');
      this.laneX = 500;
      this.laneY = -150;
      this.vel = 900;
      } else if (carRandom == 4){
      //console.log('4th lane');
    this.laneX = 660;
      this.laneY = -150;
      this.vel = 900;
      }
      
      // creat random cars
      var key = Math.floor((Math.random() * 4) + 1); ;
      
    // create enemy if one exists in the enemies group
    var enemy = this.enemies.getFirstExists(false);
    if (!enemy){
        enemy = this.add.sprite(this.laneX,this.laneY, 'car' + key);
    } else {
        enemy.reset(this.laneX,this.laneY, 'car' + key);
    }
    // make the cars move towards the bottom
    enemy.body.velocity.y = this.vel;
    
    // add enemy to enemies group
     this.enemies.add(enemy);
     
    if (carRandom > 2){
    // reverse the car image in the last 2 lanes     
    enemy.scale.setTo(-1);
    }
    
   },

 killPlayer: function(player, enemy) {
    cosnole.log('HIT !');
    this.player.kill();
    this.game.state.start('GameState');
  },
 

Thanks for any help in adavnce

Eric

Link to comment
Share on other sites

I found my error - it was in

this.scheduleEnemies = this.game.time.events.loop(Phaser.Timer.SECOND * 1, this.initEnemies, this);

it should be

this.scheduleEnemies = this.game.time.events.loop(Phaser.Timer.SECOND * 1, this.createEnemy, this);

using the first timer kept on creating a group every second hence causing no collision

hope this helps someone esle that may have an issue with a similar

Eric

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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