Jump to content

My score isn't working well


Gammerr
 Share

Recommended Posts

Hey, 

I'm trying to create a game when items falling down and there is a character at the bottom that needs to collect some of the items and you get points depends on what you're choosing.

I'm sure there is a better way to do it, that's why I'm posting it, I need help with that.

If you'll run this code, you'll see that if you end up with a score more than 70 for example it's not showing the text that I wrote on the if statement, in the code search for ---> else if ( score => 70  && score < 90  )

code:

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

function preload() {

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

}

var cursors;
var randomNumberX;
var cloud;
var cloudGroup;
var pocket;
var sky;
var tryAgain;
var score = 0;
var scoreText;
var gameOverTextStyle;
var gameOverScreen = false;
var gameOverText;
var cloudOutIndex = 0;
var cloudText = {
    "Wants director role" : 10,
    "Director role \n requires masters" : 10,
    "Works at nonprofit" : 20,
    "Nonprofit called \n Just for Girls" : 10,
    "Organization focuses \n on young women" : -5,
    "Organization helps \n with job prep" : -5,
    "Current director \n is leaving soon" : 5,   
    "Been preparing for \n the director role" : 10,   
    "Been at the \n company 5 years" : 5,   
    "Passionate about work" : 5,   
    "Needs more education \n to be successful" : 10,   
    "Director does a lot \n of grant writing" : 5,   
    "Loves working at \n Just for Girls" : -5,   
    "Wants more info \n about grant writing" : 10,   
    "Always had a goal \n to get masters" : 10,   
    "Now feels like the \n right time for masters" : 10,   
};
var cloudLength = Object.keys(cloudText).length;


function create() {

    game.physics.startSystem(Phaser.Physics.ARCADE);
    cursors = game.input.keyboard.createCursorKeys();
    spaceKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

    sky = game.add.tileSprite(0, 0, 1136, 640, 'sky');

    tryAgain = game.add.text(game.world.centerX, game.world.centerY, 'Try Again', { fill: '#ebfa46'} );
    tryAgain.anchor.set(0.5);
    tryAgain.inputEnabled = true;
    tryAgain.visible = false;
    tryAgain.input.useHandCursor = true;
    tryAgain.events.onInputDown.add(restartGame, this);

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

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

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

    cloudGroup = game.add.group();    
    cloudGroup.alpha = 0.8;    

 

    Object.keys(cloudText).forEach(function(key, i){
        // random number for the location of the clouds
        randomNumberX = Math.floor(Math.random() * 780) + 1;
        // create text with a style
        var textElement = new Phaser.Text(game, 40, 30, key, cloudTextStyle);        
        // create a new sprite at a random world location and assign it to a variable named cloud
        var cloud = cloudGroup.create(randomNumberX, i * (-300), 'cloud');
        // append for each cloud item the text from the array
        cloud.addChild( textElement );        
        cloud.customParams = { score: cloudText[key] }

        game.physics.arcade.enable(cloud);
        cloud.body.velocity.y = 120;
        cloud.anchor.set(0);       

        cloud.checkWorldBounds = true;
        cloud.events.onOutOfBounds.add(cloudOut, this); 

    });
    

    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' });

    // Game Over Text
    gameOverTextStyle = { fontSize: "32px", fill: "#000",  align: "center" };
  

}


function update() {

    sky.tilePosition.x -= 0.3;

    cloudGroup.forEach(function(item, i){
        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 = -700;
    } else if (cursors.right.isDown) {
        //  Move to the right
        pocket.body.velocity.x = +700;
    } else {
        pocket.animations.stop();
    }

    if ( gameOverScreen ) {
        spaceKey.onDown.addOnce(restartGame);
    }    
   

}

function collisionHandler (item, pocket) {
    cloudOutIndex++;    
    
    score += item.customParams.score;
    scoreText.text = 'score: ' + score;
    item.destroy();   

    if ( score < 0 ) {        
        cloudGroup.visible = false;
        gameOverText = game.add.text(game.world.centerX, game.world.centerY -150, 'Listen Again \n Remember, only pocket the most important info.', gameOverTextStyle );
        gameOverText.anchor.set(0.5);
        tryAgainScreen();
        
    } else {
        // when all the clouds are off the screen
        if ( ( cloudLength - 1 ) * 2 + 1 == cloudOutIndex ) {
            if ( score < 40 ) {
                gameOverText = game.add.text(game.world.centerX, game.world.centerY -150, 'Listen Again \n Remember, only pocket the most important info.', gameOverTextStyle );
                gameOverText.anchor.set(0.5);                
            } else if ( score => 40  && score < 70  ) {
                gameOverText = game.add.text(game.world.centerX, game.world.centerY -150, 'Great! \n Your pocketing skills are getting there.', gameOverTextStyle );
                gameOverText.anchor.set(0.5);
            } else if ( score => 70  && score < 90  ) {
                gameOverText = game.add.text(game.world.centerX, game.world.centerY -150, 'Excellent! \n You know a thing or two about pocketing.', gameOverTextStyle );
                gameOverText.anchor.set(0.5);
            } else if ( score == 90  ) {
                gameOverText = game.add.text(game.world.centerX, game.world.centerY -150, 'Perfect Score! \n You are a pocket expert.', gameOverTextStyle );
                gameOverText.anchor.set(0.5);
            }
            tryAgainScreen();
        }
        
    }
   

}

function cloudOut(item) {
    cloudOutIndex++;
   
    // when all the clouds are off the screen
    if ( ( cloudLength - 1 ) * 2 + 1 == cloudOutIndex ) {
        if ( score < 40 ) {
            gameOverText = game.add.text(game.world.centerX, game.world.centerY -150, 'Listen Again \n Remember, only pocket the most important info.', gameOverTextStyle );
            gameOverText.anchor.set(0.5);                
        } else if ( score => 40  && score < 70  ) {
            gameOverText = game.add.text(game.world.centerX, game.world.centerY -150, 'Great! \n Your pocketing skills are getting there.', gameOverTextStyle );
            gameOverText.anchor.set(0.5);
        } else if ( score => 70  && score < 90  ) {
            gameOverText = game.add.text(game.world.centerX, game.world.centerY -150, 'Excellent! \n You know a thing or two about pocketing.', gameOverTextStyle );
            gameOverText.anchor.set(0.5);
        } else if ( score == 90  ) {
            gameOverText = game.add.text(game.world.centerX, game.world.centerY -150, 'Perfect Score! \n You are a pocket expert.', gameOverTextStyle );
            gameOverText.anchor.set(0.5);
        }
        tryAgainScreen();
    
    }

}

function tryAgainScreen() {
    gameOverScreen = true;
    tryAgain.visible = true;   
    cloudGroup.visible = false;
    pocket.visible = false;
    scoreText.visible = false;
}

function restartGame() {
    cloudOutIndex = 0;
    score = 0;
    gameOverScreen = false;
    create();
}

function render() {    

    // cloudGroup.forEach(function(item){        
    //     game.debug.body(item);
    // });

    // game.debug.body(pocket);

}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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