|
楼主 |
发表于 2006-8-17 21:57
|
显示全部楼层
碰撞测试有进步了!
AS3.0 已很接近一种规范的语言了(网上有人说可以和JAVA平起平坐,甚至有些方面比JAVA还优……)
AS3.0里 hitTest 在 flash.Display.Shape类里(是Shape类的一个成员函数)
以下是摘自我手中的参考
hitTestObject () method
public function hitTestObject(obj:DisplayObject):Boolean
Evaluates the display object to see if it overlaps or intersects with the obj display object.
Parameters obj:DisplayObject — The display object to test against.
Returns Boolean — true if the display objects intersect; false if not.
Example
The following code creates three Shape objects and shows the result of calling the hitTestObject() method:
- import flash.display.Shape;
- var circle1:Shape = new Shape();
- circle1.graphics.beginFill(0x0000FF);
- circle1.graphics.drawCircle(40, 40, 40);
- addChild(circle1);
- var circle2:Shape = new Shape();
- circle2.graphics.beginFill(0x00FF00);
- circle2.graphics.drawCircle(40, 40, 40);
- circle2.x = 50;
- addChild(circle2);
- var circle3:Shape = new Shape();
- circle3.graphics.beginFill(0xFF0000);
- circle3.graphics.drawCircle(40, 40, 40);
- circle3.x = 100;
- circle3.y = 67;
- addChild(circle3);
- trace(circle1.hitTestObject(circle2)); // true
- trace(circle1.hitTestObject(circle3)); // true
- trace(circle2.hitTestObject(circle3)); // true
复制代码
hitTestPoint () method
public function hitTestPoint(x:Number, y:Number, shapeFlag:Boolean = false):Boolean
Evaluates the display object to see if it overlaps or intersects with the point specified by the x and y parameters. The x and y parameters specify a point in the coordinate space of the Stage, not the display object container that contains the display object (unless that display object container is the Stage).
Parameters x:Number — The x coordinate to test against this object.
y:Number — The y coordinate to test against this object.
shapeFlag:Boolean (default = false) — Whether to check against the actual pixels of the object (true) or the bounding box (false).
Returns Boolean — true if the display object overlaps or intersects with the specified point; false otherwise.
See also
opaqueBackground
Example
The following code creates a Shape object and shows the result of calling the hitTestPoint() method, using different points as parameters. The globalToLocal() method converts the point from Stage coordinates to the coordinate space of the shape:
- import flash.display.Shape;
- import flash.geom.Point;
- var circle:Shape = new Shape();
- circle.graphics.beginFill(0x0000FF);
- circle.graphics.drawCircle(40, 40, 40);
- circle.x = 10;
- addChild(circle);
- var point1:Point = new Point(0, 0);
- trace(circle.hitTestPoint(point1.x, point1.y, true)); // false
- trace(circle.hitTestPoint(point1.x, point1.y, false)); // false
- trace(circle.globalToLocal(point1)); // [x=-10, y=0]
- var point2:Point = new Point(10, 1);
- trace(circle.hitTestPoint(point2.x, point2.y, true)); // false
- trace(circle.hitTestPoint(point2.x, point2.y, false)); // true
- trace(circle.globalToLocal(point2)); // [x=0, y=1]
- var point3:Point = new Point(30, 20);
- trace(circle.hitTestPoint(point3.x, point3.y, true)); // true
- trace(circle.hitTestPoint(point3.x, point3.y, false)); // true
- trace(circle.globalToLocal(point3)); // [x=20, y=20]
复制代码
[ 本帖最后由 iptton 于 2006-8-19 12:53 编辑 ] |
|