I’m trying to add interactive image objects on a scene from an array of objects I’m getting from JSON.
For example, I have this JSON:
{
"graph": {
"nodes": [
{"name":"Catharsis", "type":"city", "x":500, "y":200},
{"name":"Arrakeen", "type":"city", "x":300, "y":400},
{"name":"Mine_1", "type":"facility","x":150, "y":450},
{"name":"Belgorod", "type":"city", "x":700, "y":500}
],
"paths": [
{"from":"Arrakeen", "to":"Belgorod"},
{"from":"Arrakeen", "to":"Mine_1"},
{"from":"Arrakeen", "to":"Catharsis"}
]
}
}
Nodes are locations, like cities and facilities.
This JSON is loaded as JS object into mapData
Then this JS draws objects in scene:
preload(){
this.load.image('city','img/locations/city.png');
this.load.image('facility','img/locations/facility.png');
}
create(){
mapData.graph.paths.forEach(path => {
var nodesFrom = mapData.graph.nodes
.find(node => {
return node.name == path.from
}, path);
var nodesTo = mapData.graph.nodes
.find(node => {
return node.name == path.to
}, path);
this.add.line(0, 0, nodesFrom.x, nodesFrom.y, nodesTo.x, nodesTo.y, 0x000000, 1)
.setOrigin(0, 0);
}, this);
mapData.graph.nodes.forEach(node => {
this.add.image(node.x,node.y,node.type)
.setInteractive()
.setScale(0.2);
this.add.text(node.x,node.y,node.name,{fontSize:20,color:'#000'})
.setOrigin(0.5, 0.3);
this.input.on('gameobjectdown',this.onObjectClicked);
}, this);
}
And this is the resulting scene:
But I want to show context menu of actions when one of the cities or facilities is clicked. With the list of menu items formed conditionally. For example, Move to [city.name]
etc. But now all of the interactive objects send click events when one is clicked, so I need to somehow identify added images unambigously. Maybe add a key equal to the name of node, but how? And how to read it afterwards?