actionscript-3 - 如何将不同大小的影片剪辑并排添加? AS3

标签 actionscript-3 flash flashdevelop animate-cc

大家好,我在这方面遇到了一些麻烦,我在舞台上添加了宽度不同的平台。我想在我的 for 循环中做的是在舞台上当前平台 x 位置的右侧添加更多平台。我遇到了麻烦,因为它们的尺寸不同,所以它们最终会在这款横向卷轴游戏中相互重叠。我将平台 MC 对齐到注册的右侧,如下所示:

enter image description here

这是较小尺寸的影片剪辑:

enter image description here

我这样做是因为我想为 Platform Movie Clip 中的每一帧添加不同的障碍。

添加初始平台:

private function addInitPlatform():void 
    {
        platforms = new mcPlatforms();
        platforms.x = (stage.stageWidth / 2) - 380;
        platforms.y = (stage.stageHeight / 2) + 175;
        addChildAt(platforms, 1);
        aPlatformArray.push(platforms);
    }

然后添加新平台:

private function addPlatForms():void
    {
        //Loop trhough Platform Array
        for (var i:int = 0; i < aPlatformArray.length; i++) 
        {
            var currentPlat:mcPlatforms = aPlatformArray[i];

            nOffSetX += currentPlat.width + 50;

            //Add platforms
            platforms = new mcPlatforms();
            platforms.x = nOffSetX;
            platforms.y = (stage.stageHeight / 2) + 175;
            addChildAt(platforms, 1);
            aPlatformArray.push(platforms);
            break;
        }
        trace(aPlatformArray.length + " NPLATFORMS");
    }

我正在尝试获取当前平台,这是我添加到舞台上的最后一个平台,并获取它的宽度,以便我可以在最后添加它,但随着时间的推移,它仍然在做一些奇怪和重叠的事情,

所以我想知道是否有人知道我应该如何解决这个问题,所以每当我在舞台上添加一个新平台的影片剪辑时,它都会与最后一个平台的右侧对齐影片剪辑添加到舞台上并留出一些空间像这样之间:

enter image description here

提前致谢!

最佳答案

我猜您的库中有许多不同的平台,并设置了链接名称。不确定您是否希望它们按随机顺序排列,但无论如何您可能想从数组中选取它们,所以:

var aPlatformsArray:Array = [p1,p2,p3]; //Platform Mc's from library
var addedPlatforms:Array = new Array(); //Array where we store added platforms

第一种方法是在添加每个平台后简单地提高偏移量:

var offsetX:Number = 0; //Starting x for the first platform

for(var i:int=0; i<10; i++){
    //Just picking random ones from the Linkage array:
    var platform:MovieClip = new aPlatformsArray[Math.floor(Math.random() * aPlatformsArray.length)]();
    platform.y = 200;
    platform.x = offsetX;
    offsetX = platform.x + platform.width + 50;
    addChild(platform);
    addedPlatforms.push(platform);
}

实际上您不必担心第二种方法,因为它在技术上更慢。

但主要区别在于,现在我们有一个数组用于选择平台到舞台,另一个数组用于推送添加的平台。此时不需要第二个数组,但您稍后可能会需要它。

对于偏移量计算,我们使用“前一个”元素的 xwidth + 固定间隙。

如果您只需要平台的固定顺序,您可以像之前那样使用类似的循环条件,并以正确的顺序选择平台:

for(var i:int=0; i<aPlatformsArray.length; i++){
    var platform:MovieClip = aPlatformsArray[i];

让我知道这是否能完成工作,或者我是否出错了。

关于actionscript-3 - 如何将不同大小的影片剪辑并排添加? AS3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49418430/

相关文章:

html - 网页设计 : Navigatible Large Flash Background

actionscript-3 - 在不使用服务器套接字的情况下监听 AS3 (AIR) 客户端中的传入连接

apache-flex - 如何使用 "in"运算符?柔性/AS3

javascript - 弹出式 div 使 flash 消息不可点击

javascript - 如何在 Flash 的 ExternalInterface 和 JavaScript 之间传递信息?

flash - 打开Adobe Flash IDE,打开Windows资源管理器,崩溃

actionscript-3 - NetStream.info,出现错误 #2154

apache-flex - 如何为按钮栏按钮设置工具提示

haxe - FlashDevelop/Haxe - 如何选择项目类型

actionscript-3 - FlashDevelop Profiler 不显示任何内容