A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [RESOLVED] How do I reference a class object from within another class?

  1. #1
    Junior Member
    Join Date
    Aug 2007
    Location
    Gig Harbor, WA
    Posts
    14

    resolved [RESOLVED] How do I reference a class object from within another class?

    Hello. I'm working on a crow shooting game using ActionScript 3.0 and so far I have a dynamically animated crow which is of type CrowClass flapping its wings on the stage with its eyes following my mouse cursor. My crow class consists of many dynamically animated subclasses such as RightWing, LeftWing, Beak, EyeBalls that follow the mouse cursor, ect and I am successful in accessing those subclass objects as needed from within my Crow class. (For example crow.left_wing.rotation)
    I placed the crow on stage via my document class which I call GameStage. I have also created a Bacon class and have placed a bacon class object on the stage as well via my GameStage document class. But now I want to make the EyeBall Class of my CrowClass look at the piece of bacon instead of my mouse cursor. My plan was to replace all references to the mouseX and mouseY with bacon.x and bacon.y. How do I reference the bacon class object from within my Crow classes Eyeball class? My initial attempt with syntax such as GameStage.bacon.x is giving me no luck. Any help is much appreciated....8-)

  2. #2
    FK Newb Beater
    Join Date
    Dec 2002
    Location
    Seattle
    Posts
    676
    Use getters and setters to set a class property on your Eyeball class that references your Bacon class. e.g.
    Code:
    package {
        import Bacon;
        public class Eyeball
        {
            var __baconReference : Bacon
            ...code for eyeball class
            //////////////////////
            // Getters and setters
            public function set baconReference(bacon : Bacon) : void
            {
                __baconReference = bacon;
            }
            public function get baconReference() : Bacon
            {
                return __baconReference;
            }
        }// Class
    }// Package
    After you set the reference, you can do what you want with it.
    code:
    var eye : Eyeball = new Eyeball();
    eye.baconReference = myBaconClassInstance;



    I hope this is clear.
    Just because you changed the code, doesn't mean it's yours

    Most Recent Work:
    Commercial tanning beds website

  3. #3
    Junior Member
    Join Date
    Aug 2007
    Location
    Gig Harbor, WA
    Posts
    14

    Thank you

    Thank you for your help. It's started me down the road to needed further studies. Fortunately a "Programming Wizard" friend is also introducing me to the concept of a GameObject class that creates an array that all my classes can access as needed and there's hope I'll grasp the concept before my artistic brain blows a gasket. Cross your fingers and thanks again!

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