0

我正在写一个你必须通过迷宫的游戏。我希望这个游戏有不同的层次。但是对于每个级别,迷宫都会有所不同。所以我画了其他的墙。但是如果我有 50 个不同的级别,我不想将我的碰撞检测方法写 50 次。

我想了一种方法来修复它,但它不起作用。我创建了一个没有任何内容的新符号,并将其命名为墙。我认为我可以让我的 wall = wall1(我转换的另一个符号,并导出为 as),然后执行 stage.addChild(wall)。但我找不到办法做到这一点。所以我需要帮助!

4

2 回答 2

0

您可以制作一个在第一帧上显示 50 帧的 MovieClip,stop()然后像这样执行代码:

    private var wallnum:int;

public function Main()
{
   stop();
   wallnum = 1;
   var wallobj = new Wall();
   addChild(wallobj);
   wallobj.gotoAndStop(wallnum);
}

对于碰撞检测,我推荐 Pixel Perfect Collision Detection ( https://code.google.com/p/master-air-controller/source/browse/trunk/master-air-controller/src/PixelPerfectCollisionDetection.as?spec=svn6&r =6 )

于 2013-06-11T22:01:17.150 回答
0

制作一个通用类,例如Wall,让您的库符号将其用于它们的基类。您不需要在运行时使用 ActionScript 创建它们以使这种继承起作用,您仍然可以将您的 MovieClips 放在舞台上。

您需要做的下一件事是将这些墙存放在某个地方。因为您似乎对 ActionScript 缺乏经验,并且希望避免为新级别编写代码,所以您可以使用管理器类型类自动执行此过程。我们将调用这个类WallManager,它看起来像这样:

public class WallManager
{
    private static var _walls:Vector.<Wall> = new <Wall>[];

    internal static function register(wall:Wall):void
    {
        _walls.push(wall);
    }

    public static function reset():void
    {
        _walls = new <Wall>[];
    }

    public static function get walls():Vector.<Wall>{ return _walls; }
}

然后我们将创建您的Wall课程。在此类的构造函数中,我们将自动让 Wall 将自身添加到WallManager列表中:

public class Wall extends Sprite
{
    public function Wall()
    {
        WallManager.register(this);
    }

    public function touchingMouse(mouseX:int, mouseY:int):Boolean
    {
        // For this example I am checking for collisions with the
        // mouse pointer. Replace this function with your own collision
        // logic for whatever it is that is supposed to collide with
        // these walls.

        if(parent === null) return false;

        var bounds:Rectangle = getBounds(parent);
        return bounds.contains(mouseX, mouseY);
    }
}

This setup is not 'best practice', but it is suitable in your situation because your project seems small, you appear to be working on it alone, it's simple and it gets the job done.

At the end of each level, use WallManager.reset() to remove the walls from the previous level. For checking collisions across all walls, just use a loop like this:

for each(var i:Wall in WallManager.walls)
{
    var collision:Boolean = i.touchingMouse(mouseX, mouseY);

    if(collision)
    {
        // There was a collision.
        //
        //
    }
}
于 2013-06-10T22:45:47.583 回答