Results 1 to 9 of 9

Thread: Questions about using OOP Classes Inheritence

  1. #1

    Questions about using OOP Classes Inheritence

    PROBLEM SOLVED! THANK YOU: brart, TheCanadian, Krilnon

    Intro.(Thank You)
    Hello, thank you for taking the time to read this. I really appreciate you taking the time to try to assist me. I'm new to the forum and somewhat new to programming and have run into a mental wall.

    Background.(Still Learning)
    I have made a simple game for the purpose of furthering my knowledge on programming. It works as I expected but I would like to expand it further while using OOP which I still am having trouble grasping how to implement it. The concept I understand(I think). In the game I create an object and add it too the stage and would like to create more objects of a different type with similar function so converting the object into a super class and a sub class is the next logical step for me.

    Problem.(Help)
    I need to convert class Original into class Super and Class Sub.

    Questions.(???)
    [X] What does the Super class constructor need if I don't call it? OR Do I need to call it?
    [X] Do variables pass from Super to Sub?
    [X] Do I need to import my Super Class in my Sub class?
    [X] Does my pseudo code have any flaws in logic?
    [X] 1203: No default constructor found in base class?


    Pseudo Code.(//)

    Code:
    public class Original() extends MovieClip
    {
        private var a:Number;
        public function Original()
        {
            One(a);
            Two(a);
        }
        private function One(a:Number)
         {
              //Does Something with a
         }
        private function Two(a:Number)
         {
                //Does Something with a
         }
    }
    ...To be turned into...
    Code:
    public class Super() extends MovieClip
    {
        public var a:Number;
        public function Super()
        {
        
        }
        public function One(a:Number)
         {
                 //Does Something with a
         }
        public function Two(a:Number)
         {
                 //Does Something with a
         }
    }
    
    public class Sub() extends Super
    {
        
        public function Sub()
        {
            One(a);
            Two(a);
        }
    
    }
    CODE.(#)

    From Engine
    Code:
    ...
    unitThree = new SubCreature(stage);
    ...
    From "Super"
    Code:
    package com.tisman.foodchain
    {
        
        import flash.display.MovieClip;
        import flash.display.Stage
        import flash.events.Event;
        import flash.utils.Timer;
        import flash.events.TimerEvent;
        
        public class Creature extends MovieClip 
        {
            //MISC
            public var stageRef:Stage;
            //Variables For AI
            public var up:Boolean = false;
            public var left:Boolean = false;
            public var right:Boolean = false;
            public var turnTimer:Timer;
            public var canTurn:Boolean = true;
            // Variables for Movement.
            public var speed:Number = 0.3;
            public var rotateSpeed:Number = 2;
            public var vx:Number = 0;
            public var vy:Number = 0;
            public var friction:Number = 0.95;
            public var maxspeed:Number = 1;
            //Variables For Collision
            public var size:Number = 0;
            public var growth:Number = 0;
            public var growthRate:Number = 2;
            public var kills:Number = 0;
    
            public function Creature(stageRef:Stage) : void
            {
                this.stageRef = stageRef;
                addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
                turnTimer = new Timer(700, 1);
                turnTimer.addEventListener(TimerEvent.TIMER, turnTimerHandler, false, 0, true);
            }
     
            private function loop(e:Event) : void
            {
                if (canTurn == true)
                {
                    
                    aiDecision(this, Engine.unitList);
                    canTurn = false;
                    turnTimer.start();
                }
                movement();
                collisions();
            }
    From Sub
    Code:
    package com.tisman.foodchain
    {
        import flash.display.Stage
        
        public class SubCreature extends Creature
        {
            public function SubCreature(stageRef:Stage): void
            {
                super(stageRef);
            }
            
        }
    }
    Last edited by legend_tisman; January 30th, 2011 at 01:07 AM. Reason: Clarity // Problem Progression // More Info

  2. #2
    If Original extends Super and Sub extends Original, the above code will give an error(duplicate function). To override an function, you should use the override tag. If you have done that in the original class. Than sub will only call those 2 functions from the original class.

    Super
    function One
    function Two

    Original extends Super
    override function One

    Sub extends Original
    Can call to the One function from Original and the Two function from Super.

  3. #3
    Actually I'm trying to make original obsolete and replace it with Super and Sub. I'm sorry if I'm being unclear I tried to explain it as best I could.
    Original extends movie clip.
    Super extends movie clip.
    Sub extends Super.

    Sorry, I edited it to reflects this. Thank you for your reply though.
    Last edited by legend_tisman; January 29th, 2011 at 08:19 PM.

  4. #4
    What does the Super class constructor need if I don't call it? OR
    Do I need to call it?
    Do variables pass from Super to Sub?
    Does my pseudo code have any flaws in logic?

    You don't need to call it, but if your constructor has the same name as the class you could call it with super();

    Yes they do.

    You should add an override tag to the second function called constructor.

  5. #5
    With the Constructors named after the classes they should run properly then?

    Something seems to be missing.
    When I call Super it works but when I call Sub I get an error.
    1203: No default constructor found in base class Super.

    I have a couple new questions that I can't find the answer to. Searching the forums led to an example using ships which was quite informative but I am failing to solve my problem. I posted my partial code as it stand right now. Hopefully someone can point me in the right direction?

  6. #6
    TheCanadian's Avatar
    10,302
    posts
    Noo doot aboot it, eh?
    Quote Originally Posted by legend_tisman View Post
    [X] What does the Super class constructor need if I don't call it? OR Do I need to call it?
    It will get called automatically when a one of its sub class constructors is called. You can use the super function if you want to control where it gets called in the constructor or if you need to pass arguments to it.

    Quote Originally Posted by legend_tisman View Post
    [X] Do variables pass from Super to Sub?
    Yes unless they or private. Or if they are internal and the subclass is in another package. Or if they are in any other namespace that the subclass doesn't have access to.

    Quote Originally Posted by legend_tisman View Post
    [?] Do I need to import my Super Class in my Sub class?
    Only if they are in different packages.


    I haven't really looked at this in detail, but your subclass should look like this:
    Code:
    package com.tisman.foodchain
    {
        import flash.display.Stage
        
        public class SubCreature extends Creature
        {
            public function SubCreature(stageRef:Stage): void
            {
                super(stageRef);
            }
            
        }
    }
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

    Maybe getTimer() or TweenMax is the answer to your problem . . .

  7. #7
    Thank you very much your answers were very clear and informative.
    My code does look like that and returns the error with the offending line see below.

    Code:
    package com.tisman.foodchain
    {
        import flash.display.Stage
        
        public class SubCreature extends Creature 
        //^1203: No default constructor found in base class com.tisman.foodchain:Creature.
        {
            public function SubCreature(stageRef:Stage): void
            {
                Creature(stageRef);
            }
            
        }
    }

  8. #8
    super() is somewhat of a keyword (or at least a special production in the language grammar). TheCanadian wasn't just using it as a generic way to refer to the name of whatever class is the superclass in your code (although that is its purpose in the language).

    So, you'd need super(stageRef) and NOT Creature(stageRef).

  9. #9
    The problem is solved!
    Thank you all so much.
    Silly mistake at the end lol. You are a life saver for catching my stupidity.
    Hopefully my error helps someone down the line.
    Last edited by legend_tisman; January 30th, 2011 at 01:04 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012