flash中物体运动基础之五---------障碍物

障碍物有的可以运动有的不能运动,如:可以把墙壁看出不能运动的障碍物,移动的另一个球体看出运动的障碍物。运动过程中要检测球体是否与障碍物接触,可以用hitTestObject方法或者hitTestPoint方法。

接着第四部分,修改update函数。在舞台上增加了obstruction1,obstruction2,obstruction3。

obstruction1为上下运动的矩形
obstruction2为舞台下方的浅蓝色矩形,模拟液体物质。
obstruction3为转动的矩形

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
private function update(e:Event):void
        {
            hitTestObstruction3();
            if (
                wallLeft.hitTestObject(_ball) ||
                wallRight.hitTestObject(_ball) ||
                wallTop.hitTestObject(_ball)||
                wallBottom.hitTestObject(_ball) ||
                obstruction1.hitTestObject(_ball)
            )
            {
                _ball.reset(_ballInitX, _ballInitY);
                _score-= 2;
            }
             
            hitTestCoin();
            hitTestObstruction1();
            hitTestObstruction2();
            updateScore();
        }
         
        private function updateScore():void
        {
            scoreText.text = _score.toString();
        }
         
        private function hitTestObstruction3():void
        {
            obstruction3.rotation += 0.5;
             
            if (obstruction3.hitTestPoint(_ball.x,_ball.y,true))
            {
                _ball.reset(_ballInitX, _ballInitY);
                _score-= 2;
            }
        }
         
        private function hitTestObstruction2():void
        {
            if (obstruction2.hitTestObject(_ball))
            {
                _ball.vx = _ball.vx / 1.2;
                _ball.vy = _ball.vy / 1.2;
            }
        }
         
        private function hitTestCoin():void
        {
            _right = wallRight.x - wallRight.width / 2;
            _top = wallTop.y + wallTop.height / 2;
            _left = wallLeft.x + wallLeft.width / 2;
            _bottom = wallBottom.y - wallBottom.height / 2;
             
            var dx:Number = (_ball.x-coin.x) * (_ball.x-coin.x);
            var dy:Number = (_ball.y-coin.y) * (_ball.y-coin.y);
            var dist:Number = (_ball.width / 2 + coin.width / 2) * (_ball.width/2+coin.width/2);
            if (dx+dy<=dist)
            {
                coin.x = Math.random() * (_right-2*coin.width/2 - _left) + _left+coin.width/2;
                coin.y = Math.random() * (_bottom - 2 * coin.height / 2 - _top) + _top + coin.height / 2;
                _score += 3;
            }
            if (obstruction1.hitTestObject(coin))
            {
                coin.x = Math.random() * (_right-2*coin.width/2 - _left) + _left+coin.width/2;
                coin.y = Math.random() * (_bottom - 2 * coin.height / 2 - _top) + _top + coin.height / 2;
            }
        }
         
        private function hitTestObstruction1():void
        {
            var top:Number = _top + obstruction1.height / 2;
            var bottom:Number = _bottom - obstruction1.height / 2;
            var dist:Number = bottom - top;
            obstruction1.y = stage.stageHeight/2+dist/2 * Math.sin(t);
            t += 0.05;
        }


 由于使用的是hitTestPoint和hitTestObject,限制很大,且导致检测碰撞不精确,如果要更精确的碰撞检测可以尝试使用Box2dAs3来改写,以后有时间再来改吧,这里只是记录一些简单的方式检测的代码。

posted @   ywxgod  阅读(473)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示