javascript中对象内的对象
我对 Javascript 有点陌生,所以也许这只是一个菜鸟错误,但我在环顾四周时没有找到任何对我有特别帮助的东西。我正在编写一个游戏,并且正在尝试为暂停菜单构建一个对象。
我想做的一件事是为了组织起见,让菜单上的按钮成为pause_menu对象内部的对象。我最终将向这些对象添加事件处理程序,并且我也想在pause_menu 对象内执行此操作。有些按钮还没有完全编码,但我想在继续之前至少让一些东西可以工作。
我使用 Raphael.js v1.5.2 来渲染形状。 Raphael 的东西适用于界面的其余部分,但其代码看起来并不像这样令人愉快,所以与此类似的东西对我来说更可取。
我当前的问题是,当我执行 varpause_menu = newpause_menu(); 时,实际上没有任何内容呈现。
这是我到目前为止的暂停菜单代码:
//Pause Menu Object:
function pause_menu() {
function pause_button() {
this.button = game.rect(0, 350, 150, 50, 5);
this.text = game.text(75, 375, 'PAUSE');
}
function resume_button() {
this.button;
this.text;
}
function quit_button() {
this.button;
this.text;
}
this.pause_button = new pause_button(); //the button that the user presses to pause the game (I want an event handler on this to trigger .show() method for presently hidden menu items)
this.resume_button = new resume_button();
this.quit_button = new quit_button();
this.box = game.rect(150, 50, 400, 300, 5).hide(); //the box that surrounds the menu when it appears
}
var pause_menu = new pause_menu();
好的,这是解决方案(带有事件处理程序):
var pause_menu = {
pause_button: { button : game.rect(0, 350, 150, 50, 5).click(function (event){
pause_menu.menu_box.show();
}), text : game.text(75, 375, 'PAUSE') },
menu_box: game.rect(150, 50, 400, 300, 5).hide(),
resume_button: {},
quit_button: {}
};
I'm somewhat new to Javascript, so maybe this is just a noob mistake, but I haven't found anything that specifically helps me while looking around. I'm writing a game, and I'm trying to build an object for the pause menu.
One of the things I would like to do is have the buttons on the menu be objects inside of the pause_menu object for the sake of organization. I'm eventually going to add event handlers to these objects, and I'd like to do that inside the pause_menu object as well. Some of the button's aren't fully coded yet, but I'd like to get at least something working before I keep going.
I'm using Raphael.js v1.5.2 to render the shapes. The Raphael stuff works for the rest of the interface, but the code for that is not as pleasant to look at as this, so something similar to this would be preferable to me.
My current problem is that nothing actually renders when I do var pause_menu = new pause_menu();
This is the code I have so far for the pause menu:
//Pause Menu Object:
function pause_menu() {
function pause_button() {
this.button = game.rect(0, 350, 150, 50, 5);
this.text = game.text(75, 375, 'PAUSE');
}
function resume_button() {
this.button;
this.text;
}
function quit_button() {
this.button;
this.text;
}
this.pause_button = new pause_button(); //the button that the user presses to pause the game (I want an event handler on this to trigger .show() method for presently hidden menu items)
this.resume_button = new resume_button();
this.quit_button = new quit_button();
this.box = game.rect(150, 50, 400, 300, 5).hide(); //the box that surrounds the menu when it appears
}
var pause_menu = new pause_menu();
OK, so here's the solution (with an event handler):
var pause_menu = {
pause_button: { button : game.rect(0, 350, 150, 50, 5).click(function (event){
pause_menu.menu_box.show();
}), text : game.text(75, 375, 'PAUSE') },
menu_box: game.rect(150, 50, 400, 300, 5).hide(),
resume_button: {},
quit_button: {}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
然后:
等等等等。
then:
etc etc.
只要您将一个对象声明为另一个父对象的属性,您就可以拥有任意多个对象层次结构级别。注意每个级别的逗号,这是棘手的部分。不要在每个级别的最后一个元素后使用逗号:
{el1, el2, {el31, el32, el33}, {el41, el42}}
You may have as many levels of Object hierarchy as you want, as long you declare an Object as being a property of another parent Object. Pay attention to the commas on each level, that's the tricky part. Don't use commas after the last element on each level:
{el1, el2, {el31, el32, el33}, {el41, el42}}