Sign in to follow this  
Xron

Sprite class - drawing multiple sprites

Recommended Posts

use this map editor to create a tilemap

www.mapeditor.org

 

then use tilemap feature from phaser..

http://phaser.io/examples/v2/category/tilemaps

I mean a number of game sprites not tiles, such as players/npcs, instead of drawing each one at a position with the key, looking for an example of how this could be done. If not then is using groups best?

 

Edit: Posted from a member of the team ;) of the op

Share this post


Link to post
Share on other sites

var self = this;

var sprites = [{x:15,y:10,key:"image1"},{x:15,y:10,key:"image2"},{x:15,y:10,key:"image3"}]

sprites.forEach(function(el){self.add.sprite(el.x,el.y,el.key)})

 

or

 

var groupObj = this.add.group();

var self = this;

var sprites = [{x:15,y:10,key:"image1"},{x:15,y:10,key:"image2"},{x:15,y:10,key:"image3"}]

sprites.forEach(function(el){groupObj.add(self.add.sprite(el.x,el.y,el.key))})

 

it can help

Share this post


Link to post
Share on other sites

I mean a number of game sprites not tiles, such as players/npcs, instead of drawing each one at a position with the key, looking for an example of how this could be done. If not then is using groups best?

 

Edit: Posted from a member of the team ;) of the op

 

oh, sorry I've got the question wrong..

 

san40511's answer is pretty good..

Share this post


Link to post
Share on other sites

var self = this;

var sprites = [{x:15,y:10,key:"image1"},{x:15,y:10,key:"image2"},{x:15,y:10,key:"image3"}]

sprites.forEach(function(el){self.add.sprite(el.x,el.y,el.key)})

 

or

 

var groupObj = this.add.group();

var self = this;

var sprites = [{x:15,y:10,key:"image1"},{x:15,y:10,key:"image2"},{x:15,y:10,key:"image3"}]

sprites.forEach(function(el){groupObj.add(self.add.sprite(el.x,el.y,el.key))})

 

it can help

 

Hi, thanks so much for the demo I really like that second option, could you explain the use of "el" in the forEach

Share this post


Link to post
Share on other sites

Hi, thanks so much for the demo I really like that second option, could you explain the use of "el" in the forEach

 

it is the same as this I guess

 

for(var i=0;i<sprites.length;i++){

     var el = sprites;

     ....

     ....

}

Share this post


Link to post
Share on other sites

The most professional way to do this is this:

var builder = {    rectHouse: function(x, y, width, height) {        var sprite = game.add.sprite(x, y, "rectangle_house");        sprite.x = x; sprite.y = y;        sprite.anchor.setTo(0.5, 0.5);        game.physics.arcade.enable(sprite);        sprite.body.setSize(width, height, 0, 0);        sprite.body.immovable = true;        return builder; //important    },    circleHouse: function(x, y, r) {        var sprite = game.add.sprite(x, y, "circle_house");        sprite.x = x; sprite.y = y;        sprite.anchor.setTo(0.5, 0.5);        game.physics.arcade.enable(sprite);        sprite.body.setSize(r, r, 0, 0);        sprite.body.immovable = true;        // optional: phaser_sprite_group.add.(sprite); (add this to a group)        return builder; //important    }}builder.rectHouse(0, 0, 100, 100).rectHouse(200, 0, 100, 100).rectHouse(300, 0, 100, 100).circleHouse(400, 500, 50).circleHouse(600, 400, 50);

A better way is "builder" to be a class instead of an object. If you want I can show you how to create classes.

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.