A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Answers to the top 5 questions you should never have asked

  1. #1
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801

    Answers to the top 5 questions you should never have asked

    Mods, if this causes a crapstorm, or is otherwise inappropriate for this forum, I apologize in advance.


    First, bookmark this link: http://livedocs.adobe.com/flash/9.0/...riptLangRefV3/

    Second, look at the blue bar at the top of the forum. There is a search function. Use it.

    I keep seeing the same bad questions coming up again and again. I do not mean that these are stupid questions - they are bad questions. They are bad because the question assumes certain limitations of previous flash versions, and asks for a solution to a problem that does not need to be solved. Rejoice! AS3 is not AS2 and the myriad wacky workarounds to AS2's shortcomings do not need to be used anymore. Without further ado, the biggest questions you do not need to ask, and the answers to the ones you should.

    1. How can I access something in another MovieClip by name?
    Why it's bad:
    This question assumes many things which are no longer necessary. Namely that everything should be a MovieClip, that instancenames are important, and that "paths" are necessary to get at objects.

    What you should have asked:
    How do I access a property of a class outside of that class?

    Answer:
    Easy as pie, once you understand the difference between variable/property names and
    instancenames. Let's write some code:
    Code:
    public class MyClassA extends Sprite {
      public var avalue:Number;
    
      public function MyClassA():void{
        graphics.beginFill(0xff0000);
        graphics.drawRect(0, 0, 40, 40);
        graphics.endFill();
      }
    
    }
    Code:
    public class MyClassB extends Sprite {
      public var a1:MyClassA;
      public var a2:MyClassA;
    
      public function MyClassB(){
        a1 = new MyClassA();
        a1.x = 20; //See?  EASY.
        a1.y = 10;
        a1.avalue = 1;
        a1.addEventListener(MouseEvent.CLICK, tattle);
        addChild(a1);
    
        a2 = new MyClassA();
        a2.x = 50;
        a2.y = 100;
        a2.avalue = 2;
        a2.addEventListener(MouseEvent.CLICK, tattle);
        addChild(a2);
      }
    
      public function tattle(event:MouseEvent):void{
        trace("you clicked on: "+(MyClassA(event.target).avalue));
      }
    }
    The important things to realize here are 1) I never set or used any instancenames. Because they are not necessary. I DID use variables, because variables are actual references to objects.
    2) Creating and using classes allows you to use the type checking system to ensure that the object you are dealing with is what you think it is.
    3) I used Sprite rather than MovieClip. Sprites are simpler, and should be used when you do not need to add actions to a frame, or have multiple frames. Sprites are also not dynamic, allowing you to check for accessing non-existant properties at compile time.

    2. What the heck is "Error #1009: Cannot access a property or method of a null object reference." ?

    Why it's bad:
    Because the error tells you what it means right there - your code is accessing something on a null object.

    What you should have asked:
    How can I determine what is null when I get Error 1009?

    Answer:
    This error occurs when you have a situation like this:
    Code:
    a:Sprite = null;
    
    a.x = 50;
    because you cannot set the x property of nothing. So, when you get Error 1009, look at the line that the error points out (use "go to source"). Everything that proceeds a dot (".") or array access ("[something]") could potentially be the culprit. You should ensure that each such object cannot be null. One way would be to insert trace statements outputting each just before the offending line in your code. One of them will show up null, then you can fix that.
    Code:
    a:Sprite = new Sprite();
    b:Sprite = new Sprite();
    
    function maybeGoBoom(c:Sprite):void{
      trace("a = "+a); //remove these after determining what is null.
      trace("c = "+c);
      a.x = c.x;
    }
    
    maybeGoBoom(b); //okay!
    maybeGoBoom(null); //not okay!
    3. What happened to "_root"?

    Why it's bad:
    As bad questions go, this one's subtle. First, the livedocs explain that "_root" became "root", and "_parent" became "parent", etc. Look there first before asking. Second, you should almost never actually need to use root. Using root and display heirarchy based access in general is a holdover from AS2 when that was the easiest/best way to do it.

    What you should have asked:
    How should I structure my AS3 project?

    Answer:
    In AS3, the displayList can be (and usually is) completely separate from the logic used
    inside classes. Classes deal with the logical and conceptual structure of your projects.
    There are surprisingly few behavioral reasons to reach back up the display tree. Reaching back up just to go back down is a common bad practice. Instead, it's simpler to pass around actual references to actual objects. Often the parent serves double duty as the visual container and behavioral manager of a group of children. These duties should be kept separate. This example touches on the next question, so let's go on.

    4. I have a bunch of stuff sequentially named, but parent["thingy"+i] in a loop doesn't work

    Why it's bad:
    That's not even a question. At least phrase things as a question. Besides which, names are a terrible way to group and manipulate objects because they're very fragile.

    What you should have asked:
    I have a bunch of instances which I would like to handle as a group. What's a good way to do this?

    Answer:
    Arrays are pretty neat. They let you keep multiple objects together in sequence. Read and love:
    Code:
    public class Thing extends Sprite{
      public vy:Number;  //current up/down velocity
      public incr:Number; //increment at each timeslice
    
      public function Thing(){
        vy = 0;
        incr = 1;
      }
    
      public function applyGravity():void{
        y += vy;
        if (y > stage.stageHeight){
          vy *= -1; //bounce!
        }else{
          vy += incr;
        }
      }
    }
    Code:
    public class BounceExample extends Sprite{
      public var howmany:int = 3;
      public var things:Array;
      public var timer:Timer;
    
      public function BounceExample():void{
        things = new Array();
        var t:Thing;
        for (var i:int = 0; i < howmany; i++){
         t = new Thing();
         things.push(t);
         addChild(t);
        }
    
        addEventListener(Event.ADDED_TO_STAGE, init);
      }
    
      public function init(event:Event = null):void{
       for (var i:int = 0; i < things.length; i++){
         t.x = Math.floor(Math.random() * stage.stageWidth);
         t.y = Math.floor(Math.random() * (stage.stageHeight / 2));
       }
    
       timer = new Timer(50);
       timer.addEventListener(TimerEvent.TIMER, step);
       timer.start();
      }
    
      public function step():void{
        for (var i:int = 0; i < things.length; i ++){
          things[i].applyGravity();
        }
      }
    }
    The above code is untested, and probably has at least one bug in it. The point is, the things array is faster to execute and easier to use than accessing properties by name (which are not even set in the example code).

    5. I'm trying to do <complex project>, kinda like <website>, but it's not working.
    What could be wrong?
    .

    Why it's a bad question:
    This sort of question is far too vague to even begin to answer. Even if I knew what you were trying to do, you should solve problems on a much smaller scale. Include sample code if you have it. ALWAYS include an error message (not just the fact there was one) if an error is given.

    What you should have asked:
    This code <code sample here> is giving this error <copy and paste error> under such-and-such circumstances. Could someone give me advice?

    Answer:
    Sure, that's what we're here for.

  2. #2
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    LMFAO Nothing on preloaders . Nicely written and thanks for this. Now lets see any one notices it.
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  3. #3
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    ^ ROFL, I noticed it, but I'm not sure if that's a good or bad thing. =))

    PS: I'll try to read all that after I take a shower. Prepare the popcorn !



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  4. #4
    Multitouch Aficionado
    Join Date
    Mar 2006
    Posts
    275
    Sticky

    Seriously, bookmark LiveDocs. I rely heavily on them when I'm working in Flash or answering people's questions here. They tell you everything you need to know about Flash once you understand its basic structure.
    Last edited by illustratedlife; 01-04-2008 at 08:10 PM.

  5. #5
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    ^ AMEN on that one !



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  6. #6
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    I love it man.

    I've gone through at least 2 grumpy-at-the-noobs phases per year for the...what, seven years I've been on this site? Pro'lly accounts for about 30% of my posts, the rest being evenly divided between asking for help and giving it...sometimes it's hard not to, uh, vent.

    Nobody pays us to be here rewriting peoples' code for 'em...I mean...I do it when I have a little free time because it keeps me sharp, and explaining things to people helps me formalize them in my own mind. But I'm certainly under no obligation not to get irritated by 'em occasionally. I'm actually more irritated that no one here has even tried to answer most of my recent questions...I guess this is a good board these days if you have no idea what you're doing and need advice from the couple of people who do.

    I don't think the sysops will reject this thread, because only ppl who've been around a couple years will get what you're talking about.

    Anyway, now when I see things like "I'm trying to do <complex project>, kinda like <website>, but it's not working," I just quote a five-digit price. Not in a mean way, just...y'know...maybe it helps make people think about what it is they're asking a little teeny bit...

  7. #7
    Multitouch Aficionado
    Join Date
    Mar 2006
    Posts
    275
    I feel ya on that one Josh. A few years back when I started getting into ActionScripting, or this summer when I was trying to get AS3, this board was helpful for answering questions, but I now feel like I'm in the upper echelon of our community. When I run into a problem I can't solve myself, few people here can help.

    Doesn't mean I can't come back and kill time now and again explaining event listeners to someone. =P

  8. #8
    Member
    Join Date
    Nov 2007
    Posts
    67
    If a little knowledge is dangerous, where is the man who has so much as to be out of danger?
    Thomas Huxley

  9. #9
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    ^ Sorry, but that quote is tarded. Why is little knowledge dangerous ? The less you know about certain things or maybe events, the better... The more knowledge, the more danger... less knowledge, "peace"... Knowledge is not everything, people not having to much of it are way happier not knowing what's going on around them and living their live as they always did then others who got a better understanding of how things really work.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  10. #10
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    Maybe Huxley meant, "when it comes to coding AS3, riding a motorcycle, or preparing blowfish sushi, a little knowledge can, on occasion, be a dangerous thing."

  11. #11
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    ^ I didn't say I didn't get what he was trying to say
    Anyways, that quote is still tarded.
    Last edited by fx.barrett; 01-05-2008 at 07:22 PM.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  12. #12
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Quote Originally Posted by 5TonsOfFlax
    Mods, if this causes a crapstorm, or is otherwise inappropriate for this forum, I apologize in advance.
    It is appropriate and came up in other forums for former Flash versions as well. There is unfortunately no clean solution. You may post this now here and one thread above you one of those 5 questions is asked .

    What you can do is put the link to this thread in your footer and point to the link when such question is asked.

    I wrote a number of classes and also included components not included in Flash 9, which facilitate working with AS3 and one can concentrate on the actual problems without filling up the actual working code with unnecessary code. Often times questions are asked here, which can easily be solved by just including such class.

    Click on the Flashscript biz classes on my footer. There is also documentation for each class, since I realized that without documentation, which I often see that becomes useless.
    - The right of the People to create Flash movies shall not be infringed. -

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