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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:27:38+00:00 2026-06-13T15:27:38+00:00

This is my first SO post. I’m eternally grateful for the information this community

  • 0

This is my first SO post. I’m eternally grateful for the information this community has and shares. Thanks.

I’m coming from Flash and I’m not even sure what the right question to ask is. All I can do is lay out my code example and then explain what I am trying to do. I do not fully grasp the terms that I am trying to illustrate here so I feel it is best to omit them.

The code below is incomplete as it only includes the parts that I feel are relevant to my question. Please refer to the comments in my code to see my issue.

EDIT: Full source file here: [link removed] The console.log outputs the issue in question.

    <script type="text/javascript"> 
    var a_chests = [];
    var chestID = 0;

    //I'm creating a plugin to be able to make multiple instances
    (function ($) {
        $.fn.chestPlugin = function (option) {
            //This function creates a master sprite object which many of my sprites will use
            //I've simplified the features to get to the heart of my question
            var DHTMLSprite = function (params) {
                var ident = params.ident,
                var that = {
                    getID: function(){
                        return ident;
                    }
                };
                return that;
            };

            //ChestSprite inherits DHTMLSprites properties and then adds a few of its own
            var chestSprite = function(params) {
                var ident = params.ident,
                that = DHTMLSprite(params);
                that.reveal=function(){
                    console.log(ident);
                };

                return that;
            };

            //Here I create multiple instances of the chests
            var treasure = function ( $drawTarget,chests) {
                for (i=0;i<chests;i++){
                    var cs = chestSprite({
                        ident: "chest"+chestID
                    })
                    console.log(cs.reveal()) 
                    //This logs "chest0", "chest1", "chest2" as the for loop executes
                    //This behavior is correct and/or expected!

                    a_chests[chestID]={id:i,ob:cs};
                    //I add a reference to the new chestSprite for later

                    chestID++;
                    //increment the chestID;
                }
                console.log(a_chests[1].ob.reveal());
                //This always logs "chest2" (the last chest that is created), even though
                //the logs in the for loop were correct. It seems it is referencing the
                //DHTML object (since the DHTMLSprite function returns that;) and since 
                //there is no reference to which chest I need, it passes the last one.

                //Is there any way I can pass a reference to DHTMLSprite in order to retain
                //the reference to the three individual chests that are created?

                //Is there another solution altogether? Thanks!!!
            };

            //The rest of the code.
            return this.each(function () {
                var $drawTarget = $(this);
                treasure($drawTarget,3);
            });
        };
        })(jQuery);


        </script>
  • 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-13T15:27:39+00:00Added an answer on June 13, 2026 at 3:27 pm

    You forgot to declare `that’ as a local variable, so it’s being overwritten on each iteration.

        var chestSprite = function(params) {
          var that;
          var animInterval;
          ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post on StackOverflow. This community has provided me with some
this is my first post.. so I'm learning Android & Java (coming from Actionscript),
this is my first post. I am selecting some fields from a database which
this is my first post in the community and a have a problema: represent
this is my first post here. I'm using an existing Flash widget but would
This is my first post to I apoligise if its not perfectly put. Essentially
this is my first post on stackoverflow. Hopefully I am not disturbing anybody... :)
This is my first post so I hope I am not violating any rules.
This is my first post on SO, so be gentle. I'm not even sure
this is my first post on this great source of programming information. I have

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.