1 package
2 {
3
4 import flash.display.Sprite;
5 import flash.events.MouseEvent;
6 import ws.tink.display.HitTest;
7 import flash.geom.Rectangle;
8
9 public class ComplexIntersectionRectangle extends Sprite
10 {
11
12 private var _circle0 : Sprite;
13 private var _circle1 : Sprite;
14 private var _hit : Boolean;
15 private var _intersection : Sprite;
16
17 public function ComplexIntersectionRectangle()
18 {
19 super();
20
21 initialize();
22 }
23
24 private function initialize():void
25 {
26
27 _circle0 = new Sprite();
28 _circle0.x = 50;
29 _circle0.y = 100;
30 _circle0.addEventListener( MouseEvent.MOUSE_DOWN, onCircleMouseDown );
31 draw( _circle0, 0xFFFFFF );
32 addChild( _circle0 );
33
34 _circle1 = new Sprite();
35 _circle1.x = 250;
36 _circle1.y = 100;
37 _circle1.addEventListener( MouseEvent.MOUSE_DOWN, onCircleMouseDown );
38 draw( _circle1, 0xFFFFFF );
39 addChild( _circle1 );
40
41 _intersection = new Sprite();
42 addChild( _intersection );
43
44 _hit = false;
45
46 stage.addEventListener( MouseEvent.MOUSE_MOVE, onStageMouseMove );
47 stage.addEventListener( MouseEvent.MOUSE_UP, onStageMouseUp );
48 }
49
50 private function onCircleMouseDown( event:MouseEvent ):void
51 {
52 var circle:Sprite = Sprite( event.target );
53 circle.startDrag( false );
54 }
55
56 private function onStageMouseMove( event:MouseEvent ):void
57 {
58 var intersection:Rectangle = HitTest.complexIntersectionRectangle( _circle0, _circle1, 5 );
59 var hit:Boolean = ( intersection.width > 0 && intersection.height > 0 );
60
61 _intersection.graphics.clear();
62 if( hit )
63 {
64 _intersection.graphics.beginFill( 0xFF0000, 0.5 );
65 _intersection.graphics.drawRect( intersection.x, intersection.y, intersection.width, intersection.height );
66 _intersection.graphics.endFill();
67 }
68
69 if( hit != _hit )
70 {
71 _hit = hit;
72 var color:Number = ( _hit ) ? 0x00FF00 : 0xFFFFFF;
73 draw( _circle0, color );
74 draw( _circle1, color );
75 }
76 }
77
78 private function onStageMouseUp( event:MouseEvent ):void
79 {
80 _circle0.stopDrag();
81 _circle1.stopDrag();
82 }
83
84 private function draw( circle:Sprite, color:Number ):void
85 {
86 circle.graphics.clear();
87 circle.graphics.beginFill( color, 1 )
88 circle.graphics.drawCircle( 0, 0, 25 );
89 circle.graphics.endFill();
90 }
91 }
92 }
as3碰撞检测(两图重叠区)(例子)
最新推荐文章于 2020-11-27 23:35:45 发布