The Code for the FloatingSprite
/**
* An encapsulation of a Sprite that "floats" from the top of the
* Stage to the bottom
*
* @author Prof. David Bernstein, James Madison University
* @see "The Design and Implementation of Multimedia Software (c) 2011"
* @version 1.0
*
* @augments AbstractSprite
*/
FloatingSprite.prototype = new AbstractSprite();
FloatingSprite.prototype.constructor = FloatingSprite;
/**
* Explicit Value Constructor
*
* @param {Content} The static content (either described or sampled)
* @param {float} The width of the Stage
* @param {float} The height of the Stage
*/
function FloatingSprite(content, width, height)
{
this.content = content;
this.maxX = width;
this.maxY = height;
this.x = Math.random()*this.maxX;
this.y = 0.0;
this.setLocation(this.x, this.y);
this.setVisible(true);
}
/**
* Get the (current) content associated with this Sprite
*
* @returns {Content} The static visual content
*/
FloatingSprite.prototype.getContent = function()
{
return this.content;
}
/**
* Handle a tick from the metronome
* (required by MetronomeListener)
*
* @param {int} time The time
*/
FloatingSprite.prototype.handleTick = function(time)
{
var n = Math.random();
if (n < 0.80) this.y += 2.0;
else if (n > 0.90) this.y -= 1.0;
n = Math.random();
if (n < 0.20) this.x -= 1.0;
else if (n > 0.80) this.x += 1.0;
// Check if at the bottom
if (this.y > this.maxY)
{
this.y = 0.0;
this.x = Math.random()*this.maxX;
}
this.setLocation(this.x, this.y);
}