Note that there are some explanatory texts on larger screens.

plurals
  1. POModify a global variable based on an object property
    primarykey
    data
    text
    <p>I'm trying to learn object oriented javascript and ran into the following problem:</p> <p>I have an object constructor (is that the right term?) like this:</p> <pre><code>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 ) </code></pre> <p>Now I realised I also need to map these objects to modify different global variables based on which "itemType" the object has. This is where I am stuck. How can I make a global variable that only objects with a specific itemType property can modify?</p> <p>For example I would like to create an object that increments a variable called amountOfMegarockets, but only if the itemType for that object is "item_megarocket".</p> <p>I later plan on looping an array of these items to see if player object touches them (to collect the item):</p> <pre><code>function checkForItems(){ var itemLen =allItems.length; for (i=0; i &lt; itemLen; i++){ var itemObject = allItems[i]; if ( //checking for "collisions" here (ship.x &lt; (itemObject.itemBitmap.x + itemObject.size) &amp;&amp; (ship.x + shipWidth) &gt; itemObject.itemBitmap.x) &amp;&amp; (ship.y &lt; (itemObject.itemBitmap.y + itemObject.size) &amp;&amp; (ship.y + shipWidth) &gt; 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++; } </code></pre> <p>I hope my explanation makes sense to someone!</p> <p>EDIT:</p> <p>Bergi's answer makes most sense to me, but I can't get the syntax right. Here's how I'm trying to use Bergi's code:</p> <pre><code>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)); </code></pre> <p>Now here's the problematic part:</p> <pre><code>function checkForItems(){ var itemLen =allItems.length; for (i=0; i &lt; 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; } } } </code></pre> <p>Why is my call of itemObject.itemType.increaseCount; not increasing the value of amounts.itemType?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload