Learn Create Your Own Split Screen

Next Page

First we define a new type of object that I'm choosing to call MySprite.

The definition takes the form of a function definition:

function MySprite (img_url) { this.x = 0; this.y = 0; this.velocity_x = 0; this.velocity_y = 0; this.MyImg = new Image(); this.MyImg.src = img_url ; }

The special word "this" means "whatever this happens to be". We don't know whether it's going to be a bug, a melon, or a toaster. And it doesn't matter -- they're all going to be treated the same.


The trick is that we don't use MySprite directly. Instead, we make other things that are MySprites.

var bug1 = new MySprite("http://s2js.com/img/etc/bug1.png"); var bug2 = new MySprite("http://s2js.com/img/etc/bug2.png");

Now, you can refer to things like bug1.x, bug1.y, bug2.x, etc.