ムービー クリップがあり、そのムービー クリップにはさらにいくつかのムービー クリップ (四角形) があります...すべての四角形との衝突を、すべてのコードを記述せずに検出するにはどうすればよいですか?
1 に答える
1
次のように、任意の DisplayObjectContainer の子を反復処理できます。
for( var i:int = 0; i < clip.numChildren; i++)
doStuffWith( clip.getChildAt(i) );
子オブジェクトとの衝突をテストするには、これを使用できます。
function hitTestChildren( target:DisplayObject, parent:DisplayObjectContainer ):Boolean {
for( var i:int = 0; i< parent.numChildren; i++)
if( target.hitTestObject( parent.getChildAt(i))) return true;
return false;
}
編集
ソース FLA を確認した後、重力シミュレーションに衝突を含める方法は次のとおりです。
function hitTestY( target:DisplayObject, container:DisplayObjectContainer ):Number {
//iterate over all the child objects of the container (i.e. the "parent")
for( var i:int = 0; i< container.numChildren; i++) {
var mc:DisplayObject = container.getChildAt(i)
// Test for collisions
// There were some glitches with Shapes, so we only test Sprites and MovieClips
if( mc is Sprite && target.hitTestObject( mc ))
// return the global y-coordinate (relative to the stage) of the object that was hit
return container.localToGlobal( new Point(mc.x, mc.y ) ).y;
}
// if nothing was hit, just return the target's original y-coordinate
return target.y;
}
function onEnter(evt:Event):void {
vy += ay;
vx += ax;
vx *= friction;
vy *= friction;
ball.x += vx;
ball.y += vy;
ball.y = hitTestY( ball, platform_mc);
}
ただし、それぞれの原点が形状の中央ではなく 0,0 になるように、四角形オブジェクトを変更する必要があります。
編集終了
ただし、単純な長方形の形状ではなく、複雑な形状や透明度を扱っている場合は、別のアプローチを使用することをお勧めします。形状の任意の組み合わせをビットマップに描画し、 を使用BitmapData#hitTest
してそれらが交差するかどうかを確認できます (この例ではこの場合、parent
すべての子とtarget
クリップ自体を含む全体でそれを行いますが、個々の子では行いません)。
コードは投稿しませんが (長くなっています)、 Mike Chambers のブログに、これを行う方法のきれいな例があります。
于 2013-02-24T12:01:07.667 に答える