I'm working on expanding on the tutorial found here
I currently am creating two classes based off of menu
class Menu extends Phaser.GameObjects.Container{
constructor (x, y, scene, heroes)
{
super(scene,x,y);
this.menuItems = [];
this.menuItemIndex = -1;
this.heroes = heroes;
this.x = x;
this.y = y;
}
Menu(x, y, scene, heroes) {
Phaser.GameObjects.Container.call(this, scene, x, y);
this.menuItems = [];
this.menuItemIndex = -1;
this.heroes = heroes;
this.x = x;
this.y = y;
}
addMenuItem(unit){
var menuItem = new MenuItem(0, this.menuItems.length * 20, unit, this.scene);
this.menuItems.push(menuItem);
this.add(menuItem);
}
moveSelectionUp(){
this.menuItems[this.menuItemIndex].deselect();
this.menuItemIndex--;
if(this.menuItemIndex < 0)
this.menuItemIndex = this.menuItems.length - 1;
this.menuItems[this.menuItemIndex].select();
}
moveSelectionDown(){
this.menuItems[this.menuItemIndex].deselect();
this.menuItemIndex++;
if(this.menuItemIndex >= this.menuItems.length)
this.menuItemIndex = 0;
this.menuItems[this.menuItemIndex].select();
}
select(index){
if(!index)
index = 0;
if(this.menuItemIndex != -1){
this.menuItems[this.menuItemIndex].deselect();
}
this.menuItemIndex = index;
this.menuItems[this.menuItemIndex].select();
}
deselect(){
this.menuItems[this.menuItemIndex].deselect();
this.menuItemIndex = 0;
}
confirm(){
}
}
class AttackMenu extends Menu{
constructor (x,y,scene)
{
super(x,y,scene);
this.addMenuItem('Sword');
this.addMenuItem('Staff');
this.select(0);
}
confirm(){
console.log("further");
}
}
class ActionsMenu extends Menu{
constructor (x,y,scene)
{
super(x,y,scene);
this.addMenuItem('Attack');
this.addMenuItem('Defend');
this.select(0);
}
confirm(){
this.scene.events.emit('SelectWeapon');
}
}
My code for actually creating the first class and setting up the listener is here
export default function CreateMenu(view){
view.graphics = view.add.graphics();
view.graphics.lineStyle(1, 0xffffff);
view.graphics.fillStyle(0x031f4c, 1);
view.graphics.strokeRect(2, 150, 90, 100);
view.graphics.fillRect(2, 150, 90, 100);
view.graphics.strokeRect(95, 150, 90, 100);
view.graphics.fillRect(95, 150, 90, 100);
view.graphics.strokeRect(188, 150, 130, 100);
view.graphics.fillRect(188, 150, 130, 100);
view.menus = view.add.container();
view.actionsMenu = new ActionsMenu(100, 153, view);
view.enemiesMenu = new EnemiesMenu(8, 153, view);
view.currentMenu = view.actionsMenu;
// add menus to the container
view.menus.add(view.actionsMenu);
view.menus.add(view.enemiesMenu);
view.input.keyboard.on('keydown', onKeyInput,view);
view.events.on("selectWeapon", selectWeapon,view);
}
Here are the 2 listener functions
function onKeyInput(event){
console.log(event);
console.log(this);
console.log(this.currentMenu);
if(this.currentMenu) {
if(event.code === "ArrowUp") {
this.currentMenu.moveSelectionUp();
} else if(event.code === "ArrowDown") {
this.currentMenu.moveSelectionDown();
} else if(event.code === "ArrowRight" || event.code === "Shift") {
} else if(event.code === "Space" || event.code === "Enter") {
this.currentMenu.confirm();
}
}
}
function selectWeapon(event){
this.graphics.fillStyle('green')
this.graphics.strokeRect(350,150,90,100);
this.graphics.fillRect(350,150,90,100);
this.AttackMenu = new AttackMenu(355, 153, this);
this.menus.add(this.AttackMenu);
this.currentMenu = AttackMenu;
}
The issue I am running into is that the ActionsMenu controls are working as expected, but when I press enter to bring up the AttackMenu, it gives the error no function exists for moveSelectionUp(). Since AttackMenu inherits from Menu it should already be defined. When I check the outputs, it shows ActionsMenu as a complete object, but for AttackMenu it only shows the definition of the class (the super call plus the specifics of the object)
My question is what am I doing wrong? I am new to Javascript and Phaser and Game Design so I'm not sure if this is an issue with being unable to create a new object off of an emitter, poor game design, or not knowing a way to force the constructor to return the object. Should the object be created first and then only showed when the emitter emits the signal?
