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 7522755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:36:25+00:00 2026-05-30T02:36:25+00:00

Suppose my base class is the following: function Tile(obj) { //defaults (alot of variables

  • 0

Suppose my base class is the following:

function Tile(obj) {
    //defaults (alot of variables here like colors and opacity)
}
Tile.prototype.Draw = function () {
    ctx.fillStyle = "rgba(" + this.Red + "," + this.Green + "," + this.Blue + "," + this.Opacity + ")";
    ctx.fillRect(this.X, this.Y, this.Width, this.Height);
};

my class that inherits from the Tile class

Apple.prototype = new Tile();
Apple.prototype.constructor = Apple;
function Apple(obj) {
    Tile.apply(this, arguments);
}

So what I would like to do with my apple objects is have its opacity fluctuate between 0 and 1 so that it constantly fades in and out but I would like this to happen independently of the ‘game loop’. (So that the fading animation is smooth regardless of the game speed)

    function StartGameLoop() {
        console.log("*** Starting Game ***");
        gameLoop = setInterval(Game, gameSpeed);
    }

    function Game() {
        if (!IS_GAME_ON) {                          // Did the game end?
            EndGame();
        } else if (!PAUSE) {                        // Is the game not paused?
            console.log("Tick");
            Logic();                                // do any math
        }
    }

I can’t wrap my head around how to do this but I had an idea of putting a setInterval in the Apple constructor that calls the draw function over and over again but I can’t get it to work. Like this:

Apple.prototype = new Tile();
Apple.prototype.constructor = Apple;
function Apple(obj) {
    var AnimationDirection;
    var animate = setInterval(this.Draw, 50);
    Tile.apply(this, arguments);
}
Apple.prototype.Draw = function () {
    ctx.fillStyle = "rgba(" + this.Red + "," + this.Green + "," + this.Blue + "," + this.Opacity + ")";
    ctx.fillRect(this.X, this.Y, this.Width, this.Height);

    if (this.Opacity <= 0) {
        this.AnimationDirection = 0.1;
    }
    else if (this.Opacity >= 1) {
        this.AnimationDirection = -0.1;
    }

    this.Opacity += this.AnimationDirection;
};

It works as you’d expect when the first Apple.Draw() is called but the calls from the setInterval are not working properly. Any ideas?

(PS: Also the two ctx lines in the Draw function are repeated in both the Tile and the Apple classes, is there any way I can kick it to the Tile parent for the filling instead of having the code duplication?)

  • 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-30T02:36:27+00:00Added an answer on May 30, 2026 at 2:36 am

    I believe the cause is that when the Draw() function fires as part of the setInterval call, this isn’t what you think it is.

    Instead, use a variable inside your constructor to store the value of this when it refers to the Apple object created, and use an anonymous function for setInterval() to be able to refer to that new variable and call the Draw() function correctly on it, e.g. something like:

    function Apple(obj) {
        var AnimationDirection, me = this;
        var animate = setInterval(function() {
            me.Draw();
        }, 50);
        Tile.apply(this, arguments);
    }
    

    Since you are now calling the Draw() method on an Apple object (rather than the global window), this will correctly refer to that object.

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

Sidebar

Related Questions

Suppose we have the following class hierarchy: class Base { ... }; class Derived1
Suppose I have the following (trivially simple) base class: public class Simple { public
Suppose code like this: class Base: def start(self): pass def stop(self) pass class A(Base):
Suppose the following class hierarchy with a base class and some general case class
Suppose I have following code: public class CBase: AbstractC,IRenderable { //code here } public
Suppose I have the following class Foo, that supports a function of any arity
For example, suppose I have the following base class for which I cannot modify
Suppose that I have the following python base class: class BaseClass(object): def a(): This
Suppose I have a base class B, and a derived class D. I wish
Suppose I have a Base class and a Child class that inherits from Base

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.