Extensions Test Rectangle

boolean object.testRect ( number x, number y, number w, number h, [number layer [, number xoff [, number yoff] ] ] )

Tests if the object is currently overlapping a specific rectangular region in the frame.

If a layer is specified, the test is limited to instances on that layer.  By default, layer is equal to mmf.ANY_LAYER.  If the xoff or yoff parameters are specified, the test is made as if the object had first been moved by those amounts (but without actually moving the object).

Parameters

x X coordinate of left edge of rectangle in the frame
y Y coordinate of top edge of rectangle in the frame
w Width of rectangular region to test
h Height of rectangular region to test
layer Optional.  1-based layer index, or constants mmf.ANY_LAYER, mmf.SAME_LAYER, or mmf.DIFF_LAYER 
xoff Optional.  X coordinate offset to shift object before making the test
yoff Optional.  Y coordinate offset to shift object before making the test

Return Values

boolean true if object is overlapping the specified rectangular region, false otherwise

Example

-- Assume we have a goomba object
-- If goomba tries to walk off the edge of the frame, turn around
local x = mmf.frame.xRight
local y = mmf.frame.yTop
local w = 2
local h = mmf.frame.yBottom
 
if (goomba.testRect(x, y, w, h) then
  goomba.x = goomba.x - 1
  goomba.direction = mmf.DIR_W
end

Copyright 2010 Justin Aquadro