4

我正在尝试学习面向对象的javascript并遇到以下问题:

我有一个像这样的对象构造函数(这是正确的术语吗?):

function itemCreator(itemName, itemType, itemPositionX, itemPositionY) {
            this.itemName = itemName;
            this.itemType = itemType;
            this.itemPositionX = itemPositionX;
            this.itemPositionY = itemPositionY;
            allItems.push(this); //store all items in a global variable
            }//end itemCreator;


//then I use it to create an object

megaRocket = new itemCreator (
           'megarocket',
           'item_megarocket',
           108,
           475
           )

现在我意识到我还需要映射这些对象以根据对象具有的“itemType”修改不同的全局变量。这就是我卡住的地方。如何创建一个只有具有特定 itemType 属性的对象才能修改的全局变量?

例如,我想创建一个对象来增加一个名为 amountOfMegarockets 的变量,但前提是该对象的 itemType 是“item_megarocket”。

我稍后计划循环这些项目的数组以查看玩家对象是否接触它们(以收集项目):

function checkForItems(){ 
                var itemLen =allItems.length;
                for (i=0; i < itemLen; i++){
                var itemObject = allItems[i];

                if ( //checking for "collisions" here
                (ship.x < (itemObject.itemBitmap.x + itemObject.size) &&  (ship.x + shipWidth) > itemObject.itemBitmap.x) &&
                (ship.y < (itemObject.itemBitmap.y + itemObject.size) &&  (ship.y + shipWidth) > itemObject.itemBitmap.y)
                ){
                itemObject.actor.y = -500; //just removing the item from canvas here (temporary solution)
                // Here comes pseudo code for the part that I'm stuck with  
                variableBasedOnItemObject.itemType++;

                }

我希望我的解释对某人有意义!

编辑:

Bergi 的回答对我来说最有意义,但我无法正确使用语法。以下是我尝试使用 Bergi 代码的方式:

var amounts = {},
allItems = [];
function itemCreator(itemName, itemType, itemPositionX, itemPositionY) {
this.itemName = itemName;
this.itemType = itemType;
this.itemPositionX = itemPositionX;
this.itemPositionY = itemPositionY;

(amounts[itemType]=2); // this is different from bergi's example because I need to set the initial value of the item to two
//I also shouldn't increase the item amount on creation of the item, but only when it's specifically called from another function
this.increaseCount = amounts[itemType]++; //this should IMO increase the itemType amount inside the amounts object when called, but it doesn't seem to work
}

//creating the object the way bergi suggested:
allItems.push(new itemCreator('shootUp001', 'item_up', 108, 475)); 

现在这是有问题的部分:

function checkForItems(){ 
                var itemLen =allItems.length;
                for (i=0; i < itemLen; i++){
                var itemObject = allItems[i];

                if ( my condition here)
                ){

//code below is not increasing the value for the current itemType in the amounts object. 
//Probably a simple syntax mistake?
                itemObject.itemType.increaseCount; 

                }

           }

           }

为什么我调用 itemObject.itemType.increaseCount; 不增加 amount.itemType 的值?

4

2 回答 2

1

增加一个名为 amountOfMegarockets 的全局变量,但前提是该对象的 itemType 是“item_megarocket”。

不要对这些项目类型中的每一个都使用全局变量。请使用一个对象(在全局或本地范围内)计算其属性中每种类型的数量。

var amounts = {},
    allItems = [];
function Item(itemName, itemType, itemPositionX, itemPositionY) {
    this.itemName = itemName;
    this.itemType = itemType;
    this.itemPositionX = itemPositionX;
    this.itemPositionY = itemPositionY;

    amounts[itemType]++ || (amounts[itemType]=1); // count by type
    allItems.push(this); // store all items
}

请注意,默认情况下我不会将所有Item实例都放在一个数组中,最好省略该行并让它执行调用者:

allItems.push(new Item('megarocket', 'item_megarocket', 108, 475));
于 2013-10-10T10:53:44.600 回答
0

你可以做一些事情,比如

(function (global) {
    // create a scope
    var allItems = [];
    global.itemCreator = function(itemName, itemType, itemPositionX, itemPositionY) {
        this.itemName = itemName;
        this.itemType = itemType;
        this.itemPositionX = itemPositionX;
        this.itemPositionY = itemPositionY;
        allItems.push(this); //store all items in a global variable
        }//end itemCreator;
})(this);

这会奏效。但认真不要让你的代码过于复杂,只是为了制作私有 var。如果有人想使用调试器作弊,他总能找到办法。

---- 编辑如果您只想访问基于 itemType 的全局变量,您可以执行以下操作:

function checkForItems(){ 
    var itemLen =allItems.length;
    for (i=0; i < itemLen; i++){
        var itemObject = allItems[i];

        if ( //checking for "collisions" here
            (ship.x < (itemObject.itemBitmap.x + itemObject.size) &&  (ship.x + shipWidth) > itemObject.itemBitmap.x) &&
            (ship.y < (itemObject.itemBitmap.y + itemObject.size) &&  (ship.y + shipWidth) > itemObject.itemBitmap.y)
        ){
            itemObject.actor.y = -500; //just removing the item from canvas here (temporary solution)
            // Here comes pseudo code for the part that I'm stuck with  
            if (window[itemObject.itemType + "Data"])) {
                variableBasedOnItemObject.itemType++;
            } else {
                window[itemObject.itemType + "Data"] = {
                    itemType: 1
                }
            }
        }
    }
}
于 2013-10-10T09:00:22.617 回答