dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [F8] Help with Communicating classes, attempt#2

  1. #1
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107

    [F8] Help with Communicating classes, attempt#2

    Last time I asked, I got no replies. Too simple or too hard? Or am I not clear? Please, see if you can help!

    I need to know how to let instances of classes to communicate with each other! Previously, people here adviced me to use event dispatchers, and that works fine for some occations, but not for all.

    This should be really basic! My main script is controlling movieclip-inheriting class-objects, let's say they are of the classes Alpha and Beta and stored in an array.

    I'd really like the instances A[1..x] of Alpha to be able to "keep track" of the instances B[1..y] of Beta, and the other way around. Or at least, if the main script tells, for example, A[15] to go hunt down B[4], A[15] will from then on focus on B[4] and that will affect every move A[15] makes. An Alpha instance will then interact with a Beta instance...

    But how do you write that code in the class-file? How do you refer to another class instance, when for example checking its coordinates? (Or, other instances of the same class?) It seems bizarre that classes shouldn't be able to communicate!

    Right now, my classes only implement "idle" and independent actions for their movieclips (or, dependent on something static, like the mouse), if not explicitly told by the main script.

    To make the above example more concrete: I tell an instance (with a setter) that it should keep an eye on (e.g. hunt!) another movieclip (with an array index or clip-name). That instance will now move towards the victim by checking the pray coordinates every frame. How can I refer to something that does not yet exist?

    Is "controller classes" the key? Or how do you do it? Or should the pray use event-dispatcher when it's being hunted? Any advice ,please! Thanx in advance!

  2. #2
    Junior Member
    Join Date
    Apr 2007
    Posts
    20
    I think you mean communication between instances of classes
    Perhaps you want this:
    (only pseudo-Code)

    Code:
    class B
    {
    	Main mainInstanceInB;
    	
    	function B (Main m)
    	{
    		 mainInstanceInB= m;
    	}
    
    	function controlA()
    	{
      		mainInstanceInB.controlA(); // in Main
    	}
            function traceCoordinatesOfA(xA, yA)
            {
                trace("Coordinates of A:" +xA+" "+yA);
            }
    }
    __
    class Main
    {
    	A testA; B testB;
    	function Main()
    	{
    		testA= new A();
    		testB= new B(this);
    		testB.controlA(); // in B
    	}
    	
    	function controlA()
    	{
    		testA.doSomeThing();// any function in A
                    testB.traceCoordinatesOfA(testA.getX(),testA.getY());
    	}
    }
    also possible to control objects in arrays because you can call them in Main with their array-numbers..
    best regards,
    J0e7
    Last edited by J0e7; 05-23-2007 at 07:07 AM.

  3. #3
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107
    Thanx! Ooo, this is cool! You're sending an instance of Main to B, so B can control A via Main!? Could the class Main instead be the main script, or _root, somehow? Or maybe not, because you need to declare the class Main in B, right? On the first row there - that's the pseudo-code, I guess?

    So, Main is a kind of "controller class"? Problem is, I have all my instances of A and B in my "_root script"... Do they need to be in "Main", somehow?

    Grateful, but still a little confused...

  4. #4
    All 1s and 0s dmonkey's Avatar
    Join Date
    Nov 2005
    Location
    Leeds, UK
    Posts
    606
    Hi,

    I don't really understand what you mean. You can communicate quite easily between classes in this way by passing instances as arguments into methods of another class.

    Can you maybe post some code?
    "If I have seen further, it is by standing on the shoulders of giants." - Sir Isaac Newton

  5. #5
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107
    Quote Originally Posted by dmonkey
    Hi,

    I don't really understand what you mean. You can communicate quite easily between classes in this way by passing instances as arguments into methods of another class.

    Can you maybe post some code?
    No, it's just that I'm pretty new to Actionscript and "real" OOP. Glad to hear that you say it's easy - I just need to see more examples of how it's done. There's no point in posting my code - it's too big...

    Ok, tell me if this is possible:
    The main script decides that it's time for B[4] to hunt A[7]. Then, a setter in class B is used to set the WhoIsHunted to A[7]. Class A has a getter, giving it's coordinates. Then B[4] can check A[7]'s coordinates via WhoIsHunted.GetCoordinates??

    Is that how I should interpret what you just wrote?

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