Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7630239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:02:59+00:00 2026-05-31T06:02:59+00:00

It’s a bit basic question. But, I fail to understand how to solve it.

  • 0

It’s a bit basic question. But, I fail to understand how to solve it. I have an application which has several enteties. E.g. pike <– fish –> shark. Where fish is a base class.

I am doing some kind of lake, so all my instances of fish class can move only within some boarders. Even more, they all are randomly created on one of the edges of the lake and swim to another depending on the edge they are located at. Here is how it looks for a base class:

public class Fish extends FlxSprite
{

    [Embed(source="./resources/Elipse.png")]
    public var image:Class;

    // Variable which stores the direction of item movement (false - Move right)
    // (true - Move Left)
    private var movement:Boolean;

    public function Fish(x:int, y:int)
    {
        super(x, y, image);

        // Move right
        if ( x < 5 )
        {
            movement = false;
        }
        else
        {
            movement = true;
        }
    }



    public override function update():void
    {
        if( movement )
        {
            this.velocity.x -= 3;
        }

        else
        {
            this.velocity.x += 3;
        }

        super.update();
 }

Now I want to extend this model to add some extra behaviour to the fish class. E.g. I want to make a shark. To do so I need to:

1) Replace the image of abstract fish with an image of shark

2) Change the behaviour a bit (shark still needs to know about direction but maybe use another approach to velocities)

3) Define size property (e.g. based on size the shark would be stronger or weaker)

The question is:

How do I define a shark… As a subclass of Fish? Or it’s better to subclass it from Sprite?

I thought that subclassing from Fish would be more natural way, but in this case, I do not understand how to override the visual presentation of the Fish (e.g. image), as it’s impossible to override a variable.

From the other hand if I will use FlxSprite as a base class, it would be easy to redefine the presentation, but in the case I would not be able to reuse the movement code (e.g. so fish knows in which direction to swim)…

I understand that the question is a bit hard to understand. Happy to re-define it in case of any questions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:03:00+00:00Added an answer on May 31, 2026 at 6:03 am

    You can’t override a variable (it wouldn’t make any sense), but you can set its value in the constructor.

    in Fish:

    public var image:Class;
    

    in Shark:

    [Embed(source="./resources/shark.png")]
    private const _sharkImage:Class;
    
    public function Shark(x:int, y:int)
    {
      image = _sharkImage;
      super(x, y);
    }
    

    You could also use a getter.

    In Fish:

    [Embed(source="./resources/Elipse.png")]
    public var _image:Class;
    
    public function get image():Class
    {
      return _image;
    }
    

    and then in Shark:

    [Embed(source="./resources/shark.png")]
    private const _sharkImage:Class;
    
    override public function get image():Class
    {
      return _sharkImage;
    }
    

    I think overriding getters and setters is a good option for you. You could then change the behavioral velocity/size for each species.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.