Hello, I tried making it like this:
var InGameMenu = me.Renderable.extend({
init: function(x, y){
this.parent(new me.Vector2d(x, y), 0, 0);
this.x = x;
this.y = y;
//create Button
this.playerTabBtn = new Button(200,200, "playerTab", "playerTab", 64, 64);
this.invTabBtn = new Button(200,200, "invTab", "invTab", 64, 64);
me.game.world.addChild(this.playerTabBtn);
me.game.world.addChild(this.invTabBtn);
//this.playerTabMenu = new PlayerTab();
//this.invTabMenu = new InventoryTab();
//this.playerTabBtn.onClick = function(){
// me.game.world.addChild(this.playerTabMenu);
//}
//this.invTabBtn.onClick = function(){
// me.game.world.addChild(this.invTabMenu);
//}
},
update: function(){
return true;
},
draw: function(context){
//create a black rectangle as the ingame menu background
context.fillStyle = "#000000";
context.fillRect(0, 0, 860, 380);
return true;
}
});
and I add the menu button inside the HUD container
game.HUD = game.HUD || {};
game.HUD.Container = me.ObjectContainer.extend({
init: function() {
this.parent();
this.isPersistent = true;
this.collidable = false;
this.z = Infinity;
this.status = new game.HUD.statusBar(0, 0);
this.addChild(this.status);
this.bindW = false;
this.bindA = false;
this.bindS = false;
this.bindD = false;
this.control = new game.HUD.controlPad(20, 350);
this.addChild(this.control);
this.menuBtn = new game.HUD.menuButton(800, 300);
this.addChild(this.menuBtn);
this.atkBtn = new game.HUD.attackButton(750, 360);
this.addChild(this.atkBtn);
//this.menuBar = new game.HUD.menuBar(200, 80);
//this.addChild(this.menuBar);
}
});
and this is the menu button:
game.HUD.menuButton = me.Renderable.extend({
init: function (x, y) {
this.parent(new me.Vector2d(x, y), 0, 0); // (size does not matter here)
this.x = x;
this.y = y;
//check if the mouse is already binded with key M
this.keyMBinded = false;
this.floating = true; // make sure we use screen coordinates
this.menuBtnImage = me.loader.getImage("menuBtnNorm");
this.ingameMenu = new InGameMenu(0,0);
},
update: function () {
if (me.input.mouse.pos.x >= this.x && me.input.mouse.pos.x <= (this.x + this.menuBtnImage.width) && me.input.mouse.pos.y >= this.y && me.input.mouse.pos.y <= (this.x + this.menuBtnImage.height)) {
//console.log("M");
this.keyMBinded = true;
me.game.world.addChild(this.ingameMenu);
console.log("add ingame menu");
//me.input.bindMouse(me.input.mouse.LEFT, me.input.KEY.M);
}
return true;
},
draw: function (context) {
//draw menu Button
context.drawImage(this.menuBtnImage, this.x, this.y);
return true;
}
});
However, the button I set in IngameMenu was drawn even before i hovered on the menu button (should be click, but for now i set it to hover for testing purposes). And when I hovered to the Menu Button, the rectangle wasn't drawn (this supposed to be the ingame menu background).
What am I doing wrong? The code is just testing if I could bring up the ingame Menu first (along with the two buttons)....