metalNumb Posted December 17, 2014 Share Posted December 17, 2014 Hey all ! So i've been trying to make my main.js file more compact by making a class for each of the game elements ( Player, moving platform,..) and now i want to do the same with my Enemies, only problem is that i used to generate dumb enemies at random locations and add them to a group in order to have group features ( like, killOutOfBounds ). Something like this...var enemies ;var enemyTimer;create:function(){enemies = this.add.group();enemies.enableBody = true;enemies.physicsBodyType = Phaser.Physics.ARCADE;enemies.setAll('outOfBoundsKill', true);enemies.setAll('checkWorldBounds', true);..........function spawnEnemy (){var enemy = enemies.create(this.world.width - 50,Math.random() * (this.world.height/2), 'enemy'); }......enemy.body.velocity.x = Math.random()*(-speedMax+speedMin) - speedMin ;}enemyTimer = this.game.time.create(false);enemyTimer.loop(1000, spawnEnemy, this); Now i want to do the same but the enemies generated needed to have some encapsulated AI in their own class.i.e i want to make a group of OBJECTS not SPRITES. How do i do that ? Thanks. Link to comment Share on other sites More sharing options...
rtlehr Posted December 17, 2014 Share Posted December 17, 2014 What I have started doing is extending the sprites. I followed the example below http://examples.phaser.io/_site/view_full.html?d=sprites&f=extending+sprite+demo+1.js&t=extending%20sprite%20demo%201 metalNumb 1 Link to comment Share on other sites More sharing options...
metalNumb Posted December 17, 2014 Author Share Posted December 17, 2014 Thanks a lot !Will definitely give it a try ! Link to comment Share on other sites More sharing options...
metalNumb Posted December 18, 2014 Author Share Posted December 18, 2014 I tried it, it gave no errors but the sprite did not show up ! Here's me Enemy class !/** * Created by mahmoud.ali on 12/17/2014. */function Enemy(game,spriteTag,x,y) { // Initialize Player Phaser.Sprite.call(this,game,x,y,spriteTag); // game.physics.arcade.enable(this); // Create Bullet group for Enemy}Enemy.prototype.constructor = Enemy;Enemy.prototype = Object.create(Phaser.Sprite.prototype);and here's my main state /** * Created by mahmoud.ali on 12/17/2014. */// ====== MAIN GAME VARIABLES ====={ var pixos; var ship; var enemies; // ENEMY GROUP var energyCapsules; // ENERGY_CAPS GROUP var cursors; // Capture arrow events var onPlatform = true; var floating = false; var enemyTimer; var energyTimer; var speedMax = 100; // SPAWNED Enemy X Speed MAX, MIN var speedMin = 50; myTrialGame.Game_Slim = function(game){};}myTrialGame.Game_Slim.prototype = { create:function(){ this.add.plugin(Phaser.Plugin.Debug); // INITIALIZE CONTROLLABLE ELEMENTS : THIS SHOULD NOT BE HARDWIRED !!! { ship = new Platform(this, 'platform', 0, 0); pixos = new Avatar(this, 'pixos', 0, 0); ship.avatarSprite.x = ship.avatarSprite.width / 2; ship.avatarSprite.y = this.world.height - 2 * ship.avatarSprite.height; pixos.avatarSprite.x = ship.avatarSprite.x + ship.avatarSprite.width / 2 - pixos.avatarSprite.width / 2; pixos.avatarSprite.y = ship.avatarSprite.y - pixos.avatarSprite.height; } // STATE WALKING INITIALIZE var enemy = new Enemy(this,'bullet',100,100); // SPAWN ENERGY CAPS // }, update:function(){ }};Thanks. Link to comment Share on other sites More sharing options...
rtlehr Posted December 18, 2014 Share Posted December 18, 2014 You need to addgame.add.existing(enemy);Aftervar enemy = new Enemy(this,'bullet',100,100);Reference the last two lines of the examples create function metalNumb 1 Link to comment Share on other sites More sharing options...
metalNumb Posted December 18, 2014 Author Share Posted December 18, 2014 Yes i figured that out, totally forgot it Thanks a lot though. I feel excited that my main game code is really getting more compact and my ideas are much more applicable now that my code is more OOP-like Ready for this weekend project ! So, if i am not mistaken, this is basically building a classical model over the prototypical one, right ? Link to comment Share on other sites More sharing options...
rtlehr Posted December 18, 2014 Share Posted December 18, 2014 Honestly, programing is not my number one skill set (I'm more of a graphics, video, 3d guy), so I have no idea. metalNumb 1 Link to comment Share on other sites More sharing options...
metalNumb Posted December 19, 2014 Author Share Posted December 19, 2014 Me neither, i am actually a telecom engineer !But this is so much fun ! Thanks again. Link to comment Share on other sites More sharing options...
Recommended Posts