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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:37:05+00:00 2026-05-25T18:37:05+00:00

I have written a checkers game model which has a Board object that holds

  • 0

I have written a checkers game model which has a Board object that holds a bunch of Piece objects. The Piece objects have an event (pieceMoved) which they fire each time they are moved.

I am using EaselJS to render this game model in a canvas element.

The first thing I do is create an easeljs shape object to graphically represent each piece.

I wish to handle the pieceMoved event on the pieces’ corresponding shape objects so that I can update the shapes location.

The code where I create the shapes and assign the event handlers looks something like this –

for (x=0;x<=(board_.getColCount()-1);x++) {
for (y=0;y<=(board_.getRowCount()-1);y++) {

var piece = board_.getPieceAt(x, y);

    if (!piece) continue;

    var circ = new Shape();

    circ.eventHandler = function(pieceObj) {
        this.x = pieceObj.x*squareSize;
        this.y = pieceObj.y*squareSize;
    }

    piece.addEventHandler(circ.eventHandler);

    ....

The problem is that the ‘this’ keyword inside the event handler doesn’t refer to the right thing. I also tried the following –

piece.addEventHandler(function(px) { return circ.eventHandler; });

But again, ‘this’ refers to the wrong object.

What is the proper way to do something like this?

  • 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-25T18:37:05+00:00Added an answer on May 25, 2026 at 6:37 pm

    In an event handler, this refers to the object which fired the event. There are ways around it, but my philosophy is that you should embrace the convention rather than fight against it. So, just separate the method from the event handler:

    piece.addEventHandler (function (e) {
        circ.eventHandler(e);
    });
    

    In your case, since it looks like you have looping issues, create a handler factory:

    function getHandler (circ) {
        return function(e) {
            circ.eventHandler(e);
        };
    }
    for (var i = 0; i < circles.length; i++) {
        piece.addEventHandler(getHandler(circles[i]));
    }
    

    Edit: Prettify it with Array.forEach(). I’m guessing that circles is not an Array, so you’ll probably have to use.call():

    [].forEach.call(circles, function(circ) {
        piece.addEventHandler(function(e) {
            circ.eventHandler(e);
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have written this little class, which generates a UUID every time an object of
I have written a WCF service with the REST template that has the defaultOutgoingResponseFormat
I have written come code that has worked on both Windows XP and Windows
I have written a bash script which installs a number of packages, however for
I have written a function that sorts a big scale of data. To test
I have written a function that extract the domain from hostname. e.g. www.domain.com ->
I have written a very simple CTE expression that retrieves a list of all
I have written a raw binary image file into a buffer which is an
have written a stochastic simulation in Java, which loads data from a few CSV
I have written a web-service (in Java) which produces as a result a (rather

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.