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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:31:38+00:00 2026-06-12T03:31:38+00:00

Hi I have 3 javascript files: Game.js, Player.js, Drawable.js. Now in Game.js I want

  • 0

Hi I have 3 javascript files: Game.js, Player.js, Drawable.js.
Now in Game.js I want to create an object _player, that is Drawable and than that is Player. This means that _player is a Player Object, that is a class that EXTENDS Drawable.

Drawable.js

function Drawable(x, y, src)
{
  this.x = x;
  this.y = y;
  this.img = new Image();
  this.img.src = src;
  this.width = this.img.width;
  this.height = this.img.height;

  this.draw = function(canvas) 
  {   
    canvas.drawImage(this.img,this.x, this.y);
  }

  this.midpoint = function() 
  {
    return {
      x: this.x + this.width/2,
      y: this.y + this.height/2};
    }

  }
}

Player.js

function Player()
{
  this.moveLeft = function()
  {
    this.x -= 3;
  }

  this.moveRight = function()
  {
    this.x += 3;
  }

  this.moveUp = function() 
  {
    this.y -= 3;
  }

  this.moveDown = function()
  {
    this.y += 3;
  }
}

Game.js

var _player;

 _player = new Player();

 _player.draw(...);
 _player.moveLeft();
 ...
 ...

This is what I want to do. I’ve tried to put Player.prototype = new Drawable; but It doesn’t work. How can I do?

  • 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-06-12T03:31:40+00:00Added an answer on June 12, 2026 at 3:31 am

    If you want player objects to behave such that you can call

    player.draw(...);
    player.moveLeft(...);
    

    then the draw and moveLeft functions need to be stored as properties in every player object (yikes) or somewhere on a player object’s prototype chain.

    Which brings up one comment on your actual code: those functions should be in prototypes, not the actual objects.

    So, to answer your actual problem…

    • The first thing you should do is place the methods in prototypes.

    • Then you need to make the prototype of a player’s prototype be the prototype for all drawables.

    That may sound confusing, but here is the money line:

    Player.prototype = Object.create(Drawable.prototype);
    

    Do this before creating players with new Player. 🙂

    ADDENDUM

    Live Demo — which also shows you how to do the “constructor” thing so you can build a player with an initial x and y.

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

Sidebar

Related Questions

I have several javascript files that during run-time get combined and minified. This is
I have three javascript files that I want to merge into a single file.
I have recently noticed that a lot of JavaScript files on the Web start
I have some pages that reference javascript files. The application exists locally in a
I have a class that stores paths to CSS and Javascript files in arrays.
We have javascript files that are environment specific, and so I was thinking of
I have two JavaScript files that contain a jQuery function with the same name
I want to create a simple javascript life game implementation. I want user to
I have a game built in PHP and javascript that allows users to spin
I have several large Javascript files that I need to document/digg into. Unfortunately I

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.