Heppell08 Posted January 18, 2014 Share Posted January 18, 2014 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 More sharing options...
Heppell08 Posted January 18, 2014 Author Share Posted January 18, 2014 Bump* Link to comment Share on other sites More sharing options...
Heppell08 Posted January 19, 2014 Author Share Posted January 19, 2014 bump v2.0 Link to comment Share on other sites More sharing options...
jpdev Posted January 19, 2014 Share Posted January 19, 2014 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 More sharing options...
Heppell08 Posted January 19, 2014 Author Share Posted January 19, 2014 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 More sharing options...
Heppell08 Posted January 19, 2014 Author Share Posted January 19, 2014 Last question based on the answer to this. How would I go about using that in the update loop to code in how far in relation to the player the enemy X is etc? Just need to try get that working and I should be fairly done from an AI perspective. Link to comment Share on other sites More sharing options...
Heppell08 Posted January 19, 2014 Author Share Posted January 19, 2014 var i; for (i = 0; i < enemies.length; i++) { console.log(enemies.getAt(i).body.x); if(enemies.getAt(i).body.x >= player.body.x) { console.log('This works'); } }Yeah i got it working with a console log to show me if it was working for modding the X and setting a function or velocity. Thanks Link to comment Share on other sites More sharing options...
EmmaLV Posted July 27, 2016 Share Posted July 27, 2016 can you help me please , i did it and return me null Link to comment Share on other sites More sharing options...
Recommended Posts