zoukankan      html  css  js  c++  java
  • flash中物体运动基础之五障碍物

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

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

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

    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来改写,以后有时间再来改吧,这里只是记录一些简单的方式检测的代码。

    作者:ywxgod
    E-mail:给我发邮件
    出处:http://ywxgod.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Ansible 日常使用技巧
    Linux下科学计数法(e)转化为数字的方法 [shell中几种数字计算说明]
    业务日志清理脚本
    Kubernetes容器集群
    Kubernetes 之Pod学习
    数据结构之数组
    Java Class 文件中Method的存储
    理解Flink Transformation
    理解Java BlockingQueue
    理解Java FutureTask
  • 原文地址:https://www.cnblogs.com/ywxgod/p/1786509.html
Copyright © 2011-2022 走看看