Wednesday, 9 April 2014

Working Processes: Creating the Game - Implementing the Inventory (coding)

I took the inventory system which I created on this blog post and adjusted it so that it would not need to be so repetitive dependent upon which item was picked up. Originally the code look like this:

function ifFullPassSquare():void {
    if(!inv1Full){
        inv1.gotoAndStop(2);
        inv1Full = true;
    } else if (inv1Full){
        inv2.gotoAndStop(2);
        inv2Full = true;
    }
}
function ifFullPassCircle():void {
    if(!inv1Full){
        inv1.gotoAndStop(3);
        inv1Full = true;
    } else if (inv1Full){
        inv2.gotoAndStop(3);
        inv2Full = true;
    }
}


This code works, but it is very repetitive and will become more repetitive the more items you have to add to the inventory.

Since the two functions are very similar, I have created a code which will include all items. This would be done through a variable.

Here is the code:

function ifFullPassItem():void {
    if(!itemBox1Full){
        bottomBar.barItemBox1.gotoAndStop(itemInQuestion);
        trace(itemBox1Full);
        itemBox1Full = true;
    } else if (itemBox1Full){
        bottomBar.barItemBox2.gotoAndStop(itemInQuestion);
        trace(itemBox1Full);
        itemBox2Full = true;
    }
}

itemInQuestion is the variable. When picking up an item, itemInQuestion will be set to the name of what label that item has in the inventory. This function would then be inserted and apply that code.

The lable, as seen in the previous blog post would look like this, itemLightbulb
When the item is found, the script itemInQuestion=itemLightbulb; would be implemented.

As more item boxes are added, the code will increase in size.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.