text stringlengths 0 897 |
|---|
display(4).sprites.sort "sortOrder" |
``` |
Now run again, and move our moving Stampy around near the stationary one. You should observe that whichever sprite is higher on the screen is now drawn behind, and whichever one is lower on the screen is drawn in front. This creates the illusion that the sprites are moving in 3D space. |
{width:"50%"} |
 |
Before we move on, save this program if you haven't already — I called it "StampyDemo", but you can call it whatever you like. We will use it again later. |
## Hit Detection |
The last topic of this chapter is about detecting when something is touching a sprite. When that something is the mouse, this is called *hit detection*; when it's another sprite, it's more commonly called *collision detection*. |
hit detection |
: determining whether a point on the screen (usually, the mouse) is touching a sprite |
collision detection |
: determining whether two sprites are touching |
{i:"`Bounds` class"} |
PRINT>Both of these tasks are done in Mini Micro with the help of another built-in class called `Bounds`. `Bounds` has the properties listed below, and the methods at the top of the next page. |
EBOOK>Both of these tasks are done in Mini Micro with the help of another built-in class called `Bounds`. `Bounds` has the properties listed below. |
{caption:"Properties of the Bounds class (where `b` is any Bounds object). All of these may be written (i.e. assigned to) as well as read."} |
|`b.x`|horizontal position of the center of the bounding box| |
|`b.y`|vertical position of the center of the bounding box| |
|`b.width`|bounding box width| |
|`b.height`|bounding box height |
|`b.rotation`|rotation angle of the bounding box, in degrees| |
EBOOK>`Bounds` also has the following three methods. |
{caption:"Methods of the Bounds class (where `b` is any Bounds object)."} |
|`b.corners`|returns box corners as a list of [x,y] coordinates| |
|`b.overlaps(`*b2*`)`|returns whether `b` is touching box *b2*| |
|`b.contains(`*x*`, `*y*`)`|return true if point *x, y* is within box `b`| |
You can use `Bounds` by itself, just to define a rectangular (possibly rotated) area and see whether any point is inside or outside that area. But the more usual way to use it is via the `Sprite` class. `Sprite` has special support for a Bounds object called `localBounds`. Once you assign and configure the `localBou... |
Some examples will make this clearer. Let's create a fresh sprite, using our old friend the Wumpus. |
```miniscript |
clear |
spr = new Sprite |
spr.image = file.loadImage("/sys/pics/Wumpus.png") |
display(4).sprites.push spr |
``` |
You should see the Wumpus once again peeking out from the lower-left corner. If you look at `spr.localBounds`, you will see it is null. Sprites do not automatically get any bounding box defined. But let's define one: |
```miniscript |
spr.localBounds = new Bounds |
spr.localBounds.width = spr.image.width |
spr.localBounds.height = spr.image.height |
``` |
Now we've created a `Bounds` object, assigned it to the special `localBounds` property of our Sprite, and then set its width and height to match the image. We didn't assign to the `x` or `y` properties of the bounds, so they are implicitly `0,0`, which is the center of the sprite. |
That's all the setup you need to do to enable the `contains` and `overlaps` methods of Sprite to work. For example, try this code to make the Wumpus bashful and jump away from the mouse. |
{caption:Bashful Wumpus doesn't want to be clicked on!} |
```miniscript |
while true |
if spr.contains(mouse) then |
spr.x = 960*rnd |
spr.y = 640*rnd |
end if |
end while |
``` |
Type that in and then see if you can click on the Wumpus. Slippery little monster, isn't he? |
Because this is such a common and useful task, let's summarize. |
D> **Hit-Testing Summary** |
D> 1. Set the sprite's `localBounds` to define the bounding box relative to the sprite itself. |
D> 2. Call `contains` on the sprite with a point of interest. |
Note that the `contains` method actually allows several different parameter styles for defining the point of interest: |
- give it the *x* and *y* coordinates as two separate numeric parameters; or |
- give it a list containing [*x*, *y*]; or |
- give it a map containing "x" and "y" keys. |
In the example above, we gave it `mouse`, which happens to be a map containing *x* and *y*, so we were using the third style. |
Finally, let's consider collision-testing. This is what we use to tell if two sprites are touching each other. You set this up exactly the same way, except that there are two sprites involved instead of one; and then you use the `overlaps` method to test whether they are touching. |
To illustrate, please load up that Stampy demo you made in the previous section. We're going to modify it to tint the moving sprite whenever it touches the stationary one. First, where we are defining the `Stamper` class, we need to add a `localBounds`. |
{number-from: 9} |
```miniscript |
Stamper.localBounds = new Bounds |
Stamper.localBounds.width = 128 |
Stamper.localBounds.height = 128 |
``` |
Then, let's add a special version of the `update` method just for `stampy`, our moving Stamper. It will begin by calling the superclass version of `update`, so that it does whatever all Stampers do; but it will then do the `overlaps` test against `stampy2`, and blush accordingly. |
{number-from: 34} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.