LDEasyBox2D 3.0

运行时创建多边形刚体教程中,我们学会了创建多边形刚体,又在[Box2D]多边形刚体贴图中学习了如何为多边形刚体贴图,不过多边形刚体创建和贴图的过程还是稍有复杂,所以我把它集成到了LDEasyBox2D.createPolygon()方法中。

下面我们来看一下LDEasyBox2D.createPolygon()的用法

		/**
		 * 根据一组顶点数据,创建多边形刚体,可以是顺时针绘制,也可以逆时针绘制,但不能出现交叉
		 * @param	world Box2D世界
		 * @param	vertices 顶点数组,顶点之间不能有交叉
		 * @param	isStatic	是否为静止的刚体
		 * @param	fillData	刚体的填充纹理,一个BItmapData对象,请确保整个BitmapData的尺寸大于舞台的尺寸
		 * @param	stage	添加userData的舞台
		 * @return 返回一个多边形刚体
		 */
		public static function createPolygon(
				world:b2World,
				vertices:Vector.<b2Vec2>,
				isStatic:Boolean = false,
				fillData:BitmapData = null,
				stage:DisplayObjectContainer = null
		):b2Body

我用LDEasyBox2D创建了[Box2D]多边形刚体贴图中的实例,效果是一样的,但是代码精简了很多。

完整代码和注释:

package
{

	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2World;

	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	/**
	 * http://www.ladeng6666.com
	 * @author ladeng6666
	 */
	public class MainWithSeparatoEasy extends Sprite
	{
		private const segmentLength:Number = 20;

		private var world:b2World;
		//绘制图形的画布
		private var spriteCanvas:Sprite;
		//绘制图像的纹理
		private var woodBmd:BitmapData = new Wood();

		private var prePoint:Point = new Point();
		private var curPoint:Point = new Point();

		private var verticesList:Vector.<b2Vec2> = new Vector.<b2Vec2>();

		private var isDrawing:Boolean = false;
		public function MainWithSeparatoEasy()
		{
			//创建box2D世界
			world = LDEasyBox2D.createWorld();
			//创建box2D调试图
			addChild(LDEasyBox2D.createDebug(world));
			//创建地面
			LDEasyBox2D.createWrapWall(world,stage);

			spriteCanvas = new Sprite();
			addChild(spriteCanvas);
			//侦听事件
			addEventListener(Event.ENTER_FRAME, loop);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
		}

		private function onStageMouseMove(e:MouseEvent):void
		{
			//如果鼠标没有按下,isDrawing为false,跳出
			if(!isDrawing) return;
			spriteCanvas.graphics.lineTo(mouseX, mouseY);
			//记录鼠标坐标为当前curPoint,并计算curPoint与上一个点prePoint的距离
			curPoint = new Point(mouseX, mouseY);
			var distance:Number = Point.distance(prePoint, curPoint);
			//当前后两个点的距离大于线段距离时,添加顶点
			if (distance >= segmentLength) {
				//记录顶点到verticesList数组中
				verticesList.push(new b2Vec2(mouseX / 30, mouseY / 30));
				prePoint = curPoint.clone();
			}
		}

		private function onStageMouseUp(e:MouseEvent):void
		{
			//鼠标弹起后,停止绘制
			isDrawing = false;
			//画布里的内容
			spriteCanvas.graphics.clear();
			//在鼠标位置随机创建一个多边形刚体
			//之前的createPolygon和createUseData被集成到了LDEasyBox2D.createPolygon
			LDEasyBox2D.createPolygon(world, verticesList, false,woodBmd,stage);

			//清空存储顶点的Vector数组
			verticesList = new Vector.<b2Vec2>();
		}
		/**
		 * 鼠标按下事件侦听
		 * @param	e
		 */
		private function onStageMouseDown(e:MouseEvent):void
		{
			//鼠标按下后,开始绘制
			isDrawing = true;
			//设置线条样式
			spriteCanvas.graphics.lineStyle(2);
			spriteCanvas.graphics.moveTo(mouseX, mouseY);

			//定义鼠标点为起点
			curPoint = new Point(mouseX, mouseY);
			prePoint = curPoint.clone();

			verticesList.push(new b2Vec2(mouseX / 30, mouseY / 30));
		}
		/**
		 * 刷新屏幕
		 * @param	e
		 */
		private function loop(e:Event):void
		{
			LDEasyBox2D.updateWorld(world);
		}
	}

}

 源码下载地址

联系作者

公众号:拉小登 | 微博:拉登Dony | B站:拉小登Excel

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注