Note that there are some explanatory texts on larger screens.

plurals
  1. POOdd bug in my javascript code
    primarykey
    data
    text
    <p>Picture of problem: <a href="http://puu.sh/5spfQ.png" rel="nofollow">http://puu.sh/5spfQ.png</a></p> <p>My first question here. I hope I can explain it to a decent extent, to give you an idea of what I'm trying to do.</p> <p>I am working on a small javascript game, where the only objective is to make money. One of the methods to make money is by hiring workers. However, there's a problem. For instance we have <strong>Worker A</strong> and <strong>Worker B</strong>. For some odd reason, whenever Worker B mines something, Worker A also receives the mined ore (so both seem to always have the same amount of ore). I can't seem to figure out why this is; they should not be receiving each others ore.</p> <p><strong>Note: >hired&lt; workers are stored in the "employed" variable.</strong></p> <p>The fun part, the code:</p> <p>*This function fills in placeholders to the employed variable for each possible worker for hire. *</p> <pre><code> //add "placeholder data" to prevent future undefined/errors //and makes future modification easier, specially in workerMain() var o = []; for(ore in ores){o[ore] = 0;} for(w in workers){ employed[w] = [0, o]; } </code></pre> <p>So our employed variable may look something along the lines of:</p> <p>(<strong>'miner a' => [0, ['coal_ore' => 0,'diamond_ore' => 0]]</strong>, 'miner b' => [0, ['coal_ore' => 0, 'diamond_ore' => 0]])</p> <p>Now once they are hired, a loop that is ran every second will determine how much ore they have mined, and <strong>it should</strong> put any worker's mined ore in their respective spot.</p> <pre><code>function workerMain(){ setTimeout(function(){ for(e in employed){ var wObj = workers[e]; var orePerSecond = Math.ceil(wObj.opm/60)*employed[e][0]; var oresMined = generateOres(orePerSecond, wObj.pickaxe); //add newly mined ore count to worker's ore for(ore in oresMined){ employed[e][1][ore] += oresMined[ore]; //we aren't storing the ore, so let's go ahead and sell it money += ores[ore].worth*oresMined[ore]; } } updateValues(); workerMain(); }, 1000); } </code></pre> <p>But again, somehow if Miner A mines a coal ore, somehow Miner B will also get that coal ore.</p> <p>Hope I've explained my problem thoroughly. If you can't seem to find the problem within this code, it may lie within these sibling functions:</p> <p><a href="http://pastebin.com/2WgT8Acg" rel="nofollow">http://pastebin.com/2WgT8Acg</a></p>
    singulars
    1. This table or related slice is empty.
    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