Cutu

Adding property to a group?

Recommended Posts

Hey guys, this is my second post, this time I'm making a new game, it's a never ending side scroll game, but it scrolls in Y direction, I was following this Flappy Bird Tutorial and I made the barriers to come from Y direction, this is a pic of the game.

 

Spaceship.png

Those "Barrier blocks" are made with this code:

var barrierGroup;
var velocity = 250;
var barrierDelay = 1000;

function create() {
  barrierGroup = game.add.group();
    game.time.events.loop(barrierDelay,createBarrier);
}

function update() {
    spaceshipCollide();
    /*this is where I would use the scoreSystem function with barrierGroup property.*/
    barrierGroup.forEach(function(p) {
        scoreSystem(p);
    });
}

function createBarrier() {
    var hole = Math.floor(Math.random()*5)+1;
    for(var i=0;i<7;i++)
        if(i!=hole/* && i != hole +1*/)
            this.addOneBarrier(i*60,0, velocity);    
}

function addOneBarrier(x,y,velocity) {
    var barrier = game.add.sprite(x,y, 'barrier');    
    barrierGroup.add(barrier);
    game.physics.arcade.enable(barrier);
    barrier.body.velocity.y = velocity;
    barrier.checkWorldBounds = true;
    barrier.outOfBoundsKill = true;
    barrier.body.immovable = true;
    barrier.scored = false;
}

function scoreSystem(p) {
    if(p.exists && !p.scored && p.world.y >= spaceship.world.y) {
        p.scored = true;
        score++;
        scoreText.setText(score.toString());
    }
}

But the problem is that the score system counts everyblock in the row and add it to the score so you get 6 points (1 per block) and I want this to be only 1 per row. I think that could be possibly by adding the scored property to the group of blocks and set it to false as I did with every barrier. Is that possible? Thanks!  

 

Share this post


Link to post
Share on other sites

I think what you mean is you want to give the player one point for each collision. Is that what you mean? If so, then I would do it in the function that detects the collision. I think perhaps that is the function called spaceshipCollide(). Although you haven't shown that function, the name suggests that it is a good place to increment the score. But it's possible I've completely misunderstood your design and your intent, so forgive me if this is a useless answer.

As for the question, "Is that possible?" Yes, I promise you, whatever you're trying to do, there are a million ways to do it. With JavaScript, it's a million plus one.

Share this post


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

I think what you mean is you want to give the player one point for each collision. Is that what you mean? If so, then I would do it in the function that detects the collision. I think perhaps that is the function called spaceshipCollide(). Although you haven't shown that function, the name suggests that it is a good place to increment the score. But it's possible I've completely misunderstood your design and your intent, so forgive me if this is a useless answer.

As for the question, "Is that possible?" Yes, I promise you, whatever you're trying to do, there are a million ways to do it. With JavaScript, it's a million plus one.

Hey thanks for your answer, but that wasn't what I meant, but don't worry it's my fault. What I want is to increment the score by 1 each time the player (the spaceship) crosses the barrier row, I used this code:

function update() {
    //This function is the lose condition so ignore it
    spaceshipCollide();

    //Here is where I'm working with the score system and my main problem because this makes the game count each barrier in the row (because I'm using the forEach function.
    barrierGroup.forEach(function(p) {
        scoreSystem(p);
    });
}

function scoreSystem(p) {
    //If the barrier exists and his scored property is false and his Y position is greater or equal to the spaceship Y position I increment the score.
    if(p.exists && !p.scored && p.world.y >= spaceship.world.y) {
        p.scored = true;
        score++;
        scoreText.setText(score.toString());
    }



As you can see what I want is to give that scored property to the whole group of barriers so the score will be incremented by 1 and not 6 (the number of barriers in the row). In the first post is the code I used to make the barriers. Thanks for your help!

Edited by Cutu
Added the script type in Snippet

Share this post


Link to post
Share on other sites

Hi, Cutu.

The forEach loops through each block you add to the group. You may want to create one group per row of blocks, instead of one for all the blocks.

Then, in the update(), you can check each group, instead of each block.

 

Ps: A tip, avoid naming a variable as "p", it'll make your code easier to understand (for you, as well =D)

Share this post


Link to post
Share on other sites

Hello, as said before by GreatBigBore there are a lot of ways to do this and probably this is not the best one but this seems to fix your problem...

...

var barrierGroup;
var velocity = 250;
var barrierDelay = 1000;
var score=0;
//create this var to verify if your first barrier is created
var letsgo=false;

...

function update() {
//here you some test the first alive on your group, you dont need more...
if(letsgo)
        scoreSystem(barrierGroup.getFirstAlive());
}

...

function createBarrier() {
//ok, first barrier created, lets start!
    letsgo=true;

...
}

As Claudiovc said your problem is that you are looping through all blocks of you're group, adding score for everyone. You dont need that and in this case, your first alive is a good one to help on your test...

Share this post


Link to post
Share on other sites

Hi, thanks for answering guys, as Claudiovc said that's my problem and I know it, that's the point of the topic, but my question was if can I add the property "scored" or "letsgo" to my barrierGroup instead of every Barrier is created so I can do something like everytime a new group is made their "scored" property by default is false but when the row position is greater than the ship position add 1 to the score and set the "scored" property to true and repeat the loop? I have tried something like barrierGroup.position.y but doesn't work x.x

I tried your suggestion, but its not working how I want, thank you anyways :)

Share this post


Link to post
Share on other sites

Now I (think I) get it. Yes, you can add any property to Phaser.Group - just like you did with the barrier.scored. If you want, you can also extend Phaser.Group and add the "scored" property so every Barrier you create inherits it. Just remember that you need to check for "p.parent.scored" in your loop, or you won't be checking the Group's property.

Share this post


Link to post
Share on other sites

Thank you Claudio, I already did it, but now what I need is to check if the whole group Y position is greater than the spachip one. How can I do that without using the forEach method? When I use forEach it checks every block and add +1 to the score making a total of 6 per row. I need to add just 1 point, not 6.

Share this post


Link to post
Share on other sites

Hi Cutu, have you tried rufflezs' suggestion? Let us know if it works for you:

function update() {

    spaceshipCollide();

    // You don't need the 'forEach', since each block has the same 'y' coordinate
    // Just get the first one...if it passes your test, all the blocks in the group will =)
    scoreSystem(barrierGroup.getFirstAlive());

}

The point is that you don't need the forEach in your update loop, you just need one block.

Share this post


Link to post
Share on other sites
2 hours ago, Claudiovc said:

Hi Cutu, have you tried rufflezs' suggestion? Let us know if it works for you:


function update() {

    spaceshipCollide();

    // You don't need the 'forEach', since each block has the same 'y' coordinate
    // Just get the first one...if it passes your test, all the blocks in the group will =)
    scoreSystem(barrierGroup.getFirstAlive());

}

The point is that you don't need the forEach in your update loop, you just need one block.

I tried that but I get "cannot read property 'scored' of null" when the scoreSystem() is triggered. this is it:

function scoreSystem(barrier) {
    if(!barrier.scored && barrier.y >= spaceship.world.y) {
        barrier.scored = true;
        score += 1;
        Math.round(score, 0);
        scoreText.setText(score.toString());
    }
}

I hope you can help me plz.

Share this post


Link to post
Share on other sites

That is the reason for the "letsgo" variable on my example. At your code you're creating the first barrier after 1 second:

var barrierDelay = 1000;

If you dont verify if there is a barrier your update will test the colision before you have a barrier and that will result in your "cannot read property..." bug. You dont can test that if you dont have a barrier.

So, if you set the "letsgo" variable to true after the first barrier (on the begining of your function):

function createBarrier() {
//ok, first barrier created, lets start!
    letsgo=true;
...
}

Your loop can start after your barrier is created:

function update() {
if(letsgo)
        scoreSystem(barrierGroup.getFirstAlive());
}

avoiding this error.

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

  • Recently Browsing   0 members

    No registered users viewing this page.