Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript Prototype Problems
    primarykey
    data
    text
    <p>My situation is that I am working on a Canvas game using Box2DWeb, so I have a class called <em>Sprite</em> that draws an image to the Canvas, I now want to create a <em>PhysicsSprite</em> class that inherits methods and properties from <em>Sprite</em>.</p> <p>My <em>Sprite</em> class (Sprite.js):</p> <pre><code>Sprite = function (position, dimension, image) { this.Position = position; this.Dimension = dimension; if (image) this.Image = image; ... this.Draw = function (g) { ... } ... }; </code></pre> <p>And I am setting the prototype in <em>PhysicsSprite</em> (PhysicsSprite.js) like so:</p> <pre><code>PhysicsSprite = function(position, dimension, world) { this.prototype = new Sprite(position, dimension, undefined); //alert(this.prototype.Position.X) var body = CreateBody(); this.GetBody = function () { return body; } this.Draw = function (g) { ... } function CreateBody () { ... } }; </code></pre> <p>Note the <strong>alert(this.prototype.Position.X)</strong>, which does alert me of the position, but if I change the code to <strong>this.Position.X</strong>, I get an error, and similarly, if I have an instance of <em>PhysicsSprite</em>, I have to explicitly call the prototype.</p> <p>This lead me to think that I haven't set the Object's prototype, merely created a property called <code>prototype</code>, and set that to a new <em>Sprite</em>.</p> <p>If someone could help me, and explain what I am doing wrong, that would be much appreciated. I am dyslexic, so I always misspell variables, which is frustrating, so that was the first thing I looked for, but everything looks fine to me.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload