Sign in to follow this  
LuisM

A hint!

Recommended Posts



I'm making a game with some collectible objects.

I created a group for each type of object so I know what type is the object I collected.
but I'm in need of a hint how I can arrange this!
I already have 6 groups. and each group has 4 to 7 items that are scattered throughout the scene.
would you have a better way to do that?

 

thanks!

Share this post


Link to post
Share on other sites
16 hours ago, onlycape said:

Hi @LuisM ,

You can add a custom property to the elements of a group.

Example: sprite.grouptype = 'type1';

 

Thank you. @onlycape

I'm doing something I need to collect items

and did not want to make a group for each item plus a single clip and put different types inside but give up! I'll let each group be the type of items so it gets better.

about your tip I do not know where I would use this my knowledge of javascript is low.

but that's how my group looks.

gStones= game.add.group();
    game.physics.p2.enable(gStones);
    gStones.enableBody = true;
    gStones.physicsBodyType = Phaser.Physics.P2JS;
    gStones.create(320, 510, 'stone1').scale.setTo(0.7);
    gStones.create(520, 710, 'stone2').scale.setTo(0.7);
    gStones.create(720, 310, 'stone3').scale.setTo(0.7);
    gStones.setAll('body.kinematic', true);


just in case I need to give types to each item in a group can I explain where and how to use it?

Share this post


Link to post
Share on other sites
5 hours ago, LuisM said:

Thank you. @onlycape

I'm doing something I need to collect items

and did not want to make a group for each item plus a single clip and put different types inside but give up! I'll let each group be the type of items so it gets better.

about your tip I do not know where I would use this my knowledge of javascript is low.

but that's how my group looks.

gStones= game.add.group();
    game.physics.p2.enable(gStones);
    gStones.enableBody = true;
    gStones.physicsBodyType = Phaser.Physics.P2JS;
    gStones.create(320, 510, 'stone1').scale.setTo(0.7);
    gStones.create(520, 710, 'stone2').scale.setTo(0.7);
    gStones.create(720, 310, 'stone3').scale.setTo(0.7);
    gStones.setAll('body.kinematic', true);


just in case I need to give types to each item in a group can I explain where and how to use it?

Thanks! It's useful

Share this post


Link to post
Share on other sites

Hi @LuisM ,

With this code I have created a new property for the group elements named customProperty (test code in phaser sandbox here ) :


function create() {

    var sprite = game.add.sprite(0, 0, 'phaser');
    
    
    var gStones= game.add.group();
    
    // Adding elements using a variable named "element"
    var element = gStones.create(320, 510, 'phaser');
    element.scale.setTo(0.7);
    //Here I create the new property "customProperty"
    element.customProperty = 'type1';

    // Reusing variable "element" I create other element 
    element = gStones.create(520, 710, 'phaser');
    element.scale.setTo(0.7);
    element.customProperty='type2';

    //And one more time
    element = gStones.create(720, 310, 'phaser');
    element.scale.setTo(0.7);
    element.customProperty = 'type3';
    
    //Testing customProperty with forEach (this method iterates each group's element)
    gStones.forEach(function(item){
        console.log(item.customProperty);
    });

}

I hope it helps you..

Suerte !!

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.