1 2 Previous Next 67 Replies Latest reply on May 12, 2017 10:26 PM by kglad

    Error using Arrays

    brendenc14282108

      I am trying to use Array to count my boxMC. my boxMC is on my background of WorldMC. Its saying that my line of

      "for (var count=0; count<boxMC.length; count++){

           boxMC[count].update();

      }"

      Has no default value

       

      Here is my error:

      ReferenceError: Error #1069: Property update not found on tankscript.turrent.Box and there is no default value.

       

      var boxMC:Array;

       

        boxMC = new Array();

       

        //------------------------------

        // -----Box Spawning------------

        //------------------------------

        //only spawn a box if there are less tan 100 already on screen

        while(WorldMC.numChildren<10)

        {

        // Make a new instance of the box class

        var bx = new Box();

        // add the box to the display list

        WorldMC.addChild(bx);

        //position and rotate the Box;

        bx.x = Math.random() * -1100;

        bx.y = Math.random() * 1;

        bx.rotation = 40;

        // add the box to list of box

        boxMC.push(bx);

       

        }

        for (var count=0; count<boxMC.length; count++){

        boxMC[count].update();

        }

      }

        • 1. Re: Error using Arrays
          kglad Adobe Community Professional & MVP

          if that error message is related to the code you showed (which is not clear because the error contains a typo), either you don't have an update() method defined for your array members, or you do and you need to explicitly cast them as the class type that has a well-defined update method.

           

          eg, in your Box class, if you have an update method defined, use:

           

          Box(boxMC[count]).update();

          1 person found this helpful
          • 2. Re: Error using Arrays
            brendenc14282108 Level 1

            Yes sorry i do have a Box class. in which the update(); goes to. I tried your code you gave me and it came back with:

            1195: Attempted access of inaccessible method update through a reference with static type tankscript.turrent:Box.

             

            Would it help more if i showed my Box class code?

            • 3. Re: Error using Arrays
              Colin Holgate MVP & Adobe Community Professional

              Make sure the update function is public and not private.

              • 4. Re: Error using Arrays
                kglad Adobe Community Professional & MVP

                at least, show your update() method to confirm it's accessible from wherever the problematic code is located.

                1 person found this helpful
                • 5. Re: Error using Arrays
                  brendenc14282108 Level 1

                  hm.. I did so, i do not got the error of no default value now.  but my boxMC do not seem to like to update and move now. i used the code of kglad suggested too

                   

                  Heres the Box class:

                   

                  package tankscript.turrent

                  {

                    // import any necessary files/libaries

                    import flash.display.MovieClip;

                    import flash.events.Event;

                    import flash.geom.Point;

                   

                   

                    public class Box extends MovieClip

                    {

                    private var boxSpeed:int;

                    private var target:Point;

                    public function Box()

                    {

                    // constructor code

                    boxSpeed = 10;

                    trace("Made a Box!");

                    //Set target location of the ship

                    target = new Point();

                    target.x = Math.random() * 2500;

                    target.y = Math.random() * 0;

                    // add an event listener  to update every frame

                    //addEventListener(Event.ENTER_FRAME, update);

                    }

                    public function update()

                    {

                    // point the box at the target

                    var dx = target.x - x;

                    var dy = target.y - y;

                    var angle = Math.atan2(dy,dx) / Math.PI * 180;

                    rotation = angle;

                    // move in direction of the box is facing

                    x=x+Math.cos(rotation/180*Math.PI)*boxSpeed;

                    y=y+Math.sin(rotation/180*Math.PI)*boxSpeed;

                    // Calculate the distance to target

                    var hyp = Math.sqrt((dx*dx)+(dy*dy));

                    // if the hyp is less than 5 pixels, pick a new target

                    if (hyp <5)

                    {

                    target.x = Math.random() * 2500;

                    target.y = Math.random() * 0;

                    }

                    }

                    }

                   

                   

                  }

                  • 6. Re: Error using Arrays
                    kglad Adobe Community Professional & MVP

                    there's no loop so all you're going to see is a one-time rotation change and x,y change.

                    1 person found this helpful
                    • 7. Re: Error using Arrays
                      brendenc14282108 Level 1

                      then how would i get a loop? im a beginner in coding and im following a video and just changing the objects to match what i like.

                      • 8. Re: Error using Arrays
                        kglad Adobe Community Professional & MVP

                        you could use:

                         

                         

                        package tankscript.turrent

                        {

                          // import any necessary files/libaries

                          import flash.display.MovieClip;

                          import flash.events.Event;

                          import flash.geom.Point;

                         

                         

                          public class Box extends MovieClip

                          {

                          private var boxSpeed:int;

                          private var target:Point;

                          public function Box()

                          {

                          // constructor code

                          boxSpeed = 10;

                          trace("Made a Box!");

                          //Set target location of the ship

                          target = new Point();

                          target.x = Math.random() * 2500;

                          target.y = Math.random() * 0;

                          // add an event listener  to update every frame

                          //addEventListener(Event.ENTER_FRAME, update);

                          }

                        public function startUpdate():void{

                        addEventListener(Event.ENTER_FRAME, update);

                        }

                        public function endUpdate():void{

                        removeEventListener(Event.ENTER_FRAME, update);

                        }

                          private function update()

                          {

                          // point the box at the target

                          var dx = target.x - x;

                          var dy = target.y - y;

                          var angle = Math.atan2(dy,dx) / Math.PI * 180;

                          rotation = angle;

                          // move in direction of the box is facing

                          x=x+Math.cos(rotation/180*Math.PI)*boxSpeed;

                          y=y+Math.sin(rotation/180*Math.PI)*boxSpeed;

                          // Calculate the distance to target

                        var hyp = (dx*dx)+(dy*dy);

                          // if the hyp is less than 5 pixels, pick a new target

                          if (hyp <25)

                          {

                          target.x = Math.random() * 2500;

                          target.y = Math.random() * 0;

                          }

                          }

                          }

                         

                         

                        }

                        1 person found this helpful
                        • 9. Re: Error using Arrays
                          brendenc14282108 Level 1

                          With the code you gave me i tried it but with "private function update" private it cant find the update with the code of "Box(boxMC[count]).update();" but when i  change the function to public it doesn't give me any errors but my boxMC do not move still.

                          • 10. Re: Error using Arrays
                            Colin Holgate MVP & Adobe Community Professional

                            The orginal code for Box had an enterframe listener, so that it moved itself along. You disabled that and tried to call update() from your other code. But you only call it once, instead of every frame. That's why it doesn't move.

                             

                            kglad's approach was a mixture of how the code originally was, and with the ability to start or stop the animation. update() is only called from within the class, whcih is why it can be private.

                             

                            To set things moving you would call the startUpdate() function, and to stop them moving you call the endUpdate() function. In your other code that would be like this:

                             

                            for (var count=0; count<boxMC.length; count++){

                              boxMC[count].startUpdate();

                              }

                            • 11. Re: Error using Arrays
                              brendenc14282108 Level 1

                              ah okay i see now, but now when i call startUpdate it now says

                              "ArgumentError: Error #1063: Argument count mismatch on tankscript.turrent::Box/update(). Expected 0, got 1."

                              Will the code change since my boxMC in inside of my WorldMC?

                              • 12. Re: Error using Arrays
                                Colin Holgate MVP & Adobe Community Professional

                                That's a mistake in kglad's code. Change this:

                                 

                                  private function update()

                                 

                                to:

                                 

                                  private function update(e:Event)

                                • 13. Re: Error using Arrays
                                  brendenc14282108 Level 1

                                  This code now comes with no errors, but 1 out of my 10 children that spawned do not move now

                                  • 14. Re: Error using Arrays
                                    brendenc14282108 Level 1

                                    Also how does the

                                    "for (var count=0; count<boxMC.length; count++){

                                      boxMC[count].startUpdate();

                                      }"

                                    What is this telling me that its doing? Do i need to change it since my boxMC is inside another object? Because my background is WorldMC and that is where i have my boxMC spawning to.

                                    • 15. Re: Error using Arrays
                                      kglad Adobe Community Professional & MVP

                                      does that mean 9 out of 10 do move?

                                       

                                      and use the trace function to debug your code: http://forums.adobe.com/docs/DOC-2572

                                      • 16. Re: Error using Arrays
                                        brendenc14282108 Level 1

                                        yes sorry 9 out of 10 move

                                        • 17. Re: Error using Arrays
                                          kglad Adobe Community Professional & MVP

                                          use the trace function to determine which fails to have startUpdate() applied.

                                          • 18. Re: Error using Arrays
                                            Colin Holgate MVP & Adobe Community Professional

                                            boxMC is an array, not a display object. Its values are a list of movieclips, which happen to be inside WorldMC, but that doesn't matter. The code is correct.

                                             

                                            Are you sure that your WorldMC is completely empty at the start? If you already had a single example box in there as a test, it won't be in the array.

                                            • 19. Re: Error using Arrays
                                              brendenc14282108 Level 1

                                              oh okay, well i just moved it higher off screen so it will be fine with it not moving then. Thank you

                                              • 20. Re: Error using Arrays
                                                brendenc14282108 Level 1

                                                With the video that im watching he is able to update the Box class using the code of

                                                "for (var count=0; count<boxMC.length; count++){

                                                  boxMC[count].startUpdate();

                                                  }"

                                                 

                                                He also does not have the (e:Event) too. hes able to do it with leaving it such as "private function update()" is there something obvious i'm missing? i seem to do that a lot..

                                                i have tried using public and private function with out the "(e:Event)" it tells me

                                                "ArgumentError: Error #1063: Argument count mismatch on tankscript.turrent::Box/update(). Expected 0, got 1."

                                                 

                                                private function update(e:Event)

                                                  {

                                                  // point the box at the target

                                                  var dx = target.x - x;

                                                  var dy = target.y - y;

                                                  var angle = Math.atan2(dy,dx) / Math.PI * 180;

                                                  rotation = angle;

                                                  // move in direction of the box is facing

                                                  x=x+Math.cos(rotation/180*Math.PI)*boxSpeed;

                                                  y=y+Math.sin(rotation/180*Math.PI)*boxSpeed;

                                                • 21. Re: Error using Arrays
                                                  kglad Adobe Community Professional & MVP

                                                  are you conflating your corrected code from this forum and someone else code?

                                                  • 22. Re: Error using Arrays
                                                    Colin Holgate MVP & Adobe Community Professional

                                                    The startUpdate() function won't need the e:Event part.

                                                     

                                                    Do you have the URL of the video that you're watching? That way you could say to us "take a look at 3 minutes 23 seconds, what does that bit mean?".

                                                    • 24. Re: Error using Arrays
                                                      Colin Holgate MVP & Adobe Community Professional

                                                      If you were to go all the way back to your first post, that's like the video told you to do. But the ships didn't update because you had missed the loop enterframe listener.

                                                       

                                                      If you click this link, and press pause when you get there:

                                                       

                                                      Game Development in AS3 Part 6 - For Loops - YouTube

                                                       

                                                      you can see line 17 sets up the enterframe listener,  and the code you've been trying should be in a function, which he calls 'loop' in his example. Look at line 22 to see where that is set up.

                                                       

                                                      One unusual thing is that he doesn't use public anywhere, he just says function, which I guess defaults to being public. That does add to the confusion.

                                                      • 25. Re: Error using Arrays
                                                        brendenc14282108 Level 1

                                                        okay, i did add the "function loop(e:Event)" but is there anyway to get the new code of kglad of "startUpdate" to work without the "(e:Event)" inside the Box class on this code right below here? As you can see in the video he was able to just do "function update()"

                                                         

                                                        private function update(e:Event)

                                                          {

                                                          // point the box at the target

                                                          var dx = target.x - x;

                                                          var dy = target.y - y;

                                                          var angle = Math.atan2(dy,dx) / Math.PI * 180;

                                                          rotation = angle;

                                                          // move in direction of the box is facing

                                                          x=x+Math.cos(rotation/180*Math.PI)*boxSpeed;

                                                          y=y+Math.sin(rotation/180*Math.PI)*boxSpeed;

                                                        • 26. Re: Error using Arrays
                                                          Colin Holgate MVP & Adobe Community Professional

                                                          kglad's way of working is significantly different to the video. There are too many things that you could get wrong by mixing the two approaches. Could you just get the video version going correctly, then come back to whether there's a different way to do it?

                                                          • 27. Re: Error using Arrays
                                                            brendenc14282108 Level 1

                                                            I'll do my best. Thank you a lot for yours and kglads help on this

                                                            • 28. Re: Error using Arrays
                                                              kglad Adobe Community Professional & MVP

                                                              you're welcome.

                                                              • 29. Re: Error using Arrays
                                                                brendenc14282108 Level 1

                                                                Alrighty everything should be fine now, but NEW ERROR like always...

                                                                Game Development in AS3 Part 7 - Hit tests and bugs galore - YouTube

                                                                its right at the beginning of the video is what im trying to do. but when he got his "MovieClip(parent).bulletMC.push(b);" to work mine gives me an error of.

                                                                 

                                                                TypeError: Error #1010: A term is undefined and has no properties.

                                                                  at tankscript.turrent::Turrent/fire()

                                                                 

                                                                i did move my

                                                                  var bulletMC:Array;

                                                                  bulletMC = new Array();

                                                                to where they are supposed to be. This is the code its not liking:

                                                                 

                                                                private function fire(m:MouseEvent)

                                                                  {

                                                                  trace("Fired!");

                                                                  //if we're allowed to shoot

                                                                  if (shotCooldown<=0)

                                                                  {

                                                                  // reset the cooldown

                                                                  shotCooldown = MAX_COOLDOWN;

                                                                 

                                                                  //spawn a bullet

                                                                  var b = new Bullet();

                                                                  // set the position and roation of the bullet

                                                                  b.rotation = rotation;

                                                                  b.x = x;

                                                                  b.y = y;

                                                                  // add bullets to list of bullets

                                                                  MovieClip(parent).bulletMC.push(b);

                                                                  // add the bullet to the parent

                                                                  parent.addChild(b);

                                                                  // play firing animation

                                                                  play();

                                                                  }

                                                                  }

                                                                • 30. Re: Error using Arrays
                                                                  kglad Adobe Community Professional & MVP

                                                                  again, you should learn the basics of how to debug your own code.

                                                                   

                                                                  1.  How do I debug ActionScript that triggers an error message?

                                                                   

                                                                  http://forums.adobe.com/docs/DOC-2542

                                                                   

                                                                  2.  How do i debug ActionScript that triggers no error message?

                                                                   

                                                                  http://forums.adobe.com/docs/DOC-2572

                                                                   

                                                                  but if you don't have the interest and want to remain dependent, click file>publish settings>swf>and tick 'permit debugging'. retest.

                                                                   

                                                                  what line of code is tiggering the error?

                                                                  • 31. Re: Error using Arrays
                                                                    brendenc14282108 Level 1

                                                                    I should learn sorry.

                                                                    and its

                                                                    MovieClip(parent).bulletMC.push(b);

                                                                     

                                                                    its saying its undefined and has no properties

                                                                    • 32. Re: Error using Arrays
                                                                      kglad Adobe Community Professional & MVP

                                                                      so either parent or bulletMC or b is undefined.

                                                                       

                                                                      you can rule out b being the problem because you can see it's defined a few lines above the problematic line of code.

                                                                       

                                                                      ntl, you can confirm and pinpoint the problem by using the trace() function.  if you read the documents on how to debug you'll see it is, by far, the most important tool used in debugging actionscript.

                                                                       

                                                                      so, just above the problematic line of code use:

                                                                       

                                                                      trace(parent,b,bulletMC);

                                                                       

                                                                      you'll probably find the problem is parent because you're calling fire before the class element is added to the stage.  ie, before it has a parent. to fix that, make sure your addChild() is executed before fire() is called.

                                                                      • 33. Re: Error using Arrays
                                                                        brendenc14282108 Level 1

                                                                        Hey, im back again. i've been messing around with my code and im trying to figure out how to fix the Error of "1119: Access of possibly undefined property bulletMC through a reference with static type flash.display:DisplayObjectContainer."

                                                                        the code is still the

                                                                        "parent.bulletMC.push(b);"

                                                                        I'm trying to access this array through my class of my MainGame code. I'm trying to trace its length from it, but i cant seem to be able to get my bulletMC to push onto the class code of my MainGame file.

                                                                        • 34. Re: Error using Arrays
                                                                          kglad Adobe Community Professional & MVP

                                                                          if bulletMC is defined in the parent movieclip of the code that's referenced in the error message, use

                                                                           

                                                                          MovieClip(parent).bulletMC.push(b)

                                                                          • 35. Re: Error using Arrays
                                                                            brendenc14282108 Level 1

                                                                            I've tried that one too, but then it tells me #1069: Property bulletMC not found on MainGameFile and there is no default value.

                                                                            • 36. Re: Error using Arrays
                                                                              kglad Adobe Community Professional & MVP

                                                                              then bulletMC is not public or its not a property of the parent when your problematic code executes.

                                                                              • 37. Re: Error using Arrays
                                                                                brendenc14282108 Level 1

                                                                                The codes are in two different classes, I want my MainGame Class to be able to start the function of adding the bulletMC to list of arrays. I did try the public one, but that didn't help it still gave the error. trace("Test1") and 2 do not work either, i think this is do bulletMC not being able to be found to trace the length.

                                                                                 

                                                                                Class of turrent that shoots the bulletMC

                                                                                 

                                                                                //spawn a bullet

                                                                                  var b = new Bullet();

                                                                                  // set the position and roation of the bullet

                                                                                  b.rotation = rotation;

                                                                                  b.x = x;

                                                                                  b.y = y;

                                                                                  // add the bullet to the parent

                                                                                  parent.addChild(b);

                                                                                  trace("Fired!");

                                                                                  // add bullets to list of bullets

                                                                                  MovieClip(parent).bulletMC.push(b);

                                                                                  // play firing animation;

                                                                                  play();

                                                                                 

                                                                                Code of MainGame class

                                                                                 

                                                                                // add a new loop to go through all the bullets;

                                                                                  for (var bcount=0; bcount<bulletMC.length; bcount++)

                                                                                  {

                                                                                trace("Test1");

                                                                                  // if the bullets is touching the box

                                                                                  if (boxMC[count].hitTestObject(bulletMC[bcount]))

                                                                                  {

                                                                                  trace("Test2");

                                                                                  }

                                                                                • 38. Re: Error using Arrays
                                                                                  kglad Adobe Community Professional & MVP

                                                                                  is bulletMC public?

                                                                                  • 39. Re: Error using Arrays
                                                                                    brendenc14282108 Level 1

                                                                                    It still sadly doesn't change anything, i completely confused on what going on with it lol

                                                                                    1 2 Previous Next