A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Interesting OOP problem

  1. #1
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,930

    Interesting OOP problem

    I had an interview last week that went very well. The group builds everything in FlashBuilder and Flex/Air and I haven't worked with any of those much, building most things in FlashDevelop or the Flash IDE, but I know AS3 and have a pretty good understanding of OOP principles, but am still trying to figure out "best practices" for different situations.

    Anyway, they gave me a small "project" to do so they could get an idea of how I structure things and I'm just looking to get other ideas on different ways to set it up as well as any input/critiques of the way I set it up.

    I'll post the "problem" here and post my structure/code in the next reply (in case you don't want to look before you come up with your structure, and then want to look to give any input after that).

    Would appreciate any ideas/input from some of you Flex/Air/OOP folks.

    So, they said I could build everything in code and trace statements and that I didn't have to create any graphics or put anything on the stage, but I did anyway, since that's a convention I'm more used to.

    The "project" was to create a house that contains 2 cats and 2 birds. The cats run, the birds fly. Animals of the same species talk to each other (cats meow to each other and birds chirp to each other). All events should be driven by a single action/event.

    What I did works and follows their rules, but I'm always interested in learning more ways "skin a cat" (NOT part of the problem, although, they said I could also add in code to have the cats chase the birds as an extra).

    Thanks for any ideas/input!

    My "solution" in the next reply...
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  2. #2
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,930
    So, what I did was create a House.as package/class that created the cats and birds, and handled the collision detection to determine if a cat hit another cat or a bird hit another bird (the collision was the "event" that I used to drive the actions in the house).

    I was going to have a text field pop up next to the animal to show the Meow or Chirp, but ended up just doing it in the trace statement because I wasn't sure if I should try to create the text field directly within the collision detection, or create a function within the Cat/Bird script and call that from the collision function...

    Here's House.as:

    Actionscript Code:
    package {
       
        import flash.display.MovieClip;
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.net.URLLoader;
        import flash.net.URLRequest;
        import flash.display.Loader;
       
        public class House extends MovieClip
        {
            private var cats:Array;
            private var birds:Array;
           
            private var numCats:Number = 2;
            private var numBirds:Number = 2;
           
            public function House() {
                init();
            }
           
            private function init() {
                makeCats();
                makeBirds();
                addEventListener(Event.ENTER_FRAME, catCollision);
                addEventListener(Event.ENTER_FRAME, birdCollision);
            }
                   
            public function makeCats() {                       
                cats = new Array();
                for (var i:int = 0; i < numCats; i++) {
                    var cat:Cat = new Cat();
                    cat.x = Math.random() * stage.stageWidth;
                    cat.y = Math.random() * stage.stageHeight;
                    addChild(cat);
                    cats.push(cat);

                }          
            }

            public function makeBirds() {                      
                birds = new Array();
                for (var i:int = 0; i < numBirds; i++) {
                    var bird:Bird = new Bird();
                    bird.x = Math.random() * stage.stageWidth;
                    bird.y = Math.random() * stage.stageHeight;
                    addChild(bird);
                    birds.push(bird);
                }          
            }
           
            private function catCollision(event:Event):void{
                for (var i:int = 0; i < cats.length; i++) {
                    if (cats[0].hitTestObject(cats[1])) {
                        trace("meow");
                    }
                }
            }
            private function birdCollision(event:Event):void{
                for (var i:int = 0; i < birds.length; i++) {
                    if (birds[0].hitTestObject(birds[1])) {
                        trace("chirp");
                    }
                }
            }

        }
    }

    Then I created a Cat.as and Bird.as to load graphics for the animals, position them in the house and create random motion to move them around.

    Here's Cat.as:

    Actionscript Code:
    package {
       
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.net.URLLoader;
        import flash.net.URLRequest;
        import flash.display.Loader;
        import flash.text.*;
       
        public class Cat extends Sprite
        {
            var imageLoader:Loader = new Loader();
            private var vx:Number = Math.random() * 8 - 4;
            private var vy:Number = Math.random() * 8 - 4;
           
            var left:Number = 20;
            var right:Number = 500;
            var top:Number = 20;
            var bottom:Number = 350;

            public function Cat () {
                trace ("cat created");         
                var image:URLRequest = new URLRequest("kitty.png");
                imageLoader.load(image);
                this.addChild (imageLoader);
                this.addEventListener(Event.ADDED_TO_STAGE, Run);              
            }      
                   
            private function Run(e:Event) {
                trace("running");
                this.addEventListener(Event.ENTER_FRAME, RandomRun);
            }
           
            private function RandomRun(e:Event) {  
                this.x += vx;
                this.y += vy;          
               
                if (this.x > right) {
                    this.x = right;
                    vx *= -1;
                }else if (this.x < left) {
                    this.x = left;
                    vx *= -1;
                }else if (this.y > bottom) {
                    this.y = bottom;
                    vy *= -1;
                }else if (this.y < top) {
                    this.y = top;
                    vy *= -1;
                }
               
            }
           

        }
    }

    and Bird.as:

    Actionscript Code:
    package {
       
        import flash.display.MovieClip;
        import flash.events.Event;
        import flash.net.URLLoader;
        import flash.net.URLRequest;
        import flash.display.Loader;
       
        public class Bird extends MovieClip
        {
            var imageLoader:Loader = new Loader();
            private var vx:Number = Math.random() * 10 - 5;
            private var vy:Number = Math.random() * 10 - 5;
           
            var left:Number = 20;
            var right:Number = 500;
            var top:Number = 20;
            var bottom:Number = 350;

            public function Bird () {
                trace ("bird created");        
                var image:URLRequest = new URLRequest("bird.png");
                imageLoader.load(image);
                this.addChild (imageLoader);                       
                this.addEventListener(Event.ADDED_TO_STAGE, Fly);
            }
           
            private function Fly(e:Event) {
                trace("flying")
                this.addEventListener(Event.ENTER_FRAME, RandomFly);
            }
                   
            private function RandomFly(e:Event) {  
                this.x += vx;
                this.y += vy;      
               
                if (this.x > right) {
                    this.x = right;
                    vx *= -1;
                }else if (this.x < left) {
                    this.x = left;
                    vx *= -1;
                }else if (this.y > bottom) {
                    this.y = bottom;
                    vy *= -1;
                }else if (this.y < top) {
                    this.y = top;
                    vy *= -1;
                }
            }
        }
    }


    Thanks for any input/ideas on this!!
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  3. #3
    Member
    Join Date
    May 2012
    Posts
    51
    That's pretty much how I would program it. Straight forward and clean. Nothing unnecessary. I would probably have the Cat and Bird class fire off CatEvent and BirdEvent instead of just trace statements. But then it's just an exercise after all.

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    I would use a design pattern and the most common one is Model-View-Controller. The reason to use such pattern is to get rid of most of what happens from the views, which are the cat, bird and house. Also when you look at the cat and bird, there is a functional code, which is repeated. Especially coding in Flash builder, design patterns are highly recommended. You should imagine the situation that you want to add dogs, fish, a tent and may be even people. Then design patterns become handy in that situation.
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    Senior Member
    Join Date
    Aug 2006
    Posts
    322
    Actionscript Code:
    private function catCollision(event:Event):void{
              //  for (var i:int = 0; i < cats.length; i++) {
                    if (cats[0].hitTestObject(cats[1])) {
                        trace("meow");
                    }
              //  }
            }
            private function birdCollision(event:Event):void{
               // for (var i:int = 0; i < birds.length; i++) {
                    if (birds[0].hitTestObject(birds[1])) {
                        trace("chirp");
                    }
              //  }
            }


    I din't understood the use of for loop in your collision.


    marlopax

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