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

  • Home
  • SEARCH
  • 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 6552891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:30:55+00:00 2026-05-25T12:30:55+00:00

I am writing a card game using javascript/html5 I get the gamestate as an

  • 0

I am writing a card game using javascript/html5

I get the gamestate as an ajax request. this is JSON data that lists the players and what cards they have in their hand

I am trying to loop over each player and set the hand data as follows

gameState.Players.forEach(function (player, i) {    
    var inx = i + 1;
    var canvas = document.getElementById("player" + inx);
    var ctx = canvas.getContext("2d");

    var hand = Object.create(Hand);
    hand.initialiseHand(player.Hand);
    hand.setPosition(10, 10);
    hand.draw(ctx);
});

There are six canvases on the page. One for each player

I am using Object.create to create a new “instance” of a hand. And then calling the draw method, which lays out the images on the canvas

However, what actually happens is that each players data just gets added to the same instance

i.e. each time I go round the forEach loop, the hand object just gets assigned more and more cards

I would like to have a seperate instance for each player

So how do I achieve this?

I want to loop over the data and create a new hand for each iteration of the loop

I am guessing that the hand variable has been hoisted out of the loop and so I get the same one each time?

and this is what Hand looks like

var Hand = {

    faceDownCards: [],
    faceUpCards: [],
    inHandCards: [],

    initialiseHand: function (handData) {
        handData.FaceDownCards.forEach(function (c, i) {
            this.faceDownCards.push(Object.create(Card, pd({ rank: c.Rank, suit: c.Suit })));
        }, this);

        handData.FaceUpCards.forEach(function (c, i) {
            this.faceUpCards.push(Object.create(Card, pd({ rank: c.Rank, suit: c.Suit })));
        }, this);

        handData.InHandCards.forEach(function (c, i) {
            this.inHandCards.push(Object.create(Card, pd({ rank: c.Rank, suit: c.Suit })));
        }, this);

    },

    draw: function (context) {
        this.faceDownCards.forEach(function (c, i) {
            c.draw(context);
        });

        this.faceUpCards.forEach(function (c, i) {
            c.draw(context);
        });

        this.inHandCards.forEach(function (c, i) {
            c.draw(context);
        });
    },

    setPosition: function (x, y) {
        this.x = x;
        this.y = y;
        this.faceDownCards.forEach(function (c, i) {
            c.setPosition(x + i * 70, y);
        });
        this.faceUpCards.forEach(function (c, i) {
            c.setPosition(x + i * 70, y + 60);
        });
        this.inHandCards.forEach(function (c, i) {
            c.setPosition(x + i * 20, y + 80);
            //c.rotation = 3;
        });
    }
};
  • 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-25T12:30:55+00:00Added an answer on May 25, 2026 at 12:30 pm

    Your hand variable is not being hoisted out of the loop. Javascript variables have function scope and you are already conveniently using a function inside your forEach loop.


    What is Hand? The usual convention is having capitalized names represent constructor functions, but the Players and the Object.create make it look like Hand is just an object?

    If Hand is already an object (and not a Constructor you are misusing) my best bet would be that initializeHand/setPosition are seting closed-over variables instead of accessing them via this. (Of course, this is just a wild guess without looking at the Hand code)


    After looking at your Hand code, I now think the problem is that the hands are sharing the faceDownCards, etc, arrays. The base prototype should only be used for shared traits and the arrays and other instance-olny state should be set on initialization :

    For a concrete example, change

    handData.FaceDownCards.forEach(function (c, i) {
        this.faceDownCards.push(Object.create(Card, pd({ rank: c.Rank, suit: c.Suit })));
    }, this);
    

    to

     this.faceDownCards = handData.FaceDownCards.map(function (c, i) {
         return Object.create(Card, pd({ rank: c.Rank, suit: c.Suit }));
     });
    

    ps.: All the Object.create going around is not exactly idiomatic Javascript. But then, I guess you should have an Idea of what you are doing, right?

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

Sidebar

Related Questions

So I am writing a class that plays the card game war. I have
I am writing card game using Silverlight and HttpPollingDuplex . I have 4 seperate
I am writing a card game in java where I need to spread cards
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
I'm writing an AI for a card game and after some testing I've discovered
I spent a month writing an elaborate payment system that handles both credit card
I am writing a code that collects all the credit card information of each
I am writing a program in Mips that is given this asciiz string: .asciiz
I am writing a program that and like to implement data verification system. It
I'm writing a card game. Right now I'm having problems with mouse handling. Below

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.