2015-08-11 2 views
0

Привет всем Я новичок в Phaser, это действительно здорово, но у меня проблема с использованием add.sprite, у меня есть массив, заполненный информацией о игре, и я пытаюсь вставьте картинки в карту на экране, код следующий. Это полный игровой код, однако я буду подробно останавливаться. У меня есть массив под названием «paises», который имеет название каждой страны, а также население и немного больше информации. Я пытаюсь добавить функцию для добавления спрайтов (изображений), чтобы позже я мог просто использовать цикл for, чтобы добавить все их на экран. Эта функция называется addSprite();Phaser загружает изображения с помощью функции объекта

сообщения об ошибке, что я получаю то, что расположение изображения, функция addSprite будет использовать считаются неопределенными, поскольку он не знает где "венесуэльский внутри следующий код находится: addSprite:function(){ this.add.sprite(85,2,'venezuela');} Его же вопрос другие функции addSprite. Я немного новичок в прототипировании, так что это возможно. Также попытался добавить массив paises внутри функции create, чтобы дать ему время для загрузки активов, но не повезло.

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. 

}}; 

ответ

0

Привет, ребята только что получили его на работу, решение сопряжено с перемещением функции вне объекта и просто сохранить параметры внутри объекта (X, Y и имя файла). Я также сделал перемену паз глобальным. Это может быть не самое элегантное решение, но оно делает то, что мне нужно. Я отправлю код в случае, если кто-то может извлечь из него выгоду.

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. 

} 

}; 
Смежные вопросы