`

AS3 碰撞类

阅读更多

 

package {

	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.display.Sprite;

	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class HitTest {

		public static function complexHitTestObject( target1:DisplayObject, target2:DisplayObject,  accurracy:Number = 1 ):Boolean {
			return complexIntersectionRectangle( target1, target2, accurracy ).width != 0;
		}

		public static function intersectionRectangle( target1:DisplayObject, target2:DisplayObject ):Rectangle {
			// If either of the items don't have a reference to stage, then they are not in a display list
			// or if a simple hitTestObject is false, they cannot be intersecting.
			if (! target1.root || ! target2.root || ! target1.hitTestObject(target2)) {
				return new Rectangle();
			}

			// Get the bounds of each DisplayObject.
			var bounds1:Rectangle = target1.getBounds(target1.root);
			var bounds2:Rectangle = target2.getBounds(target2.root);

			// Determine test area boundaries.
			var intersection:Rectangle = new Rectangle();
			intersection.x = Math.max(bounds1.x,bounds2.x);
			intersection.y = Math.max(bounds1.y,bounds2.y);
			intersection.width      = Math.min( ( bounds1.x + bounds1.width ) - intersection.x, ( bounds2.x + bounds2.width ) - intersection.x );
			intersection.height = Math.min( ( bounds1.y + bounds1.height ) - intersection.y, ( bounds2.y + bounds2.height ) - intersection.y );

			return intersection;
		}

		public static function complexIntersectionRectangle( target1:DisplayObject, target2:DisplayObject, accurracy:Number = 1 ):Rectangle {
			if (accurracy <= 0) {
				throw new Error("ArgumentError: Error #5001: Invalid value for accurracy",5001);
			}

			// If a simple hitTestObject is false, they cannot be intersecting.
			if (! target1.hitTestObject(target2)) {
				return new Rectangle();
			}

			var hitRectangle:Rectangle = intersectionRectangle(target1,target2);
			// If their boundaries are no interesecting, they cannot be intersecting.
			if (hitRectangle.width * accurracy < 1 || hitRectangle.height * accurracy < 1) {
				return new Rectangle();
			}

			var bitmapData:BitmapData = new BitmapData(hitRectangle.width * accurracy,hitRectangle.height * accurracy,false,0x000000);

			// Draw the first target.
			bitmapData.draw( target1, HitTest.getDrawMatrix( target1, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, -255, -255, 255 ) );
			// Overlay the second target.
			bitmapData.draw( target2, HitTest.getDrawMatrix( target2, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, 255, 255, 255 ), BlendMode.DIFFERENCE );

			// Find the intersection.
			var intersection:Rectangle = bitmapData.getColorBoundsRect(0xFFFFFFFF,0xFF00FFFF);

			bitmapData.dispose();

			// Alter width and positions to compensate for accurracy
			if (accurracy != 1) {
				intersection.x /=  accurracy;
				intersection.y /=  accurracy;
				intersection.width /=  accurracy;
				intersection.height /=  accurracy;
			}

			intersection.x +=  hitRectangle.x;
			intersection.y +=  hitRectangle.y;

			return intersection;
		}


		protected static function getDrawMatrix( target:DisplayObject, hitRectangle:Rectangle, accurracy:Number ):Matrix {
			var localToGlobal:Point;;
			var matrix:Matrix;

			var rootConcatenatedMatrix:Matrix = target.root.transform.concatenatedMatrix;

			localToGlobal = target.localToGlobal( new Point( ) );
			matrix = target.transform.concatenatedMatrix;
			matrix.tx = localToGlobal.x - hitRectangle.x;
			matrix.ty = localToGlobal.y - hitRectangle.y;

			matrix.a = matrix.a / rootConcatenatedMatrix.a;
			matrix.d = matrix.d / rootConcatenatedMatrix.d;
			if (accurracy != 1) {
				matrix.scale( accurracy, accurracy );
			}

			return matrix;
		}

	}

}
分享到:
评论

相关推荐

    as3碰撞类精确到象素

    这是基于as3的碰撞类,大家可以试一下 ,好用的多,可以精确到象素!

    flash AS3.0检测碰撞工具类 超好用

    本篇文章将详细介绍AS3.0中的碰撞检测工具类以及如何使用它们。 首先,AS3.0提供了基本的几何形状类,如Rectangle和Circle,这些类可以用来进行简单的碰撞检测。例如,你可以比较两个Rectangle对象的x、y坐标和宽度...

    AS3 格子方法检测碰撞的代码

    在ActionScript 3 (AS3)中,进行游戏开发或者物理模拟时,高效的碰撞检测是必不可少的。"AS3 格子方法检测碰撞的代码" 提供了一种使用格子(Grid)数据结构来优化碰撞检测的方法。这种方法通常称为“空间分区”或...

    as3.0实现的一个碰撞类小游戏

    在这个“as3.0实现的一个碰撞类小游戏”中,我们将探讨AS3.0的核心特性以及如何利用这些特性来构建碰撞检测的游戏机制。 在AS3.0中,游戏开发的基础是事件驱动模型。游戏循环通常由一个主循环函数(如`enterFrame`...

    as3 碰撞检测的代码,按形状检测

    as3 碰撞检测的代码,按形状检测 精度很高 自带的碰撞有bug

    as 3.0拖动碰撞外部类

    这是一个自定义的as3.0拖动碰撞外部类,用法是直接将外部类链接到需要拖动碰撞的影片剪辑上,然后再舞台上将被碰撞的影片剪辑实例名传给外部类

    Flash as3 多个小球碰撞回弹

    在本文中,我们将深入探讨如何使用Flash ActionScript 3(AS3)实现多个小球之间的碰撞检测和回弹效果。ActionScript是Adobe Flash Professional和Flex Builder等开发工具中用于创建交互式内容的主要编程语言,而AS3...

    as3 flash检验碰撞

    在AS3中,你可以通过`Rectangle`类的`intersects()`方法来实现这个功能。例如: ```actionscript var object1Bounds:Rectangle = new Rectangle(object1.x, object1.y, object1.width, object1.height); var object...

    AS3中的物理学 常见的碰撞,弹性,边界,摩擦力,加速,缓动。。。

    为了实现这些物理特性,开发者可能需要使用到AS3的标准库,如Flash的DisplayObject类提供的方法,或者第三方的物理引擎,如Pbox2D或Flixel的物理系统。这些库可以帮助简化物理计算,提供预设的行为,并优化性能。 ...

    as3写的粒子与贝塞尔曲线碰撞

    在AS3中,我们可以创建一个粒子类,包含粒子的位置、速度、生命周期等属性,并通过更新这些属性来模拟粒子的行为。 对于贝塞尔曲线,它是计算机图形学中常用的平滑曲线,由控制点定义。在AS3中,我们可以使用`flash...

    AS3.0像素级别精确检测碰撞

    总之,AS3.0的像素级别精确碰撞检测是一个高级技术,通过BitmapData类和自定义扩展类,我们可以实现更精确的碰撞效果。"BitmapHitTestPlus.as"文件很可能提供了这样一个自定义的解决方案,可以灵活适应各种场景的...

    AS模拟小球的斜面碰撞

    "AS模拟小球的斜面碰撞"这个主题聚焦于如何使用ActionScript(AS)语言来创建一个动态系统,该系统能够精确地模拟小球与斜面之间的碰撞。ActionScript是Adobe Flash Player支持的一种脚本语言,常用于开发交互式...

    [转] 斜面碰撞 as3模拟

    【标题】:“斜面碰撞 AS3 模拟”是一篇关于使用ActionScript 3(AS3)编程语言实现物理世界中的斜面碰撞效果的技术分享。该主题主要关注游戏开发或互动媒体应用中常见的一种物理模拟技术,即物体在斜坡上滚动、滑动...

    AS3.0 像素级别精确检测碰撞

    AS3.0 检测碰撞 像素级别精确检测碰撞,里面函数都是静态的,使用非常简单!

    Hittest As3.0碰撞检测

    在ActionScript 3.0(AS3.0)中,Hittest是用于实现对象间碰撞检测的核心技术。这篇由国外大师编写的关于“Hittest As3.0碰撞检测”的资源,提供了一种静态且高效的解决方案,适用于那些需要在游戏中或者交互式应用...

    as3 开发中球的碰撞检测

    利用mouse拖动,实现球之间的碰撞检测

    Flash as3基于位图的碰撞检测实例

    在AS3中,我们可以利用BitmapData类和它的相关方法来完成这一过程。BitmapData对象代表了一个位图,包含了图像的所有像素信息。以下是一些关键的步骤和知识点: 1. **创建BitmapData对象**:首先,我们需要将显示...

    flash as3.0碰撞测试代码

    碰撞测试,打气球游戏,当光标接触气球时候,气球就消失

    as3精确检测类

    这是个as3的工具类,用于实现检测2个不规则对象,是否有碰撞。 用法:var res:Boolean=BitmapHitTest.test( mc1,mc2); trace("mc1与mc2碰撞情况为:"+res);

    as3比较详尽的工具类

    hitTestObjects.as ---------------------- 像素级碰撞检测 LoaderSprite.as ---------------------- 处理Loader相关的Sprite Registration.as ---------------------- 变换注册点 SubMovieClip.as ------------...

Global site tag (gtag.js) - Google Analytics