views:

35

answers:

1

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() :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();
A: 

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.

loxxy