I am trying to load data (including images) via a JSONP feed into Phaser 3.60.0. I know how to load image asset normally as per my GameScene.js code below:
class GameScene extends Phaser.Scene {
constructor() {
super({ key: 'GameScene' });
}
preload() {
this.load.image('oranges', './assets/Ambersweet_oranges.jpg');
}
create() {
this.add.image(240, 320, 'oranges')
}
}
The JSONP feed is located on a different server and is structured like this:
/**/ typeof handleItems === 'function' && handleItems([
{"offerWeek":"kw17","id":"1028377","name":"Bananen","price":"1.79","url":"https://www.rewe.de/angebote/nationale-angebote/#1028377","image":"https://upload.wikimedia.org/wikipedia/commons/5/5d/Pl%C3%A1tanos_de_Canarias.JPG"},
{"offerWeek":"kw17","id":"2670547","name":"Big City Pizza","price":"1.99","url":"https://www.rewe.de/angebote/nationale-angebote/#2670547","image":"https://upload.wikimedia.org/wikipedia/commons/a/a5/Pizza_20.jpg"},
{"offerWeek":"kw17","id":"3663221","name":"Die Thüringer Bratwurst","price":"4.59","url":"https://www.rewe.de/angebote/nationale-angebote/#3663221","image":"https://upload.wikimedia.org/wikipedia/commons/9/91/Bratwurst_on_the_grill.jpg"},
{"offerWeek":"kw17","id":"3919250","name":"Orangen","price":"2.99","url":"https://www.rewe.de/angebote/nationale-angebote/#3919250","image":"https://upload.wikimedia.org/wikipedia/commons/4/43/Ambersweet_oranges.jpg"}]
);
How do I access the product names and images and display them in Phaser? Also how do I wait for the request to finish before create() is called? All advice appreciated!
There is probably a better solution, but my first idea would be:
create a global variabel, like this
create the jsonp callback function, like this
Now in your game in the
updatemethod of the scene, you can check for the finished load, like this(Or alternative just use the global variable)
OR if you want to use events and emitters in phaser, here is a small example:
Just:
createmethod, withthis.events.on(...);(link to the documentation)scene.events.emit(...)function. (link to the documentation)If you only want to start the game after loading the data, the easy solution would be to create the game in the
handleItemsfunction. Something like thisand in the create function you could get the data like this: