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

The Archive Base Latest Questions

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

I am trying new things with arrays and having some difficulty. I am trying

  • 0

I am trying new things with arrays and having some difficulty. I am trying to create multiple instances of 1 class and putting them into an array.

I am creating the instances like so:

public function creatingitem(e:TimerEvent)
    {
    amtcreated = Math.ceil(Math.random() * 4);
    while (amtcreated >= 1)
            {
                amtcreated--;
                var i:Number = Math.ceil(Math.random() * 3);
                switch (i)
                {
                    case 1 :
                        //Object1
                        objectnum = 1;
                        objectwei = 3;
                        r = new Board(objectnum,objectwei,stagw,stagh);
                        addChild(r);
                        fallingitem.push(r);
                        break;
                    case 2 :
                        //Object2
                        objectnum = 2;
                        objectwei = 4;
                        c = new Board(objectnum,objectwei,stagw,stagh);
                        addChild(c);
                        fallingitem.push(c);
                        break;
                    case 3 :
                        //Object3
                        objectnum = 3;
                        objectwei = 4;
                        l = new Board(objectnum,objectwei,stagw,stagh);
                        addChild(l);
                        fallingitem.push(l);
                        break;
                    default :
                        break;
                }
            }
}

Once these are created they check if they collide with the main ball:

        public function hitcheck(e:Event)
    {
        for (var v:int = fallingitem.length - 1; v >= 0; v--)
        {
            if (ball.hitTestObject(fallingitem[v]))
            {
                                 trace(fallingitem[v]);
                if (fallingitem[v] == r)
                {
                    bonusscore +=  100;
                    fallingitem[v].removeitem();
                }
                else if (fallingitem[v] == c)
                {
                    bonusscore +=  75;
                    fallingitem[v].removeitem();
                }
                else if (fallingitem[v] == l)
                {
                    bonusscore +=  75;
                    fallingitem[v].removeitem();
                }

trace(bonusscore);
            }
        }
    }

The issue is I am seeing every item getting hit due to the trace function. Not all instances are meeting the if conditions. As an example I could have 2 “r” instances and when I hit both 1 will go through and add to the score and the other will just continue past. The trace directly following the hitTestObject shows me that both are being hit and registered but I am not sure why it does not add score.

Thank you,

  • 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-06T15:13:53+00:00Added an answer on June 6, 2026 at 3:13 pm

    You can’t really have 2 r instances. When you’re creating the instances, if you happen to create 2 rs, the second r = new Board... statement overwrites the reference, and the variable r is referring to the second one. Both objects still exist, but the variable can only refer to one of them, so when you perform the check, you’re ignoring the object that was previously set to r but isn’t any more.

    To fix this, you could turn r, c and l into Arrays and whenever you create an instance, add it to the appropriate array. Then, you would perform the check using (r.indexOf(fallingitem[v]) != -1), which returns true if the object is in the array.

    The other way, based on the provided code, would be to check whatever value objectnum is setting in the constructor, since you’re setting that value based on whether it’s in the r, c or l category. Though, that won’t work if the property is private or might be changed.

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

Sidebar

Related Questions

I am new to javascript and I'm trying some different things. Take this following
I am new to rails and am having troubles figuring out some things with
I am new to bash scripting and trying to learn a few things. Here
I am trying something new and I am having issues with my current idea.
I am new to trying to create a book app adapting the page-based application
I am trying to establish new SDL Tridion UI 2012 functionality on some test
I'm trying to learn more about arrays since I'm new to programming. So I
I am trying to load a CSV file into an array using ColdFusion (version
I've been having some problems while trying to fix a simple video recording app*.
I looked into the implementation of Array.Resize() and noticed that a new array is

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.