开发者

How do you move an internal function to an external Actionscript 3 class file?

I have this catcher game, where soda bottles falls on to the stage, and we use a character as a cursor to catch it. But all the actionscript is built in already into the file internally. But I was asked to put the two classes I use (the cursor and the falling soda bottles) in an external classfile.

And since I'm really new to OOP I really don't understand how to do this. So, how do you move an internal actionscript for a class into an external actionscript file?

Thanks in advanced, for considering to help :p

The code is bellow if it helps.

var koyupo:Koyupo;
var createsodaID:uint;
var gamespeed:uint;
var score:uint=0;

function startGame () :void {

    // create the character as a cursor 
    koyupo= new Koyupo();
    koyupo.x=200;
    koyupo.y=270;
    addChild(koyupo);
    // listens for a mouse move event to move the koyupo
    stage.addEventListener(MouseEvent.MOUSE_MOVE, movekoyupo);
    Mouse.hide();
    gamespeed=500;
    createsodaID=setInterval (createsoda, gamespeed);
    //setting the ammount displayed on screen to be 0
    droppedtext.text=caughttext.text='0';

}

function movekoyupo (e:MouseEvent) :void{
    // move koyupo according to the mouse movement
    koyupo.x=this.mouseX;
    e.updateAfterEvent ();
}
function getRandomNumber ( low:Number , high:Number):Number 
{
    var randomNumber:Number = Math.random()*(high-low) +low;
    return randomNumber;
}
function createsoda () :void {
    //creating falling soda bottles
    var soda:Soda= new Soda ();
    soda.y=-50;
    trace(getRandomNumber(50,900));
    soda.x=getRandomNumber(100 , 500);
    soda.addEventListener(Event.ENTER_FRAME, dropsoda);
    addChild(soda);
}

function dropsoda (e:Event):void{

    var mc:Soda=Soda(e.target);
    mc.y+=10;
    if(mc.hitTestObject (koyupo)){
        caught (mc);
    }else if (mc.y>stage.stageHeight) {
    dropped (mc);
    }
}

function caught (mc:Soda):void{
    mc.removeEventListener(Event.ENTER_FRAME,dropsoda);
    removeChild(mc);
    caughttext.text=String(Number(caughttext.text)+1);
        if(caughttext.text=='35'){
        proceed();
    }
}


function dropped (mc:Soda):void{
    mc.removeEventListener(Event.ENTER_FRAME,dropsoda);
    removeChild(mc);
    droppedtext.text=String(Number(droppedtext.text)+1);
    if(droppedtext.text=='5'){
        gameOver();
    }
}

function gameOver()开发者_JAVA百科 :void{
    score=Number(caughttext.text);
    stage.removeEventListener(MouseEvent.MOUSE_MOVE,movekoyupo);
    removeChild(koyupo);
    clearInterval(createsodaID);
    removeChild(caughttext);
    removeChild(droppedtext);
    while(numChildren>0) {
        getChildAt (0) .removeEventListener(Event.ENTER_FRAME, dropsoda);
        removeChildAt(0);
    }
    Mouse.show();
    gotoAndStop('gameover');

}

function proceed() :void{
    score=Number(caughttext.text);
    stage.removeEventListener(MouseEvent.MOUSE_MOVE,movekoyupo);
    removeChild(koyupo);
    clearInterval(createsodaID);
    removeChild(caughttext);
    removeChild(droppedtext);
    while(numChildren>0) {
        getChildAt (0) .removeEventListener(Event.ENTER_FRAME, dropsoda);
        removeChildAt(0);
    }
    Mouse.show();
    gotoAndStop('win');

}
startGame();


You integrate th e flash IDE with external class by using a "Document class" (Something sort of a main function in C/C++) ie When the flash IDE looks for a class set as the document class & calls its Constructor & enter frame handlers.

     package classes
        {
        public class startGame extends MovieClip
            {                
               // Class variables can be declared here

                // Constructor of the document class
               function startGame () :void {                    

               // Function to handle Updates as per frame rate
               addEventListener(Event.ENTER_FRAME,enterFrameHandler);                    

                }

         }

    }

The above may not give you anything useful. But I have tried to give you an idea about how the code should be moved. Its as simple as

  • creating a class (startgame) , setting it as the document class.
  • The constructor will be called (startgame function above).
  • You may set an event listener to update things every frame in the constructor itself.
  • The package keyword allows you to place all of this class & subsequent ones into hierarchically ordered folders (package/folder name classes here) in the file system.

I know it might sound confusing if you are totally working with OOP & AS3 together for the first time, But since it is a very wide topic, there is no alternative but to really practice & learn OOP concepts at the least. As I doubt whoever (you client or teacher) asked you to use classes would be satisfied with just the above.


When you first start doing OOP you might want to try this script i wrote that turns timeline code into a document class.

timeline to document class

It isn't perfect but it works nicely, just paste your timeline code into the textarea, and give the class a name of Main. It will then generate a doc class for you.

Take the new code and copy it into a file called Main.as. Then remove your old timeline code from your fla. Next, click somewhere on the stage and then in the properties window under "publish" you'll see an input field called "Class" type Main into the input field and test your movie. Make sure that Main.as is saved next to your fla file.

You can compare your original timeline code with the new document class to begin to get a feel for what is required when you want to write classes. This is really just the tip of the iceberg. Wrapping your head around OOP and design patterns takes time, but once you do its worth it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜