Jump to content

Sprites in Group X position


Heppell08
 Share

Recommended Posts

So I've editted the full post with code and a better description of what i mean. I have a group of enemies, they all walk towards the player in 1 direction, to the left. They collide with the player and a function to hurt player is called. It all works perfectly. My issue is that i need the X position of sprites in the group to better code the AI and movement. For example, if an enemy in the group gets with 45 X of the player, increase speed a little or animate it more etc etc. 

I have everything in place that i need, its just the X position of the enemies in relation to the player. I have the group parent set to the player for spawning in on the X/Y when game starts but there's where the relationship between enemies and player end.

Heres some code if it helps:

enemies = game.add.group(player);    for(var i = 0; i < NumofBots; i++)    {        var enemybot = enemies.create(this.x,this.y,'robot1');                this.x = game.world.randomX + game.rnd.integerInRange(100,250);        this.y = game.world.height - 63;        this.maxSpeed = 40;        this.minSpeed = -40;        var rndSpeedY = Math.random()*(this.maxSpeed - this.minSpeed+1)-this.minSpeed;        var rndSpeedX = Math.random()-(this.maxSpeed - this.minSpeed+1)-this.minSpeed;        enemybot.animations.add('walk');        enemybot.animations.play('walk', 10, true);        enemybot.anchor.setTo(0.5, 1);        enemybot.body.velocity.x = rndSpeedX;        enemybot.body.velocity.y = rndSpeedY;        enemybot.body.collideWorldBounds = true;        enemybot.body.gravity.y = 9.6;        }
Link to comment
Share on other sites

Hey Heppell08,

 

Is this what you are looking for?

    var i;    console.log('begin info');    for (i = 0; i < enemies.length; i++) {      console.log('y Position of Object ' + i + ' in group enemies: ' + enemies.getAt(i).body.y);    }    console.log('endinfo');
Link to comment
Share on other sites

 

Hey Heppell08,

 

Is this what you are looking for?

    var i;    console.log('begin info');    for (i = 0; i < enemies.length; i++) {      console.log('y Position of Object ' + i + ' in group enemies: ' + enemies.getAt(i).body.y);    }    console.log('endinfo');

yeah this one:

var i;    console.log('begin info');    for (i = 0; i < enemies.length; i++) {      console.log('x Position of Object ' + i + ' in group enemies: ' + enemies.getAt(i).body.x);    }    console.log('endinfo');

just changed it to the X but seems like its telling me the X of each enemy. This is super useful to me Thanks!!

Link to comment
Share on other sites

  • 2 years later...
 Share

  • Recently Browsing   0 members

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