I have an array call “Predatorlist” storing
<div id="Predator1"><img src='jef-frog.gif' width='50'></div>
And I declared a var to store the “Predator” and attached the ID.
var speciesType = 'Predator' + document.getElementById('amount').value;
And I have an Input button ::
<INPUT type="button" value="Play" onClick="javascript:runItem('Predator') ;">
May I know what item I should pass in for the following function to capture and produce as what is required in the input button. “Javascript:runItem(‘Predator’);?
function runItem(Predatorlist) {
var item1 = $("#"+speciesType),cycle1;
/* Set a the starting position to be random by editing the css */
$(item1).css("left", startItem1X+"px");
$(item1).css("top", startItem1Y+"px");
//$("<img src='jef-frog.gif' width='50'>").appendTo("div#Predator");
/* Cycle1 and Cycle2 variables allow infinite loop */
(cycle1 = function() {
var m = randomRange(50,100);
var n = randomRange(75,150);
item1.animate({left:'+='+n},2000);
item1.animate({left:'+='+m, top:'+='+m}, 2000)
item1.animate({left:'-='+m, top:'+='+m}, 2000)
item1.animate({left:'-='+n},2000);
item1.animate({top:'-='+n},2000,cycle1)
})();
alert(speciesType);
}
This doesnt look right, you should only give a string into a jQuery object, it makes the code far more readable than passing the enire object in there. So, the above lines should become either
or
Next, I would use a
setIntervalfor the animation loop, because you cannot stop it like you have it now, and it makes the code much cleaner.Finally, for your predators: Do you need something like the following, that appends code to the
bodyof the document?For more questions, please comment, I found the question hard to understand, so my answer might not be what you mean.
EDIT 2:
See the code (working): http://jsfiddle.net/sREEw/3/
What am I doing?
On an addition of a predator:
num, so the next predator will have a new IDOn a removal of a predator:
numby 1 for easier calculationsAs you can see, all I am saving in variables is the current number of predators, and the interval ID, so I can stop the loop if needed.