|
image - image to test x - x position the image is drawn at y - y position the image is drawn at frame - frame of the image to test rect_x - x coordinate of the rectangle's top left corner rect_y - y coordinate of the rectangle's top left corner rect_width - width of the rectangle in pixels rect_height - height of the rectangle in pixels |
|
Returns True if an image touches a rectangle, testing pixel by pixel and ignoring masked pixels. Use it when the thing you are colliding against is not a sprite but an area: a hot spot in a point-and-click room, a doorway, a trigger zone, a lava strip, the bounds of an on-screen button. Only pixels that are not the image's mask colour can register a hit, so a pointer shaped like an arrow only fires where the arrow actually is. Pass the same x,y you would pass to DrawImage; the image's handle is taken into account. The rectangle is given as a corner plus a width and height, in the same coordinate space, and frame chooses which animation frame's shape to test. The pixel walk costs more than a plain rectangle test. If the image fills its frame, or you do not need that precision, ImageRectOverlap does the same job with just the bounding boxes and is much faster. Where you have many zones, test with the cheap version first and only call this one on the zone that matched. See also: ImageRectOverlap, ImagesCollide, ImagesOverlap, RectsOverlap, MaskImage. |
; ImageRectCollide Example
; ------------------------
Graphics 640,480,0,2
SetBuffer BackBuffer()
player=LoadImage("media/player.bmp")
px=100
py=100
While Not KeyDown(1)
Cls
; Arrow keys steer the ship
If KeyDown(203) Then px=px-3
If KeyDown(205) Then px=px+3
If KeyDown(200) Then py=py-3
If KeyDown(208) Then py=py+3
; ImageRectCollide is the pixel-perfect version of ImageRectOverlap:
; only the ship's SOLID pixels trigger a hit on the screen rectangle.
; Costs more than the rectangle-only test - use it when accuracy matters.
; The 0 is the animation frame of the image to test.
hit=ImageRectCollide(player,px,py,0,250,180,140,120)
; Draw the hot zone - red while solid ship pixels touch it
If hit Then
Color 255,60,60
Else
Color 80,200,80
End If
Rect 250,180,140,120,False
DrawImage player,px,py
Text 0,0,"Arrow keys: fly the ship into the zone Esc: exit"
Text 0,20,"ImageRectCollide(player,"+px+","+py+",0,250,180,140,120)="+hit
Flip
Wend
End