Add amount with javascript

1

I am working on a project to make a lucky spinner. Roulette is composed of several sections. Depending on which section you fall, a text is generated on the screen that tells you what you have won or lost.

The idea is that as long as you fall in the points sections, add the points with a counter. And if it falls in the "lose" section, lose everything and restart the game.

I do not like to add up the points or lose everything and have to start again. This is the code that I have at the moment.

Thank you very much!

var game;
var wheel; 
var canSpin;
var slices = 8;
//Aquí genero la cadena de textos con la puntuación de las diferentes secciones
var slicePrizes = [{title:"10 PUNTOS", value:10}, {title:"50 PUNTOS", value:50}, {title:"500 PUNTOS", value:500}, {title:"PERDER", value:"reset"}, {title:"200 PUNTOS", value:200}, {title:"100 PUNTOS", value:100}, {title:"150 PUNTOS", value:150}, {title:"PERDER", value:"reset"}];

var prize;
// Variable del texto del premio
var prizeText;

var puntos = 0; // suma de puntos iniciada a 0

var puntosText; // muestra la suma de los puntos

window.onload = function() {    
     // creo el juego
    game = new Phaser.Game(500, 600, Phaser.AUTO, "");
    game.state.add("PlayGame",playGame);
    game.state.start("PlayGame");
}

// PLAYGAME STATE

var playGame = function(game){};

playGame.prototype = {
     preload: function(){
          // cargamos los gráficos
        game.load.image("wheel", "wheel.png");
        game.load.image("pin", "pin.png");     
     },

    create: function(){
          // Color de fondo
        game.stage.backgroundColor = "#880044";
          // Coloco la rueda en el medio del canvas
        wheel = game.add.sprite(game.width / 2, game.width / 2, "wheel");
          wheel.anchor.set(0.5);
          var pin = game.add.sprite(game.width / 2, game.width / 2, "pin");
          pin.anchor.set(0.5);
          prizeText = game.add.text(game.world.centerX, 480, "");
          prizeText.anchor.set(0.5);
          prizeText.align = "center";
          canSpin = true;
          game.input.onDown.add(this.spin, this);   

          puntos = game.add.text(game.world.centerX, 510, "");
          puntos.anchor.set(0.5);
          puntos.align = "center";
      // adding the text field
      puntosText = game.add.text(game.world.centerX, 540, "");         
      puntosText.anchor.set(0.5);
      puntosText.align = 'center';

      //    Font style
      prizeText.fontSize = 22;
      puntosText.fontSize = 20;

      //    Stroke color and thickness
      puntosText.fill = '#7C4698';

    },
     // función del spin
     spin(){
          // puedo girar la rueda?
          if(canSpin){  
               // reseteando el texto 
               prizeText.text = "";

               var rounds = game.rnd.between(2, 4);
               var degrees = game.rnd.between(0, 360);
               prize = slices - 1 - Math.floor(degrees / (360 / slices));
               canSpin = false;
               var spinTween = game.add.tween(wheel).to({
                    angle: 360 * rounds + degrees
               }, 3000, Phaser.Easing.Quadratic.Out, true);
               spinTween.onComplete.add(this.winPrize, this);
          }
     },
     // Asignamos los premios
     winPrize(){
          canSpin = true;
          prizeText.text = slicePrizes[prize].title;
          puntos.text = slicePrizes[prize].value;
                if (slicePrizes[prize].value === 'reset'){
                    puntos = 0;
                } else {
                    puntos += slicePrizes[prize].value;
                }
          puntosText.text = puntos;

     }
}

I have already been able to show the score, but only the first roll appears. I do not add after continuing to pull.

    
asked by Jok 16.10.2018 в 22:08
source

1 answer

1

You could try saving the scores as objects:

var slicePrizes = [{title:"10 PUNTOS", value:10}, {title:"50 PUNTOS", value:50}, ..., {title:"PIERDES", value:"reset"}, ...];

Then you create a global varialble that stores the sum:

var sum; // suma de puntos

And when you call winPrize() , you add or restart the score depending on the case:

winPrize(){
    canSpin = true;
    prizeText.text = slicePrizes[prize].title;
    if (slicePrizes[prize].value === 'reset'){
        sum = 0;
    } else {
        sum += slicePrizes[prize].value;
    }
}
    
answered by 16.10.2018 / 23:16
source