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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:46:51+00:00 2026-05-13T06:46:51+00:00

So, i’m attempting simple card game. I have player class with draw function, and

  • 0

So, i’m attempting simple card game. I have player “class” with draw function, and public members deck and hand, both are Arrays.

I need to draw a card from the deck, put it in hand and show it in player “hand” area. I’m concerned about the way I do “flip” and “play” buttons (through closures).

Here is the code:

littlegame.player.prototype.draw = function() {
    if (this.canDrawCard()) {
        var card = this.deck.draw(); // this.deck is Array

        //Create card element on the playfield

        var card_object = $('<div class="card"></div>').append($('<span class="attack">' + card.attack + '</span>')).append($('<span class="defence">' + card.defence + '</span>'));

        // Add controls to card
        if (this.playerid == 1) {
            var flipper = $('<span class="flip">Flip</span>');
            flipper.click(function(){
                card.flip();
            });

            var actuator = $('<span class="play">Play</span>');
            console.log('Loading actuator closure with id ' + this.playerid + ' and name ' + this.playername);
            var player = this;

            var old_hand = this.hand.slice(0); // Store a copy of old hand. Stupid trick, i know. It doesn't work

            actuator.click(function(){
                card.play(player.playerid);
                delete card;
                player.hand = old_hand;
            });

            card_object = card_object.append(flipper).append(actuator);
        }

        this.element.append(card_object);
        card.element = card_object;

        // Put card in hand
        this.hand.push(card);
    }
};

What i need is a way to call card.play() and card.flip() on corresponding button presses, with card.play knowing card position in hand, to remove the card. How can I do 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-13T06:46:51+00:00Added an answer on May 13, 2026 at 6:46 am
    littlegame.player.prototype.draw = function() {
            if (this.canDrawCard()) {
                    var card = this.deck.draw(); // this.deck is Array
    
                    //Create card element on the playfield
    
                    var card_object = $('<div class="card"></div>').append($('<span class="attack">' + card.attack + '</span>')).append($('<span class="defence">' + card.defence + '</span>'));
    
                    // Add controls to card
                    if (this.playerid == 1) {
                            var flipper = $('<span class="flip">Flip</span>');
                            flipper.click(function(){
                                    card.flip();
                            });
    
                            var actuator = $('<span class="play">Play</span>');
                            console.log('Loading actuator closure with id ' + this.playerid + ' and name ' + this.playername);
                            var player = this;
    
                            var old_hand = this.hand.slice(0); // Store a copy of old hand. Stupid trick, i know. It doesn't work
    
                            actuator.click(function(){
                                    card.play(player.playerid);
                                    delete card;
                                    player.hand = old_hand;
                            });
    
                            card_object = card_object.append(flipper).append(actuator);
                    }
    
                    this.element.append(card_object);
                    card.element = card_object;
    
                    // Put card in hand
                    this.hand.push(card);
                    var hand = this.hand;
                    card.remove = function () {
                         var i;
                         for(i=0;i<hand.length;i++) {
                           if(hand[i]===this) {
                               hand.splice(i,1);
                           }
                         }
                    }
            }
    };
    

    The key here is to define the remove function in the scope that contains the variables you’re interested in. Here, I define the variable hand, which makes it available inside the remove function that I define immediately after. You can then call the remove function whenever you like.
    If you know that the card will not change position within the hand, you can shortcut by making the index some variable (say, cardposition) and simply splice your array there, or whatever it is you want to do with your array.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.