A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: First attempt at classes

  1. #1
    Member
    Join Date
    Apr 2010
    Posts
    34

    First attempt at classes

    hi,
    I tried moving all of my MC functions into a class in order to apply them to many MC's.

    I don't really understand the class structure- I'm just sewing patches of information gathered on the web.

    Why can't I use stage.addEventListener in the class file? It says it doesn't recognize the property stage...

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You can learn about AS3 classes here.

    http://flashscript.biz/flashas3/AS3_...roduction.html

    The stage is still null, when the class is called. It is a matter of timing.
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Member
    Join Date
    Apr 2010
    Posts
    34
    so if I need to an event listener for mouse or keyboard events within my class- to who do I attach it to? ([something].addEventListener...)

  4. #4
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    Hey there

    Yeah, no instance of a class can access the stage until it has been added to the display list - lots of ways around this. You can listen for added_to_stage, or access the instance of the document class.

    A while ago I wrote a tutorial about just this:

    http://flashgametutorials.blogspot.c...input-and.html

    the first 2 tutorials talk about the document class and stuff.
    Check out my blog showing the development of my flash game, the Dregs of War

  5. #5
    Member
    Join Date
    Apr 2010
    Posts
    34
    now, everything seems to be in order (I wrote the event listeners within a function and it works) but I get error #1009 and It's written in the output window so i can't even know where the mistake is...

  6. #6
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    Is the 1009 error happening when you're trying to access the stage? Could you give use the text of the error?

    I can only imagine it's because you're trying to access the stage from an object that is not on the display list. How are you accessing the stage for keyboard input?
    Check out my blog showing the development of my flash game, the Dregs of War

  7. #7
    Member
    Join Date
    Apr 2010
    Posts
    34
    This is the code of my class:

    package {

    import flash.display.*;
    import flash.ui.*;
    import flash.events.*;

    public class TankOne extends MovieClip {

    var run:Boolean = false;
    var t_active:Boolean = false;
    var direct:Number;

    public function TankOne(){
    stage.addEventListener(KeyboardEvent.KEY_DOWN, fpress);
    stage.addEventListener(KeyboardEvent.KEY_UP, frelease);
    stage.addEventListener(Event.ENTER_FRAME, fframer);
    this.addEventListener(MouseEvent.CLICK, factive);
    }

    function factive(e:MouseEvent){
    if(t_active == false){
    t_active = true;
    }else{
    t_active = false;
    }
    }

    function fpress(e:KeyboardEvent){
    if(t_active == true){
    run = true;
    switch(e.keyCode) {
    case Keyboard.UP:
    direct = 1;
    break;
    case Keyboard.RIGHT:
    direct = 2;
    break;
    case Keyboard.DOWN:
    direct = 3;
    break;
    case Keyboard.LEFT:
    direct = 4;
    break;
    default:
    run = false;
    trace("pressed wrong key");
    break;
    }
    }
    }

    function frelease(e:KeyboardEvent){
    run = false;
    }

    function fframer(e:Event){
    if(t_active == true){
    gotoAndStop(2);
    }
    }
    }
    }
    And this is the error I get:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at TankOne()
    at grid_fla::MainTimeline/frame1()
    As I mentioned, I get this error in the output window- not the compiler error window...

  8. #8
    Member
    Join Date
    Apr 2010
    Posts
    34
    p.s:

    This is the code I have in my fla file:

    var tank:TankOne = new TankOne();

    function putTanks() {
    stage.addChild(tank);
    tank.x = 215;
    tank.y = 400;
    }

    putTanks();

  9. #9
    Member
    Join Date
    Apr 2010
    Posts
    34
    O.K- I "solved" it by deleting the code from the fla file and putting the MC in myself (without addChild)- not preferable, but I wanted to continue working.
    Now the problem is this:
    When I give a command to move the MC (this.y += 50) in my class- nothing happens. It doesn't move...

  10. #10
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    Hey there OferG! Sorry I didn't reply earlier.

    So let me try to explain again - the stage variable is null in all MovieClips until that MovieClip has been added to the displayList (using "addChild" on any container that is already on the stage).

    Within your TankOne Class, the function TankOne() is the constructor, which is called upon creation of a TankOne instance (when you write var tank:TankOne = new TankOne()). So when the tank is created, it hasn't been added to the display yet (that happens in your putTanks() funciton), so when you try to access stage by using stage.addEventListener, you get the error because as far as your tank is concerned, stage is null.

    This is annoying, because you'd think the stage should always be accessible, but there's probably a good reason that someone cleverer than me could explain.

    So there are two things you could do. Inside your TankOne class constructor, you can do this:

    PHP Code:
    public function TankOne(){
    this.addEventListener(Event.ADDED_TO_STAGEonAddedToStage);
    this.addEventListener(MouseEvent.CLICKfactive);
    }

    public function 
    onAddedToStage(e:Event):void
    {
    this.removeEventListener(Event.ADDED_TO_STAGEonAddedToStage);
    stage.addEventListener(KeyboardEvent.KEY_DOWNfpress);
    stage.addEventListener(KeyboardEvent.KEY_UPfrelease);
    stage.addEventListener(Event.ENTER_FRAMEfframer);

    But this is clunky, and you'd have to do it with all movieclips that need keyboard interaction.

    My preferred solution is to access the stage through a static instance of the document class, and also to have player input processed through a separate class to the tank class.

    Also, you don't want to add ENTER_FRAME event listeners to all your movieClips, but rather control all actions within a frame through a single ENTER_FRAME handler. This is especially important with things like collision detection.

    As I said in my first post, the solution is explained in great detail here:

    http://flashgametutorials.blogspot.c...input-and.html
    Check out my blog showing the development of my flash game, the Dregs of War

  11. #11
    Member
    Join Date
    Apr 2010
    Posts
    34
    Thank you for your reply, Awoogamuffin.
    Any ideas why my tank doesn't response to x and y movement commands (as I posted earlier)?

  12. #12
    Senior Member Awoogamuffin's Avatar
    Join Date
    Nov 2008
    Posts
    208
    I'd need to see how how and where you're calling that. Maybe the tank never actually receives the instruction.
    Check out my blog showing the development of my flash game, the Dregs of War

  13. #13
    Member
    Join Date
    Apr 2010
    Posts
    34
    I add the code you suggested and now the moving does work- so, Tanks a lot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center