As with obstacles, we'll start with an array to keep track of all our fruit.
var fruit = []; // fruit available to be eaten
And we'll create a function that adds one piece of fruit at a specific location:
function add_fruit(x, y) {
var n = new MySprite("http://www.s2js.com/img/etc/strawberry.png", 35) ;
n.x = x;
n.y = y;
n.animation_rate = Math.random()*40+40;
n.animation_final_costume = 4;
fruit.push(n);
}
A couple of things to note about this function:
- We tell the MySprite the costume width
- We set a slightly random animation rate, between 40 and 80, so they all roll their eyes around at slightly different times
- We limit the animation by specifying it only runs up to costume 4 (then repeats like normal)
Finally we add a bunch of fruit to some carefully chosen locations.
You may notice many of these are outside the visible bounds of the canvas. Not a problem -- this is going to be a 2-d scroller.
add_fruit(20, 20) ;
add_fruit(-20, 220) ;
add_fruit(-50, 320) ;
add_fruit(290, 170) ;
add_fruit(390, 100) ;
add_fruit(200, -60) ;
Of course, let's not forget to loop through and call each fruit's frame function:
for (var i=0; i < fruit.length; i++) fruit[i].Do_Frame_Things();