dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Right place to put code

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    6

    Right place to put code

    Hi,

    Here is the situation, I have a main as file that imports a circle as file. when my project loads, I have a circle in the middle of the screen. When i click on the circle, I create a new cirle and drag and drop it where ever I want on the screen. Pretty basic. Now the question is : where is the best place to put the code of the create and drag and drop features. In the Main as file or in the Circle as file. Which is the proper way?

    Richard

  2. #2
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    if the circle class is always intended to drag, i'd put it in there. if the circle class is just a circle, and some circles will drag and other won't, i'd put it in main.

    doesn't really matter tho

  3. #3
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,131
    As a general rule, I put mouse listeners on objects, and mouse behavior on objects, outside the object itself. While there's not one "right" way to do it, there are a few good reasons for putting the listeners outside. For one thing, if your MouseEvent listeners are in the object and aren't weak referenced, their existence prevents the object from being garbage collected when it's removed from the stage. Another reason is that if you have a lot of objects with this kind of behavior, and want the holding class to figure out which one is being clicked or which one is being dragged, if you then put the listeners inside the objects you're going to have to make them dispatch events to the holding class which will have to listen for them anyway to figure out which one is being used. Again, there are ways around this, but I prefer a direct approach where the listeners are in the holder and can be applied to whatever sub-objects you want. Moagrius is right that if this is the consistent, normal and expected behavior of this object and there's no reason for the holder to know about it, it may make sense under some circumstances to embed the listener in the object; and there may be other reasons, like if it's a button that you only want to generate a certain event when it's enabled, for example. Again, it comes down to your own particular style, and I don't think anyone would fault you for doing it either way.

  4. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Thanks for the enlightment.

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