0
votes

Time for a really nooby question, but I can't find a satisfying answer.. Everytime I search for array tutorials, they are about how you put different strings of text into an array and accessing them again.

Here is what I have:

When you start the game, you are able to press lets just say 5 buttons. When you press button 1 flash (simplified) goes:

addChild(backGround) addChild(world) world.gotoAndStop(1) addChild(character)

And so on with the other buttons, changing which world you're in.

Here is what I want: I want to be able to add the same enemy movieclip specific places, according to which button the player presses.

So let's say the player presses button 3, and in world 3 I want there to be an enemy at x 200, y 300, and an enemy at x 600, y 450 and one more enemy some other specific place. All enemies are the same movieclip.

So how can add enemies like that???

I know I need an array, but how do i access the movieclip like I want to?

My code:

package {

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
In the array you will just save enemies coordinates not objects.akmozo

1 Answers

0
votes

Something like the following should do the trick:

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