javascriptspriteloadingphaser-frameworkprototyping

Phaser loading images using object function


Hi everyone I'm new to Phaser, its really cool but I'm having an issue trying to use add.sprite, I have an array filled with the game information and I'm trying to paste the pictures into the map on the screen the code is as follows. It is the full game code however I'll elaborate further. I have an array called 'paises' which has the name of each country as well as the population and a little bit of more information. I'm trying to add a function for adding the sprites (images) so that later on I can just use a for loop to add all of them to the screen. This function is known as addSprite();

The error message that I get is that the location of the image that the addSprite function will use is considered as undefined since it doesnt know where 'venezuela' inside the following code is located: addSprite:function(){ this.add.sprite(85,2,'venezuela');} Its the same issue for the other addSprite functions. I'm a little new to prototyping so that might be it. Also tried adding the array paises inside the create function to give it time to load the assets but no luck.

BasicGame = {};

BasicGame.Game = function (game) {};

var paises = [
{
    name:'venezuela',
    population: 30620404,
    brandRecognition: 0,
    clients:0,
    sales:0,
    addSprite:function(){
        this.add.sprite(85,2,'venezuela');
}},
{
    name:'colombia',
    population:48264000,
    brandRecognition:0,
    clients:0,
    sales:0,
    addSprite:function(){
         this.add.sprite(40,2,'colombia');

}},
{
    name:'ecuador',
    population:16309000,
    brandRecognition:0,
    clients:0,
    sales:0,
    addSprite:function(){
    this.add.sprite(25,80,'ecuador');}
},
{
    name:'guayana',
    population:747884,
    brandRecognition:0,
    clients:0,
    sales:0,
addSprite:function(){
this.add.sprite(164,26,'guayana');}
}];

BasicGame.Game.prototype = {

init: function () {
    // set up input max pointers
    this.input.maxPointers = 1;
    // set up stage disable visibility change
    this.stage.disableVisibilityChange = true;
    // Set up the scaling method used by the ScaleManager
    // Valid values for scaleMode are:
    // * EXACT_FIT
    // * NO_SCALE
    // * SHOW_ALL
    // * RESIZE
    // See http://docs.phaser.io/Phaser.ScaleManager.html for full document
    this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
    // If you wish to align your game in the middle of the page then you can
    // set this value to true. It will place a re-calculated margin-left
    // pixel value onto the canvas element which is updated on orientation /
    // resizing events. It doesn't care about any other DOM element that may
    // be on the page, it literally just sets the margin.
    this.scale.pageAlignHorizontally = true;
    this.scale.pageAlignVertically = false;
    // Force the orientation in landscape or portrait.
    // * Set first to true to force landscape. 
    // * Set second to true to force portrait.
    this.scale.forceOrientation(true, false);
    // Sets the callback that will be called when the window resize event
    // occurs, or if set the parent container changes dimensions. Use this 
    // to handle responsive game layout options. Note that the callback will
    // only be called if the ScaleManager.scaleMode is set to RESIZE.
    this.scale.setResizeCallback(this.gameResized, this);
    // Set screen size automatically based on the scaleMode. This is only
    // needed if ScaleMode is not set to RESIZE.
    this.scale.setScreenSize(true);
    // Re-calculate scale mode and update screen size. This only applies if
    // ScaleMode is not set to RESIZE.
    this.scale.refresh();

},


preload: function () {

    // Here we load the assets required for our preloader (in this case a 
    // background and a loading bar)
    this.load.image('logo', 'asset/phaser.png');
    this.load.image('brasil','asset/brasil.png');
    this.load.image('colombia','asset/colombia.png');
    this.load.image('venezuela','asset/venezuela.png');
    this.load.image('ecuador','asset/ecuador.png');
    this.load.image('peru','asset/peru.png');
    this.load.image('guayana','asset/guayana.png');
    this.load.image('surinam','asset/surinam.png');
    this.load.image('guayanaFrancesa','asset/guayanaFrancesa.png');
    this.load.image('brasil','asset/brasil.png');
    this.load.image('chile','asset/chile.png');
    this.load.image('bolivia','asset/bolivia.png');
    this.load.image('argentina','asset/argentina.png');
    this.load.image('paraguay','asset/paraguay.png');
    this.load.image('uruguay','asset/uruguay.png');
    this.load.image('menu','asset/menu.png');
    this.load.image('oceano','asset/oceano.jpg');
},

create: function () {        
    var oceano = this.add.sprite(0,0,'oceano');
   var menu = this.add.sprite(450,50,'menu');

    for(var i=0;i<3;i++){
        paises[i].addSprite();
    }

},


gameResized: function (width, height) {

    // This could be handy if you need to do any extra processing if the 
    // game resizes. A resize could happen if for example swapping 
    // orientation on a device or resizing the browser window. Note that 
    // this callback is only really useful if you use a ScaleMode of RESIZE 
    // and place it inside your main game state.

}};

Solution

  • Hi guys just got it to work, the solution involved moving the function outside the object and just saving the parameters inside the object (the x,y and file name). I also made the paises variable a global one. It might not be the most elegant solution but it does what I need it to do. I'll post the code in case someone else can benefit from it.

    var paises = [
    {
        name:'venezuela',
        population: 30620404,
        brandRecognition: 0,
        clients:0,
        sales:0,
        x:85,
        y:2
    },
    {
        name:'colombia',
        population:48264000,
        brandRecognition:0,
        clients:0,
        sales:0,
        x:40,
        y:2
    },
    {
        name:'ecuador',
        population:16309000,
        brandRecognition:0,
        clients:0,
        sales:0,
        x:25,
    y:80
    },
    {
        name:'guayana',
        population:747884,
        brandRecognition:0,
        clients:0,
        sales:0,
    x: 164,
    y:26
    }];
    
    BasicGame = {
    
    };
    
    BasicGame.Game = function (game) {
    };
    
    BasicGame.Game.prototype = {
    
    init: function () {
        // set up input max pointers
        this.input.maxPointers = 1;
        // set up stage disable visibility change
        this.stage.disableVisibilityChange = true;
        // Set up the scaling method used by the ScaleManager
        // Valid values for scaleMode are:
        // * EXACT_FIT
        // * NO_SCALE
        // * SHOW_ALL
        // * RESIZE
        // See http://docs.phaser.io/Phaser.ScaleManager.html for full document
        this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
        // If you wish to align your game in the middle of the page then you can
        // set this value to true. It will place a re-calculated margin-left
        // pixel value onto the canvas element which is updated on orientation /
        // resizing events. It doesn't care about any other DOM element that may
        // be on the page, it literally just sets the margin.
        this.scale.pageAlignHorizontally = true;
        this.scale.pageAlignVertically = false;
        // Force the orientation in landscape or portrait.
        // * Set first to true to force landscape. 
        // * Set second to true to force portrait.
        this.scale.forceOrientation(true, false);
        // Sets the callback that will be called when the window resize event
        // occurs, or if set the parent container changes dimensions. Use this 
        // to handle responsive game layout options. Note that the callback will
        // only be called if the ScaleManager.scaleMode is set to RESIZE.
        this.scale.setResizeCallback(this.gameResized, this);
        // Set screen size automatically based on the scaleMode. This is only
        // needed if ScaleMode is not set to RESIZE.
        this.scale.setScreenSize(true);
        // Re-calculate scale mode and update screen size. This only applies if
        // ScaleMode is not set to RESIZE.
        this.scale.refresh();
    
    },
    
    
    preload: function () {
    
        // Here we load the assets required for our preloader (in this case a 
        // background and a loading bar)
        this.load.image('logo', 'asset/phaser.png');
        this.load.image('brasil','asset/brasil.png');
        this.load.image('colombia','asset/colombia.png');
        this.load.image('venezuela','asset/venezuela.png');
        this.load.image('ecuador','asset/ecuador.png');
        this.load.image('peru','asset/peru.png');
        this.load.image('guayana','asset/guayana.png');
        this.load.image('surinam','asset/surinam.png');
        this.load.image('guayanaFrancesa','asset/guayanaFrancesa.png');
        this.load.image('brasil','asset/brasil.png');
        this.load.image('chile','asset/chile.png');
        this.load.image('bolivia','asset/bolivia.png');
        this.load.image('argentina','asset/argentina.png');
        this.load.image('paraguay','asset/paraguay.png');
        this.load.image('uruguay','asset/uruguay.png');
        this.load.image('menu','asset/menu.png');
        this.load.image('oceano','asset/oceano.jpg');
    },
    
    create: function () {        
        var oceano = this.add.sprite(0,0,'oceano');
       var menu = this.add.sprite(450,50,'menu');
    
        for(var i=0;i<3;i++){
            this.add.sprite(paises[i].x,paises[i].y,paises[i].name);
        }
    
    },
    
    
    gameResized: function (width, height) {
    
        // This could be handy if you need to do any extra processing if the 
        // game resizes. A resize could happen if for example swapping 
        // orientation on a device or resizing the browser window. Note that 
        // this callback is only really useful if you use a ScaleMode of RESIZE 
        // and place it inside your main game state.
    
    }
    
    };