Jump to content

Group and Text


Gammerr
 Share

Recommended Posts

How can I use this loop of Phaser examples and add.text to each one of the object that we created in the group?

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create});

 function preload() {

    game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');

}

var yourGroup;

function create() {

    //  Here we'll create a new Group
    yourGroup = game.add.group();

    //  And add 10 sprites to it
    for (var i = 0; i < 10; i++)
    {
        //  Create a new sprite at a random world location
        yourGroup.create(game.world.randomX, game.world.randomY, 'sonic');
    }

    //  Each sprite is now a member of yourGroup

}

 

Edit: I'm so close to the solution now, Common guys you've got to help me, not that much left here

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });

function preload() {

    game.load.image('sky', 'assets/sky.png');
    game.load.image('cloud1', 'assets/cloud.png');    
    game.load.image('pocket', 'assets/pocket.png');

}

var cursors;
var randomNumberX;
var cloud1;
var cloudGroup;
var pocket;
var score = 0;
var scoreText;
var text1;

function create() {

    game.physics.startSystem(Phaser.Physics.ARCADE);
    cursors = game.input.keyboard.createCursorKeys();

    game.add.image(0, 0, 'sky');    

    cloud1 = game.add.sprite(0, -400, 'cloud1');
    pocket = game.add.sprite(568, 568, 'pocket');

    game.physics.arcade.enable([cloud1, pocket]);

    var cloudTextStyle = { font: "bold 16px Arial", fill: "#000",  align: "center" };

    cloudGroup = game.add.group();
    for (var i = 0; i < 2; i++) {
        randomNumberX = Math.floor(Math.random() * 580) + 1;
        //  Create a new sprite at a random world location
        cloudGroup.create(randomNumberX, i * (-300), 'cloud1');        
    }    

    cloudGroup.forEach(function(item){
        game.physics.arcade.enable(item);
        item.body.velocity.y = 80;
        item.anchor.set(0);
    });

    
    // game.physics.arcade.gravity.y = 200;
    // cloud1.body.bounce.y = 0.95;

    pocket.body.collideWorldBounds = true;

    pocket.body.allowGravity = false;
    pocket.body.immovable = true;

    // The score
    scoreText = game.add.text(16, 16, 'score: 0', { fontize: '32px', fill: '#fff' });

    text1 = game.add.text(0, 0, "Organization", cloudTextStyle, cloudGroup);
    text1.anchor.set(0.5);    

}


function update() {

    cloudGroup.forEach(function(item){
        game.physics.arcade.overlap(item, pocket, collisionHandler, null, this);        
    });

    pocket.body.velocity.x = 0;

    // Movement
    if (cursors.left.isDown) {
        //  Move to the left
        pocket.body.velocity.x = -350;
    } else if (cursors.right.isDown) {
        //  Move to the right
        pocket.body.velocity.x = +350;
    } else {
        pocket.animations.stop();
    }

    // Text
    cloudGroup.forEach(function(item){

        item.alpha = 0.85;        
        text1.x = Math.floor(item.x + item.width / 2);
        text1.y = Math.floor(item.y + item.height / 2);
        
    });

}

function collisionHandler (cloud1, pocket) {

    cloud1.destroy();    
    text1.destroy();    
    
    score += 10;
    scoreText.text = 'Score: ' + score;

}

 

Link to comment
Share on other sites

//  And add 10 sprites to it
for (var i = 0; i < 10; i++)
{
    //  Create a new sprite at a random world location
    var sprite = yourGroup.create(game.world.randomX, game.world.randomY, 'sonic');

    sprite.addChild(game.make.text(0, 0, 'sprite' + i)); // etc.
}

 

Link to comment
Share on other sites

OK, I tried that
 

cloudGroup = game.add.group();
    for (var i = 0; i < 2; i++) {
        randomNumberX = Math.floor(Math.random() * 580) + 1;
        //  Create a new sprite at a random world location
        cloudGroup.create(randomNumberX, i * (-300), 'cloud1');  
        cloudGroup.addChild(game.make.text(0, 0, 'sprite' + i));      
    }   


and I'm getting an error

Uncaught TypeError: Cannot read property 'velocity' of undefined

Link to comment
Share on other sites

cloudGroup = game.add.group();

for (var i = 0; i < 2; i++) {
  randomNumberX = Math.floor(Math.random() * 580) + 1;
  //  Create a new sprite at a random world location
  var cloud = cloudGroup.create(randomNumberX, i * (-300), 'cloud1');
  cloud.addChild(game.make.text(0, 0, 'cloud' + i));
}

 

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