A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Basic question about referencing an instance.....

  1. #1
    Member
    Join Date
    May 2007
    Posts
    46

    Basic question about referencing an instance.....

    Hi all!

    I came across something id just like to straighten out.


    //class A is just a useful thing with public methods

    Code:
    var a = new A();
    //class B will handle events and change a's state via mouse events, it also
    should be given a reference to object a so that it can manipulate a's state.


    Code:
    var b = new B(a);

    When i do this, i am not 100% sure what i have done.


    If class Bs constuctor includes this:

    Code:
    private var objectToControl:A;
    
    public function B (objectToControl:A)
    {
        this.objectToControl = objectToControl;
    }
    Then what am i doing here? I do not seem to be able to contact 'a'. I think maybe im making a copy of 'a' into objectToControl and not actually referencing it like i want to.

    I just dont know though. If i am making a copy of 'a' and assigning it to a property, then how do i achieve a reference to 'a' and not a clone of it?

  2. #2
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    you shouldn't be making a copy of it, it should be passing along a reference, just like when you work with an array or matrix:
    Code:
    var a:Array = new Array(1, 2, 3, 4);
    var b:Array = a;
    
    b.push(5);
    
    trace(a);  // 1, 2, 3, 4, 5
    But I think a better way to handle this is to dispatch events from B, and have A listen for them.

  3. #3
    Member
    Join Date
    May 2007
    Posts
    46
    Thanks for that matey

    Can i ask you to explain further if you have the time?


    Would you be able to give me an example of what you mean by dispatching events for A?

    You mean inside A

    this.addEventListener(event, function);

    Although the way i am confused is i do not know how this works.

    Does that not mean instead that 'a' instances picks up mouseOver events?.

    I am not sure how to listen to events from instance a
    Then dispatch those events from b

    I think thats why im doing things in a messy way. No matter how much i read about events, i just cannot see the bare bones.
    Last edited by bellend; 09-24-2007 at 04:41 PM.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The event model would certainly work, but I don't think it's necessarily the best fit. I'm going to try my psychic abilities here. I'm getting a vision of code in B that looks like this:
    Code:
    public function doSomething(){
     a.someProperty = someValue;
    }
    Of course that won't work. B has no idea what "a" means. Change that to:
    Code:
    public function doSomething(){
     objectToControl.someProperty = someValue;
    }
    Now, I'm going to explain by way of example. Your real name is probably not "bellend". For this example, I'm going to pretend your real name is "Bob" (but I don't know that).

    If someone told me to explain this to Bob, I'd have no idea who to talk to. But, if someone told me to explain this to bellend, well, I just did that, because I have a reference to you as "bellend".

    So there you go, Bob. Make sense?

  5. #5
    Member
    Join Date
    May 2007
    Posts
    46
    Thanks but now im more confused than ever!

    In a new direction slightly from my thread say: A random example

    A lightbulb sprite and a lightswitch sprite.

    The lightbulb has a listener attached that is listening for a mouseclick to happen on lightswitch.

    When the user clicks on lightswitch, this event is dispatched and the lightbulb comes on.

    I dont know how to implement this if there are 2 seperate instances. This part is what i fail to understand.

  6. #6
    Member
    Join Date
    May 2007
    Posts
    46
    i posted this reply twice by mistake
    Last edited by bellend; 09-24-2007 at 05:44 PM. Reason: mistake

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Sorry for confusing you Bob. In that case I would go with MyFriendIsATaco's suggestion and use event listeners.

    Code:
    public class LightBulb{
      ...
      private var on:Boolean = false;
      public function flipState(evt:Event = null):void{
        on = !on; //if it was on, turn off, and vice versa
      }
    }
    Code:
    var switch:LightSwitch = new LightSwitch();
    addChild(switch);
    var bulb:LightBulb = new LightBulb();
    addChild(bulb);
    
    switch.addEventListener(MouseEvent.CLICK, bulb.flipState); //when switch is clicked, bulb flips.
    Last edited by 5TonsOfFlax; 09-24-2007 at 05:52 PM.

  8. #8
    Member
    Join Date
    May 2007
    Posts
    46
    !!!!!!!

    Thanks!!

  9. #9
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Hehe, I was referring to even further by broadcasting a custom event, like: LightBulbEvent.SWITCH, and attaching a lightbulb to the listener. LightSwitch will dispatchEvent(new LightBulbEvent(LightBulbEvent.SWITCH)); or something along those lines.

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