Sign in to follow this  
SmugBaldy

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

 

Share this post


Link to post
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));
    }	
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.