I have been having no end of trouble with Flash CS3 lately. I really need some help clearing the scripting.
I've been trying to make a code in actionscript that will set a function to true
if an object is moved to a specific part of the screen. The confusing thing is making the rule apply to when it's between a specific four coordinates (in this case, 165 and
231 as the X coordinates, and 295 and 330 as the y coordinates; "honey" is the instance name of the symbol).
The last time I asked for help, I was given this code:
bool IsBetweenInclusive(int value, int lower, int upper)
{
return value >= lower
&& value <= upper;
}
However, when I tried to turn it into a condition statement, it woul开发者_C百科dn't work. I really need help, and will really appreciate assistance.
/**
* Whether a point is within a rectangular bounds.
*
* @param x Point x-coordinate to be tested.
* @param y Point y-coordinate to be tested.
* @param bounds Rectangle boundary to be tested against.
* @return True if point is within bounds.
*/
public static function withinBounds(x:Number, y:Number, bounds:Rectangle):Boolean
{
if ((x > bounds.x) &&
(x < bounds.x + bounds.width) &&
(y > bounds.y) &&
(y < bounds.y + bounds.height))
return true;
return false;
}
You can use a rectangle, it already has a method for that. Assuming you have an the upper left point x1,y1
and the bottom right point x2,y2
and the point x,y
you want to check:
var area = new Rectangle (x1, y1, x2-x1, y2-y1);
if( area.contains(x,y) ) { /* ... */ }
There's also containsRect if you need to check if an object is completely in the given area.
精彩评论