【问题标题】:Resizing Movie Clips AS3调整影片剪辑 AS3 的大小
【发布时间】:2013-03-31 13:25:51
【问题描述】:

好的,所以我有一个Flash游戏,一个平台游戏。在这个游戏中,你必须跳过尖峰。所以我创建尖峰并将其转换为一个符号,一个电影剪辑。当它注册为一个电影剪辑时,它不是一个三角形(像钉子),而是一个矩形。这意味着当玩家去避开钉子并跳跃时,如果他太靠近他会死,但他没有击中钉子,他会击中隐形长方形围绕尖峰。无论如何要更改影片剪辑的形状以使其适合尖峰和尖峰。

【问题讨论】:

    标签: actionscript-3 resize movie clip


    【解决方案1】:

    你可以使用hittest,这里是一个使用例子

    http://www.foundation-flash.com/tutorials/as3hittesting/

    【讨论】:

      【解决方案2】:

      对象之间的内部命中测试将检查对象的边界框,因此这对您不起作用。

      如果你能以某种方式使用玩家代理作为一个点(最低点,比如他的脚中部或类似的东西)你可以使用spike.hitTestPoint(globalFootX, globalFootY, true);

      如果这不起作用,您必须手动为项目创建 hittest-representation 并执行您自己的 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;
          }
      }
      

      【讨论】:

        【解决方案3】:

        根据我处理相同类型问题的经验,您不能真正将 AS3 hitTestObject(如果您遇到此问题,我想您会使用它)与 MC 的特定形状一起使用。它总是默认为占用影片剪辑本身空间的框。

        所以要回答您的问题,不,您不能更改 MC 的形状以使其仅尖峰,默认情况下 hitTestObject 使用 MC 周围的边界框。

        解决这个问题的方法很少(很多人通常建议不要使用hitTestOjbect,因为它效率不高,而是编写自己的命中测试代码。我建议在网上寻找这样的例子,有很多其中)。

        如果您不想处理这个问题,您的另一个选择是创建一个单独的一系列盒形 mc,它们充当您的边界框并且可以排列在数组中,然后运行一个循环以查看是否你的角色正在击中任何一个。这样,您可以根据需要调整边界框大小。

        但是如果你想使用hitTestObject,很遗憾你必须使用整个对象边界框。由于我不完全确定您将如何处理此问题的详细信息,因此我最好的建议是上网并阅读 AS3 中命中检测的基础知识。

        您的问题在于命中检测,不一定是电影剪辑。

        【讨论】:

          【解决方案4】:

          您可以像这样使用BitmapData 和hitTest 来检查像素级碰撞。

          (要测试代码,请在 Flash 舞台上放置两个符号,“rectClip”和“spike”。还要先测试它,让它们远离,然后第二次让它们接触并测试它。)

          (无论哪种方式,您都可以设置MouseMove 或startDrag() 并实时查看。)

          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");
          }
          

          祝你好运。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-08-28
            • 1970-01-01
            • 2013-01-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多