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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:37:41+00:00 2026-06-01T20:37:41+00:00

I was playing around with making game in JS. And hit a brick wall

  • 0

I was playing around with making game in JS. And hit a brick wall which is inability to change a variable from an event in the main html file. Namely speaking offSetX. Why it doesn’t change?

var game = new Game();
window.addEventListener("keyup", game.input);
game.start('myCanvas');

The game object looks like this:

function Game() {
    
    this.offSetX = 0;

    this.init = function (id) {

        this.canvas = document.getElementById(id);
        this.context = this.canvas.getContext('2d');
        this.blocks = [];
        this.blocks.push(new block());

    };

    this.logic = function () {
        for (var i in this.blocks) {
            this.blocks[i].update(this.offSetX);
        }
    };

    this.draw = function () {
        for (var i in this.blocks) {
            this.blocks[i].draw(this.context);
        }
    };

    this.main = function () {
        this.logic();
        this.draw();
        console.log(this.offSetX);
    };
    this.input = function (key) {

        if (key.keyCode == 37) {
            this.offSetX--;
            console.log(this.offSetX);
        }
        if (key.keyCode == 39) {
            this.offSetX++;
            console.log(this.offSetX);
        }


    };


    this.start = function (id) {
        var _this = this;

        this.init(id);
        this.interval = setInterval(function () {
            _this.canvas.width = _this.canvas.width;
            _this.main();
        }, 30);
    }
};
  • 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-01T20:37:44+00:00Added an answer on June 1, 2026 at 8:37 pm

    Try this:

    window.addEventListener("keyup", function(key){
       game.input.apply(game,[key]);
    });
    

    The problem was by window.addEventListener("keyup", game.input) line , you are adding handler for window object, that’s why in input method , "this" is window object(which does not have any "offSetX" method), not the game object.

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

Sidebar

Related Questions

Ok, so I'm making an html5 canvas/javascript game. I'm playing around with the idea
I'm playing around with making a simple terminal-based game with Ruby, and I'm currently
I was playing around making a simple haiku site using sqlalchemy and pylons. It
I was playing around the other day with making a class to handle some
I am playing around with a little drawing app I am making and was
I have been playing around with various methods of making the Visitor pattern in
I've been playing around with Knockout recently and I'm trying to replace a $().change
I am in process of making a small game, where the main Gameplay Layer
I've been playing around with making my mac a webserver, and now I'm trying
So, I was just playing around making some page for fun. I cannot seem

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.