erich

is this the correct way to add body to multible sprites

Recommended Posts

Hi everyone,

quick question,

I googled that you can't add velecity to a group so I wrote this script and it works fine, but am I pulling to many resourses on the cpu if I create mulitble enemies at once on the screen, at present I have 2 enemies but will add more

in laymans terms I have an external json file for the enemy x,y, key, velocity etc.., I then push each enemy into an empty array, and use a for loop in the update function to check collision against a wall and reverse the velocity

here's the 2 parts I wrote

in the create function :

  //create an empty array
    this.en = [];
    for (var i = 0; i <this.levelData.enemyData.length; i ++){
      // add sprite to stage
      this.enemy = this.add.sprite(this.levelData.enemyData.x, this.levelData.enemyData.y, this.levelData.enemyData.key);
      //enable physics
      this.game.physics.arcade.enable(this.enemy);
      //give it a speeed for x
      this.enemy.body.velocity.x = this.levelData.enemyData.vel;
      this.enemy.scale.setTo(0.5);
      this.enemy.anchor.setTo(0.5);
      //push enemy into the array
      this.en.push(this.enemy);
    };

in the update function

  // enemy stuff
    for (var i=0; i < this.en.length; i++){
        //hits wall
        this.game.physics.arcade.collide(this.en,  this.collisionLayer, function(){

          var hitRight = this.en.body.blocked.right;
          var hitLeft = this.en.body.blocked.left;

           if (hitRight){
             this.en.body.velocity.x = - this.levelData.enemyData.vel;
             this.en.scale.setTo(-0.5,0.5);
           }
           if (hitLeft){
             this.en.body.velocity.x = this.levelData.enemyData.vel;
             this.en.scale.setTo(0.5, 0.5);
           }

        }, null, this);
        // player hits enenmy
        this.game.physics.arcade.overlap(this.player, this.en, this.hitEnemy);
    }

am I right or is there a better way to do it ?

also on a seconadry note I originally used this in the update function

this.game.physics.arcade.collide(this.en,  this.collisionLayer, this.reverseEn, null, this);

reverseEn : function(enemy, layer){
   var hitRight = enemy.body.blocked.right;
   var hitLeft = enemy.body.blocked.left;
   var speed = enemy.body.velocity.x;

    if (hitRight){
      enemy.body.velocity.x = -30;
      enemy.scale.setTo(-0.5,0.5);
//      console.log('hit right wall at ' + speed);
    }

    if (hitLeft){
      enemy.body.velocity.x = 30;
      enemy.scale.setTo(0.5, 0.5);
  //    console.log('hit left wall at ' + speed);
    }
 },

but speed is set at 0 and not at the this.en.bodyvelocity.x eg 30 - why is that ?

hope it makes sense - thanks in advance

Eric

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.