Jump to content

Space Invaders Tutorial - Adding Enemy Lasers - Initial Position


Recommended Posts

First off - I'm having a lot of fun playing with melonJS. Great work on the library.

I've just gone through the Space Invaders tutorial, and have worked through some of the challenges (for example, adding a HUD, adding waves).

I'm now looking at adding enemy lasers that would be spawned from the enemy entities, and collide with the player. The obvious idea is that enemies should be able to shoot the player, and if the player is hit, then I'll decrement the number of lives. 

I've got the basic Enemy_Laser entity coded, and it appears to work, except for the initial position.  When the player moves, and you hit the space bar, the player's Laser will fire from the player's current player position.  The enemy laser, however, spawns from the initial position of the enemy entity objects rather than their current position. I suspect this is due to the enemy entities being wrapped within the enemy_manager container.

Do I need to update the position of the enemy entities within the enemy_manager? I see there's a setChildsProperty() method in the container object. Can/should this be used to update the position of the enemy_manager child entities?

You can view the source code here: https://github.com/michael-peacock/cosmictrespassers/tree/develop/src/main/resources/static/js


Link to comment
Share on other sites

A I figured this out after cracking open the source for the me.Container class.  It turns out that the container's update method will update the absolute position of renderable child objects.  With that, I was able to update my enemy laser to spawn with:


me.game.world.addChild(me.pool.pull("enemyLaser", theChild._absPos.x + game.EnemyLaser.width, theChild._absPos.y + game.EnemyLaser.height));


In addition, I moved this logic from the enemy update call into the enemy_manager's (aka - the container's) update call. The full change was:

if (this.children.length > 0 && this.createdEnemies) {
    // pick a random child
    var childIndex = common.functions.getRandomInt(0,this.children.length -1);
    var theChild = this.getChildAt(childIndex);

    // pick a random number between 1 & 1000
    var randomInt = common.functions.getRandomInt(1,1000);

    // still tweaking this to get the right amount of enemy fire
    if (randomInt > 900) {
	me.game.world.addChild(me.pool.pull("enemyLaser", theChild._absPos.x + game.EnemyLaser.width, theChild._absPos.y + game.EnemyLaser.height));


Link to comment
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.

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.


  • Recently Browsing   0 members

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