zoukankan      html  css  js  c++  java
  • 物体的运动(三)

    大多数情况都是以一个矩形作为边界,当物体(圆形)运动到边界时,有四种处理方法:

    1.删除它

    2.将物体移到原来位置,就像生成一个新的物体。

    3.在屏幕上折回,就像相同的物体出现在不同的位置。

    4.弹回到原来的位置。

    第一种处理方法:

    判断圆的位置:

      从右边出界:ball.x-ball.radius>stage.stageWidth;

      从左边出界:ball.x+ball.radius<0;

      从上边出界:ball.y+ball.radius<0;

      从下边出界:ball.y-ball.radius>stage.stageHeight;

    只要满足上面一个条件即可以删除圆形,代码如下:

    private function onFrame(e:Event):void 
    		{
    			_circle.vy += _circle.ay;
    			_circle.vx += _circle.ax;
    			_circle.vy += _gravity;
    			_circle.vx += _wind;
    			_circle.vx *= _circle.friction;
    			_circle.vy *= _circle.friction;
    			_circle.x += _circle.vx;
    			_circle.y += _circle.vy;
    			_circle.rotation += _circle.vx;
    			
    			vTi.text = _circle.vx.toFixed(2);
    			aTi.text = _circle.vy.toFixed(2);
    			
    			setBounds();
    		}
    		
    		private function setBounds():void
    		{
    			if (_circle.x - _circle.width / 2 > stage.stageWidth ||
    				_circle.x + _circle.width / 2 < 0 ||
    				_circle.y + _circle.height / 2 < 0 ||
    				_circle.y - _circle.height / 2 > stage.stageHeight
    				)
    			{
    				removeEventListener(Event.ENTER_FRAME, onFrame);
    				stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyboardDownEventHandle);
    				stage.removeEventListener(KeyboardEvent.KEY_UP, keyboardUpEventHandle);
    				removeChild(_circle);
    				_circle = null;
    			}
    		}

    第二种处理方法:

    当物体出边界后,重新生成它。修改了setBounds()函数,重新设置了圆形的位置,初始速度。

    private function setBounds():void
    		{
    			if (_circle.x - _circle.width / 2 > stage.stageWidth ||
    				_circle.x + _circle.width / 2 < 0 ||
    				_circle.y + _circle.height / 2 < 0 ||
    				_circle.y - _circle.height / 2 > stage.stageHeight
    				)
    			{
    				
    				_circle.x = stage.stageWidth / 2 - _circle.width / 2;
    				_circle.y = stage.stageHeight / 2 - _circle.height / 2;
    				_circle.vx = Math.random() * 2 - 1;
    				_circle.vy = Math.random() * 2 - 1;
    			}
    		}
    
    

    第三种处理方法:

    屏幕折回相当于,物体从边界的一边移出后,出现到相对此边界的另一边同一位置。

    private function setBounds():void
    		{
    			var left:Number = 0;
    			var right:Number = stage.stageWidth;
    			var top:Number = 0;
    			var bottom:Number = stage.stageHeight;
    			
    			if (_circle.x - _circle.width / 2 > right)
    			{
    				_circle.x = left - _circle.width / 2;
    			}
    			else if (_circle.x + _circle.width / 2 < left)
    			{
    				_circle.x = right + _circle.width / 2;
    			}
    			if (_circle.y + _circle.height / 2 < top)
    			{
    				_circle.y = bottom + _circle.height / 2;
    			}
    			else if (_circle.y - _circle.height / 2 > bottom)
    			{
    				_circle.y = top - _circle.height / 2;
    			}
    		}
    
    

    第四种处理方法:

    回弹处理分为三步:1.检测物体是否超过边界;2.如果超过将其放到边界的边上;3.反转物体的速度(改变速度方向),代码如下:

    private function setBounds():void
    		{
    			var left:Number = 0;
    			var right:Number = stage.stageWidth;
    			var top:Number = 0;
    			var bottom:Number = stage.stageHeight;
    			
    			if (_circle.x + _circle.width / 2 > right)
    			{
    				_circle.x = right - _circle.width / 2;
    				_circle.vx *= -1;
    			}
    			else if (_circle.x - _circle.width / 2 < left)
    			{
    				_circle.x = left + _circle.width / 2;
    				_circle.vx *= -1;
    			}
    			if (_circle.y - _circle.height / 2 < top)
    			{
    				_circle.y = top + _circle.height / 2;
    				_circle.vy *= -1;
    				
    			}
    			else if (_circle.y + _circle.height / 2 > bottom)
    			{
    				_circle.y = bottom - _circle.height / 2;
    				_circle.vy *= -1;
    			}
    		}
    
    

    代码执行的时候,会看到圆形碰到边界时可能会与边界有一定距离。这是由于圆形在选择一定的角度后,flash计算它的宽度与高度会有不同了,如果直接上面的_circle.width/2,_circle.height/2变成一个常数,即圆的半径,那么圆形就会按照我们预想的那样靠近边界了。

  • 相关阅读:
    appium+python自动化测试之webview的处理。
    Mysql为什么要使用视图
    show status 查看各种状态
    MySQL show processlist
    Java爬虫系列(五)
    div p、div>p、div+p、div~p、div.a 、p,span的用法和区别
    Spring Boot 定时任务 -- @Scheduled
    设计模式
    12月15日总结
    成员变量和静态变量的区别
  • 原文地址:https://www.cnblogs.com/ywxgod/p/1708898.html
Copyright © 2011-2022 走看看