Note that there are some explanatory texts on larger screens.

plurals
  1. POsending an object's property as parameter to a function returns undefined
    primarykey
    data
    text
    <p>I am getting myself into javascript/jQuery and therefore am building a Plant vs Zombies. I created an object for the enemies and create a reference for each enemy that shows up with that code: This is the object:</p> <pre><code>var Enemy = function(row, num) { 'use strict'; this.row = row; this.number = num; this.move_interval; this.html_element; }; Enemy.prototype.create = function() { this.html_element = $('&lt;div class="enemy '+this.row+' full" style="left:1140px;top: '+$('tr.row_'+this.row).position().top+'px"&gt;&lt;/div&gt;').prependTo('.container'); return this; }; Enemy.prototype.die = function() { clearInterval(this.move_interval); this.html_element.remove(); return this; }; Enemy.prototype.move = function() { this.move_interval = setInterval(function() {moveEnemy(this.number)}, 2000); return this; }; </code></pre> <p>this is the interval for creating an enemy (i is defined as global variable "var i = 0;"):</p> <pre><code>enemy_interval = setInterval("addEnemy(i++)", 9000)} </code></pre> <p>this is the function to create an Enemy (the random-thingy is because i have four rows on which the enemy could spawn on, thats also what the property row in the object is for):</p> <pre><code>function addEnemy(num) { if (num &lt; 5) { switch (getRandom(1,4)) { case 1: enemies[num] = new Enemy("one", num); enemies[num].create().move(); break; case 2: enemies[num] = new Enemy("two", num) enemies[num].create().move(); break; case 3: enemies[num] = new Enemy("three", num) enemies[num].create().move(); break; case 4: enemies[num] = new Enemy("four", num) enemies[num].create().move(); break; default: break; } } } </code></pre> <p>And here is where the error is showing up:</p> <pre><code>function moveEnemy(number) { enemies[number].html_element.animate({left: "-=150"}, 2000, "linear"); } </code></pre> <p>I have tried several ways by editing the moveEnemy function to get the property "html_element" as parameter and doing it that way:</p> <pre><code>function moveEnemy(obj) { obj.animate({left: "-=150"}, 2000, "linear"); } </code></pre> <p>I have tried to write it like enemies[number][html_element] because i read thats actually the way to get a property but it didn't work as well. It is always telling me that the parameter ('obj' or as above 'number' is undefined). </p> <p>Is anyone able to see that mistake? D:</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.
    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