2012-07-19 92 views
0

我需要为舞台添加一个MovieClip,限制条件是它只能添加到舞台上的空白区域。舞台本身要么包含复杂的形状,要么可以由用户操作,即他可以拖动/移动物体来改变空白区域。 hitTest和hitTestObject方法需要DisplayObject已经在舞台上可用。什么是正确的路要走 - 我能想象的唯一解决方案是在舞台上添加了我的对象,然后重复进行点击测试?HitTest for still not on阶段

[想象一下,在视频游戏中添加小精灵 - 它们必须在空白区域产卵;如果他们彼此的内弹出,那么它会看起来非常奇怪。]

回答

1

那么,当你创建一个新的类,只需用一个变量将其关闭,并设置能见度为false,那么循环,直到有没有hitTest。

是一个愚蠢的例子:

public class someClass extends Sprite 
{ 
    private var objectsOnStage:Array; 
    public function someClass(objectsArray:Array) { 
     objectsOnStage = objectsArray; 
     visible = false; 
     addEventListener(Event.ADDED_TO_STAGE, init); 
    } 
    private function init(e:Event){ 
     removeEventListener(Event.ADDED_TO_STAGE, init); 
     addEventListener(Event.ENTER_FRAME, SEARCH); 
    } 
    private function SEARCH(e:Event) { 
     var doesHit:Boolean = false; 
     x = Math.round(Math.random() * (550 - 0)) + 0; 
     y = Math.round(Math.random() * (400 - 0)) + 0; 
     for (var i:int = 0; i < objectsOnStage; i++) { 
      if (doesHit) break; 
      if (this.hitTestObject(objectsOnStage[i])) { 
       doesHit = true; 
      } 
     } 
     if (doesHit) return; 
     placedInit(); 
    } 
    private function placedInit() { 
     visible = true; 
     removeEventListener(Event.ENTER_FRAME, SEARCH); 
     //now init the stuff you want. 
    } 
} 
0

你只要检查这两个剪辑的边界框重叠。就像这样:

import flash.geom.Rectangle; 
import flash.display.MovieClip; 

// create simple movie clips that has a rectangle shape inside 
var sym1 : MovieClip = new Sym1(); 
var sym2 : MovieClip = new Sym2(); 

// get a rectanle of both clipt 
var boundingBox1 : Rectangle = sym1.getBounds(this); 
var boundingBox2 : Rectangle = sym2.getBounds(this); 

// check if bounding boxes of both movie clips overlaps 
// so it works like hitTestObject() method 
trace(boundingBox1.intersects(boundingBox2)) 
0

我知道这个帖子是超级老,但在情况下,它可以帮助任何人 -

如果你需要做的命中测试中的MovieClip不是在舞台上。解决方法是首先将其栅格化为位图。

var bitmapData:BitmapData = new BitmapData(mc.width, mc.height, true, 0x0000000); 
bitmapData.draw(mc); 

if (bitmapData.getPixel32(x, y) > 0) { 
    // Hit true. 
}