Jump to content

Creating a group of smart objects ?


metalNumb
 Share

Recommended Posts

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

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

Yes i figured that out, totally forgot it  :huh:

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 !  B)

 

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

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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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