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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:55:41+00:00 2026-06-13T08:55:41+00:00

I’m starting on a small HTML5 game and it’s the first one I’m doing

  • 0

I’m starting on a small HTML5 game and it’s the first one I’m doing on. At the moment, I’m creating a function that holds variables, I don’t really know what it’s called, I just remember how to do it from a tutorial. Anyway, I’m trying to set up some of the variables using ‘this’ but it doesn’t seem to get a value. Here’s the code that I think is causing the problem:

function ship() {
    this.width = 100;
    this.height = 75;
    this.drawX = gameWidth/2 - this.width/2;
    this.drawY = gameHeight + this.height;
}

this.drawX and Y however, seem to not get a value. Also, I’m sure that gameHeight and gameWidth have a value.

Edit: Here is the draw function and rest of the code:

var player = new ship();

var gameWidth  = canvasShip.width;
var gameHeight = canvasShip.height;

var sprites = new Image();
sprites.src = 'images/spriteSheet.png';

var canvasPlayer = document.getElementById('canvasShip');
var ctxPlayer = canvasPlayer.getContext('2d');

//Clear any canvas
function clearCtx(canvas) { 
    canvas.clearRect(0,0,gameWidth,gameHeight);
}

//Ship object, the player
function ship() {
    this.width = 100;
    this.height = 75;
    console.log(gameWidth,gameHeight);
    this.drawX = gameWidth/2 - this.width/2;
    this.drawY = gameHeight + this.height;
}

ship.prototype.draw = function() {
    clearCtx(ctxPlayer);
    ctxPlayer.drawImage(sprites,0,0,this.width,this.height,this.drawX,this.drawY, this.width, this.height);
}
  • 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-13T08:55:47+00:00Added an answer on June 13, 2026 at 8:55 am

    This part in your code looks fishy:

    var player = new ship();
    
    var gameWidth  = canvasShip.width;
    var gameHeight = canvasShip.height;
    

    Inside the constructor of ship(), the values of gameWidth and gameHeight would be undefined.

    Reversing the statements should work as expected:

    var gameWidth  = canvasShip.width,
    gameHeight = canvasShip.height,
    player = new ship();
    

    This is the way that JavaScript sees your code:

    var gameWidth, gameHeight, player, ship;
    
    ship = function() { ... }
    
    player = new ship();
    gameWidth = 123;
    gameHeight = 456;
    

    The declaration of ship() gets moved up in a process sometimes referred to as “hoisting“, making it more obvious why it behaves this way.

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

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm making a simple page using Google Maps API 3. My first. One marker
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 used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace

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.