We'll start by
altering the definition of our MySprite object.
As well as taking a parameter for the image we want the sprite to adopt, we'll add a second parameter which I've chosen to call bounce. As the object creates itself, we store the parameter in a property called bounciness. We could have called them the same thing, but I like to use different names for parameters and properties.
function MySprite (img_url, bounce) {
this.x = 0;
this.y = 0;
this.visible= true;
this.velocity_x = 0;
this.velocity_y = 0;
this.bounciness = bounce;
this.MyImg = new Image();
this.MyImg.src = img_url ;
}
Now when we create the actual sprite, we can specify the amount of bounciness we want.
var tennis_ball= new MySprite("tennis-ball.png", 0.8);
var bowling_ball= new MySprite("tennis-ball.png", 0.2);
var not_a_ball= new MySprite("tomato.png", 0.0);
You can see how it
makes sense to deal with the image and the bounciness in the same breath, because they're
clearly related.
The wonderful thing about the structure we have is that once defined, each object will know the appropriate way to behave.