如何控制在AS3中添加相同的动画片段不同的地方?

时间:2014-12-06 21:32:06

标签: arrays actionscript-3 movieclip

时间问题真的很棒,但我找不到令人满意的答案..每次我搜索数组教程时,它们都是关于如何将不同的文本字符串放入数组并再次访问它们。

以下是我的内容:

当你开始游戏时,你可以按下让我们只说5个按钮。当你按下按钮1闪光(简化)时:

的addChild(背景) 的addChild(世界) world.gotoAndStop(1) addChild(character)

等等其他按钮,改变你所处的世界。

这就是我想要的: 我希望能够根据玩家按下的按钮添加相同的敌人动画片段特定位置。

所以让我们说玩家按下按钮3,在世界3中我希望在x 200,y 300处有一个敌人,在x 600,y 450处有一个敌人,在另一个特定地方还有一个敌人。所有敌人都是相同的动画片段。

那么如何添加像这样的敌人???

我知道我需要一个数组,但是我如何像我想要的那样访问movieclip?

我的代码:

包{

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

public class Main extends MovieClip {

    public var character:Character;
    public var backGround:BackGround;
    public var world:World;
    public var worldMap:WorldMap;
    public var monster1:Monster1;
    public var worldLevel:int;
    public var currentLevel:int;

    public function Main() {

        backGround = new BackGround;
        world = new World;
        monster1 = new Monster1
        character = new Character();
        worldMap = new WorldMap;

        // Every world lies in a different frame in the "world" movieclip 
        world.worldDangers.gotoAndStop(currentLevel);
        world.SafeGround.gotoAndStop(currentLevel);
        world.GroundViz.gotoAndStop(currentLevel);
        world.PortaltoNew.gotoAndStop(currentLevel);

        //----- on the world map is the different world buttons ---
        addChild(worldMap);

        addEventListener(Event.ENTER_FRAME, moveWorld)

        //------------------- world buttons --------------------
        worldMap.buttonWorld1.addEventListener(MouseEvent.CLICK, gotoWorld1)
        worldMap.buttonWorld2.addEventListener(MouseEvent.CLICK, gotoWorld2)
        worldMap.buttonWorld3.addEventListener(MouseEvent.CLICK, gotoWorld3)
        worldMap.buttonWorld4.addEventListener(MouseEvent.CLICK, gotoWorld4)
    }


    function gotoWorld1 (m:MouseEvent):void
    {
        currentLevel = 1;
        addChild(backGround);

        addChild(world);
        world.x = 0;
        world.y = 0;

        isinWorld = true

        addChild(character);
        character.x = 300;
        character.y = 650;
        character.gotoAndStop(3);


        worldMap.parent.removeChild(worldMap);
    }


    //----------------- function world 2 ---------------------
    function gotoWorld2 (m:MouseEvent):void
    {
        currentLevel = 2;
        addChild(backGround);

        addChild(world);
        world.x = 0;
        world.y = 0;

        isinWorld = true

        addChild(character);
        character.x = 300;
        character.y = 650;
        character.gotoAndStop(3);

        worldMap.parent.removeChild(worldMap);
    }

    //----------------- function world 3 -----------------------
    function gotoWorld3 (m:MouseEvent):void
    {
        currentLevel = 3;
        addChild(backGround);

        addChild(world);
        world.x = 0;
        world.y = 0;

        isinWorld = true

        addChild(character);
        character.x = 300;
        character.y = 650;
        character.gotoAndStop(3);


        worldMap.parent.removeChild(worldMap);
    }

    function gotoWorld4 (m:MouseEvent):void
    {
        currentLevel = 4;
        addChild(backGround);

        addChild(world);
        world.x = 0;
        world.y = 0;

        isinWorld = true

        addChild(character);
        character.x = 300;
        character.y = 650;
        character.gotoAndStop(3);


        worldMap.parent.removeChild(worldMap);
    }




    function moveWorld (e:Event)
    {


        if (isinWorld)
        {
            world.y = world.y + 5;


            if (character.hitTestObject(monster1))
            {
            world.parent.removeChild(world);
            backGround.parent.removeChild(backGround);
            character.parent.removeChild(character);

            isinWorld = false


            }
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

以下内容应该可以解决问题:

// Define a bunch of positions for this world
var positions:Array = [
    new Point(200, 300),
    new Point(600, 450),
    new Point(50, 350)
];

// Probably makes sense to store your enemies on 
// an array for access later in your game
var enemies:Array = [];

// Temporary variables for the loop
var enemy:Enemy;
var position:Point;

// Iterate over the positions array
for (var i:int = 0; i < positions.length; i ++) 
{
    // Get the position
    position = positions[i] as Point;

    // Create a new enemy and position him
    enemy = new Enemy();
    enemy.x = position.x;
    enemy.y = position.y;

    // Add child and store it on an array 
    addChild(enemy);
    enemies.push(enemy);
}