Jump to content

Changing sprites into objects?


Ulbast
 Share

Recommended Posts

HI everyone, sorry for very noobish question, but I'm new in phaser and objective js :D

I'm making platform game, where I placed enemy like a sprite and programmed his movement. It works, but when I add another enemy, one is not moving, while the other is. I understand this, because sprites are not objects and all of my instructions can conduct only one, the latest sprite. How change the code, group or sth, can someone give me example, that I could add multiple sprites and all of them will be doing the same?

Here's the code:

//IN CREATE:

for (var i = 0; i < 20; i++)
	{
        droid = game.add.sprite(game.rnd.integerInRange(0, 128), game.world.randomY, 'droid');
		
    }
    
    
    
    game.physics.enable(droid, Phaser.Physics.ARCADE);

    droid.body.bounce.y = 0.1;
    droid.body.setSize(32, 32, 5, 1);

    droid.animations.add('left', [0, 1, 2], 10, true);
    droid.animations.add('right', [5, 6], 10, true);
    
    droid.body.collideWorldBounds = true;
        droid.previous_x=droid.body.x;
        droid.body.velocity.x = 50;

//IN UPDATE:
if( Math.abs(droid.body.x-droid.previous_x) > 100)
        enemy_switch_direction(droid);
    
    function enemy_switch_direction(enemy)
    {
       
        if ( enemy.body.velocity.x > 0 ) enemy.body.velocity.x=-50;
        else
        enemy.body.velocity.x=50;
    }

 

Link to comment
Share on other sites

Use a constructor function and instantiate new objects with "new" keyword. From the top of my head, unoptimized for real life usage but works as an example.

 

 

function Droid(x,y,texture){
var speed = 100;
this.obj = game.add.sprite(x,y,texture);

this.update = function(){
 this.obj.x+= DELTA_TIME() * speed;
 }

}

And use it like this in your create function

var droid1 = new Droid(100,200,"myTexture");
var droid2 = new Droid(0,0,"myOtherTexture");

And in update function update the two objects:

droid1.update();
droid2.update();

 

Things you can do to make it better:

1)Have a master droid holder object that will update and remove droids automatically so you will only have to call update only once. Then you can simply tell it to instantiate and update a droid on all itself.

2)Droid function should have a destroy method that will clean up you code and call destroy on the instantiated sprite (this.obj.destroy) 

Also you should be extremely aware of the "this" keyword and in what context you're calling it - basically you have to really understand JS scope - otherwise you'll have unexpected behaviours.

Link to comment
Share on other sites

Thanks for the reply, but I realised my knowledge is very low ;/

Here's the code:

function droid(x,y,texture){
this.obj = game.add.sprite(x,y,texture);
 
}

//CREATE:
var droid1 = new droid(100,200,"droid");
var droid2 = new droid(100,300,"droid");
    
    game.physics.enable(droid, Phaser.Physics.ARCADE);

    droid.body.bounce.y = 0.1;
    droid.body.setSize(32, 32, 5, 1);

    droid.animations.add('left', [0, 1, 2], 10, true);
    droid.animations.add('right', [5, 6], 10, true);
    
    droid.body.collideWorldBounds = true;
        droid.previous_x=droid.body.x;
        droid.body.velocity.x = 50;

And all the parameters above are not working. Console gives  me: "cannot read the property of bounce". How apply all of these affects to every droid? I tried to put these in droid objects like "this.body" but with no effect.

Link to comment
Share on other sites

Ok, so first of all you can't do game.physics.enable (droid, Phaser.Physics.ARCADE). Enable works on sprites only and droid it's a constructor function, not a sprite. droid1.obj and droid2.obj are sprites - and you should do game.physics.enable(droid1.obj, Phaser.Physics.ARCADE). 

Also you're trying to acces other properties on your droids (body, animations) but you're doing it wrong. droid its' a constructor function and it doesn't have body or animations properties. Your instances of droid - droid1 and droid2 - have an obj property (which is a sprite) and that obj property has body and animations properties. So you should do droid1.obj.body.bounce.y = 0.1. 

I reccomend that you improve your JS knowledge to at least a basic level before attempting to make games with it - otherwise you're in for a world of frustration. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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