SmugBaldy Posted July 28, 2017 Share Posted July 28, 2017 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 Quote Link to comment Share on other sites More sharing options...
SmugBaldy Posted July 29, 2017 Author Share Posted July 29, 2017 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)); } } Quote Link to comment Share on other sites More sharing options...
Parasyte Posted August 2, 2017 Share Posted August 2, 2017 Nice! This is something we should definitely include in the tutorial. I have added an issue to track this: https://github.com/melonjs/tutorial-space-invaders/issues/2 If you would like to contribute to the tutorial, we would be glad to accept. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.