2013-03-31 154 views
0

好了,我有闪光游戏,平台game.In这个游戏,你必须跳过去spikes.So创建秒杀并将其转换为一个符号,一个电影clip.When它注册作为一个电影剪辑,它不是一个三角形(如尖峰)而是一个rectangle.This当玩家去避免尖峰和跳跃,如果他是太接近他死意味着什么,但他不打钉,他击中了钉子周围的不可见矩形。无论如何,要改变影片剪辑的形状,使其仅适用于钉和钉。调整大小影片剪辑AS3

回答

0

从我的经验处理同一类型的问题的一个例子,你不能真正使用AS3 hitTestObject(我会想你”重新使用,如果你有这个问题)与MC的特定形状。它始终默认使用占用movieclip本身空间的框。

所以要回答你的问题,不,你不能改变MC的形状,使其只有秒杀,默认hitTestObject使用围绕MC的边界框。 (很多人会建议不要使用hitTestOjbect,因为它不是很高效,而是编写自己的测试代码,我建议寻找这个在线的例子,这里有很多他们)。

你的其他选择,如果你不希望处理的是,将创建一个单独的系列箱形MC的充当您的边框,可以布置成阵列,然后就贯穿看到一个循环如果你的角色击中任何一个。这样,您可以根据自己的需要调整边界框的大小。

但是,如果你想使用hitTestObject,不幸的是你必须使用整个对象边框。由于我不完全确定你如何处理这个问题的细节,所以我最好的建议是上网阅读AS3中的点击检测的基础知识。

你的问题是与碰撞检测,不一定影片剪辑。

0

对象之间的内部命中测试将检查对象的边框,这样不会为你工作。

如果你能以某种方式使用播放器代理作为点(最低点,喜欢他的脚或类似的东西的中间),你可以使用spike.hitTestPoint(globalFootX, globalFootY, true);

如果不工作,你要么必须手动创建测试表示物品并且做你自己的hittest逻辑。

另一解决方案是绘制的项目一个单独的子画面,然后查看是否像素重叠。我知道我在一个旧的AS2项目中做过这个项目,这个项目里有不规则形状的AI机器人,在一个形状不规则的世界里移动。

我会提供该代码以供参考,它可能会转换为AS3,或者至少您可以使用它作为灵感来寻找谷歌为了在AS3中找到该解决方案的内容。

class messer_studios.utils.CollisionDetection { 
    static public function checkForCollision(p_clip1:MovieClip, p_clip2:MovieClip, p_alphaTolerance:Number):Rectangle {  
     // set up default params: 
     if (p_alphaTolerance == undefined) { 
      p_alphaTolerance = 255; 
     } 

     // get bounds: 
     var bounds1:Object = p_clip1.getBounds(_root); 
     var bounds2:Object = p_clip2.getBounds(_root); 

     // rule out anything that we know can't collide: 
     if (((bounds1.xMax < bounds2.xMin) || (bounds2.xMax < bounds1.xMin)) || ((bounds1.yMax < bounds2.yMin) || (bounds2.yMax < bounds1.yMin))) { 
      return null; 
     } 
     //Debug.log("might collide"); 

     // determine test area boundaries: 
     var bounds:Object = {}; 
     bounds.xMin = Math.max(bounds1.xMin, bounds2.xMin); 
     bounds.xMax = Math.min(bounds1.xMax, bounds2.xMax); 
     bounds.yMin = Math.max(bounds1.yMin, bounds2.yMin); 
     bounds.yMax = Math.min(bounds1.yMax, bounds2.yMax); 

     // set up the image to use: 
     var img:BitmapData = new BitmapData(bounds.xMax-bounds.xMin, bounds.yMax-bounds.yMin, false); 

     // draw in the first image: 
     var mat:Matrix = p_clip1.transform.concatenatedMatrix; 
     mat.tx -= bounds.xMin; 
     mat.ty -= bounds.yMin; 
     img.draw(p_clip1, mat, new ColorTransform(1, 1, 1, 1, 255, -255, -255, p_alphaTolerance)); 

     // overlay the second image: 
     mat = p_clip2.transform.concatenatedMatrix; 
     mat.tx -= bounds.xMin; 
     mat.ty -= bounds.yMin; 
     img.draw(p_clip2, mat, new ColorTransform(1, 1, 1, 1, 255, 255, 255, p_alphaTolerance), "difference"); 

     // find the intersection: 
     var intersection:Rectangle = img.getColorBoundsRect(0xFFFFFFFF, 0xFF00FFFF); 
     // if there is no intersection, return null: 
     if (intersection.width == 0) { 
      return null; 
     } 

     // adjust the intersection to account for the bounds: 
     intersection.x += bounds.xMin; 
     intersection.y += bounds.yMin; 
     return intersection; 
    }; 

    public static function hitTestShape(mc1:MovieClip, mc2:MovieClip, alphaTolerence:Number):Boolean { 
     return checkForCollision(mc1, mc2, alphaTolerence) != null ? true : false; 
    } 
} 
0

您可以使用hitTest一起BitmapData检查像素级别的碰撞,像这样。 (测试代码在Flash舞台上放置两个符号,“rectClip”和“spike”。还要先测试它们,然后再让它们保持接触并进行测试。 )

(无论哪种方式,你可以设置MouseMovestartDrag()和检查现场。)

var rect:Rectangle = rectClip.getBounds(this); 
var rectClipBmpData = new BitmapData(rect.width, rect.height, true, 0); 
rectClipBmpData.draw(rectClip); 

var spikeRect:Rectangle = spike.getBounds(this); 
var spikeBmpData = new BitmapData(spikeRect.width, spikeRect.height, true, 0); 
spikeBmpData.draw(spike); 

if(rectClipBmpData.hitTest(new Point(rectClip.x, rectClip.y), 
           255, 
           spikeBmpData, 
           new Point(spike.x,spike.y), 
           255)) 
{ 
    trace("hit"); 
}else 
{ 
    trace("No hit"); 
} 

祝您好运。