Images and Sprites
The type imageLoader can be used to load
an external image. An URL is the only argument of initializer for
type imageLoader
The getter .url from type
imageLoader returns the image URL
(the one given in the initializer).
The getter .isError from type
imageLoader waits for the image to load.
If the loading succeeds, it returns false.
If it fails, it returns true.
The type image represents images.
The functional getter .getImage from type
imageLoader returns the image if the loading has succeeded.
The member function .draw from type
image draws the image to the screen.
This member is actually a procedure, because it modifies the displayed output,
but it is common to refer to procedures as “functions”.
The argument is of type point2D, and it
specifies the position on the screen where the top left corner of the image is to be placed.
This member function does not return a value, which makes it a procedure.
In this program, the argument of member function
.draw is given by the initializer of type
point2D.
To make the zombie not leave a trail, clear the screen on each iteration of the
for loop.
The member function .draw from type
image copies the image to the screen.
It can accept 1 arguments or 2 arguments.
The first argument is obligatory. It is of type point2D
and specifies the position on the screen
where the top left corner of image copy is to be placed.
The second arguments is optional. It is of type vector2D
and specifies the size of the image copy.
If the second argument is ommited, then the size of the original is used.
In this program, all arguments of member function
.draw are given by
initializers of types point2D
and vector2D.
The second argument can be used to stretch or resize the image.
The getter .dx from type
image returns the image width.
The getter .dy from type
image returns the image height.
The image dimensions can also be printed out by the
println statement.
The type sprite is similar to the type
image with a few extras: a sprite has its own
size and center.
The initializer of type sprite accepts an image.
The dimensions of the sprite are set to equal the dimensions of the image by default.
The member .size from type sprite
contains the dimensions of the sprite. This member is both getter and setter; changing this
member will change the size of the sprite. This member is of type vector2D.
The member function .draw from type sprite
can accept 1, 2 or 3 arguments. The first argument is the position of the sprite.
The other arguments will be explained on the next page.
The member .image from type sprite
contains the original image.
As mentioned on the previous page, the member function
.draw from type sprite
can have 1, 2 or 3 arguments.
The second argument is scale factor, used for zooming in and out.
This argument is of type num.
The third argument is the angle in radians, used for rotating the image.
This argument is of type num.
Since the y-coordinate points downwards, the rotation is in the
clockwise direction when the angle has a positive value.
The member .center from type sprite
is of type point2D.
It specifies the center of the sprite, which affects the rotation and the positioning of the sprite.
The member function .draw from type sprite
always places the center point on the position given by the first argument.
The center points of the resulting images have been marked by red dots.
The member function .drawdg accepts the
angle in degrees instead of radians.
A full angle has 360 degrees.
Here is a rotating zombie mouse program.