1
votes

I have a weird problem in a game which I want to create. At first I have created a project without external classes. On the root I have three Characters and one Level. Also there is a script for the key listeners and I have eventListeners to register the level, levelElements, coins and the characters. Then I have a CharacterControl MovieClip in the library. This MovieClip contains the character behaviour. As example walk, jump, idle, gravity if not colliding to the ground. There are also different events and eventListeners. The scripts are on the timeline. If I call in both timelines a trace-function, the root was called before the CharacterController.

After that in my next exercise I created a document class Main. Now there are all root scripts. And for the CharacterController I also copied the timeline code and put it into an external class.

Now my problem is that the CharacterController class is called before the main class gets called. This leads to the problem that the eventListener and events can't get called in right order. There are happening a few errors. No Coin and no Character collides on the ground or a plattform. Everything is falling down.

How can I achieve that the Main gets called at first? Should I remove the characters and create them by script?

EDIT: Ok, I give a short example which shows the basic problem without the complex code of my game.

package {
    import flash.display.MovieClip;

    public class Main extends MovieClip {
        public function Main() {
            trace("main was called");
        }
    }
}
package  {

    import flash.display.MovieClip;

    public class My_Circle extends MovieClip {

        public function My_Circle() {
            // constructor code
            trace("circle was called");
        }
    }

}

Here are some pictures of the configuration and structure of my project:

sample image

sample image

sample image

sample image

I need Main called as first. I think it's a basic problem in as3.

2

2 Answers

1
votes

You'd make the class file of your stage Main.as in the properties pane.

enter image description here

Edit: Interesting. Just replicated this. I believe then that flash/air constructs elements so they're ready to be put on the stage instead of constructing the stage first and elements after. You should put the code you want to execute for your circle in some sort of init function and execute it in.

package 
{
    import flash.display.MovieClip;

    public class Main extends MovieClip 
    {

        public function Main() 
        {
            super();
            trace("Hello");
            (circle_mc as Circle).init();
        }

    }
}

Circle:

package 
{
    import flash.display.MovieClip;

    public class Circle extends MovieClip 
    {

        public function Circle() 
        {
            super();
        }

        public function init():void
        {
            trace("World");
        }

    }
}
0
votes

ok, I figured out a simple solution how you can make it by code. At first I think it's a better solution to create the object (circle, character, whatever) by code.

The timeline code:

import flash.events.Event;

Main.setStageRef(this.stage); //Super-important

stop();

The Main class code:

package {
    import flash.display.MovieClip;
    import flash.display.Stage; 
    import flash.display.DisplayObject;
    import flash.events.Event;

    public class Main extends MovieClip {
        public static var stageRef : Object = null;
        var movieClipExample : My_Circle;

        public function Main() {
            this.addEventListener(Event.ENTER_FRAME,this.afterMainTimeLine);
            stage.addEventListener("myEvent", myEventFunction);
        }

        public function afterMainTimeLine(e:Event) {
            trace("Hello");

            movieClipExample = new My_Circle;
            movieClipExample.init();
            stageRef.addChild(movieClipExample);

            removeEventListener(Event.ENTER_FRAME, this.afterMainTimeLine);
            this.addEventListener(Event.ENTER_FRAME,this.updateFunction);
        }

        public function updateFunction(e:Event){
            movieClipExample.moveFunction();
        }

        public function myEventFunction(_event: Event) {
            trace("myEvent was triggered");
        }

        //Getter/setter for stage
        public static function setStageRef(_stage : Object) : void
        {
            stageRef = _stage;
        }

        public static function getStageRef() : Object 
        {
            return stageRef;
        }
    }
}

The Object code (as example My_Circle):

package  {
    import flash.display.MovieClip;
    import flash.display.Stage; 
    import flash.display.DisplayObject;

    public class My_Circle extends MovieClip {
        public function My_Circle() 
        {
            stageRef = Main.getStageRef();

            trace("World");
            this.x = x;
            this.y = y;

            var evt = new Event("myEvent", true);
            stageRef.dispatchEvent(evt);
        }

        public function init():void 
        {
            trace("Created Circle");
            this.x = 300;
            this.y = 100;
        }

        function moveFunction(){
            this.y += 1;
        }
    }   
}

The output is following:

Hello
World
myEvent was triggered
Created Circle

Maybe this could be helpful for others.

Just one thing. For different objects from the same class it's better to use an array. The position (maybe) should be random.