Skip to main content
deborahb44958436
Inspiring
March 9, 2015
Answered

why am I getting 1120: Access of undefined property ?

  • March 9, 2015
  • 1 reply
  • 1285 views

So I created this simple counter script to create a score board for a game.

import flash.events.MouseEvent;

redButton.addEventListener(MouseEvent.CLICK, randomOutput);

function randomOutput(e: MouseEvent): void {


  var count: Number = 0;

  count = count + 100;

  Counter.text = (count).toString();

}

It worked fine on the stage timeline. but when I transferred this code to ActionScript file I get the 1120 error on both redButton and Counter Instance Names. why? what am I doing wrong?

Here is the full code for the ActionScript file

package {

  import flash.display.MovieClip

  public class myScript1 extends MovieClip {

  public function myScript1() {

  // constructor code

  }

}

}

import flash.events.MouseEvent;

redButton.addEventListener(MouseEvent.CLICK, randomOutput);

function randomOutput(e: MouseEvent): void {

  var count: Number = 0;

  count = count + 100;

  Counter.text = (count).toString();

}

This topic has been closed for replies.
Correct answer moccamaximum

package

{

   

    import flash.display.MovieClip

    import flash.events.MouseEvent;

   

    public class myScript1 extends MovieClip

    {

       

        private var count:Number = 0;

       

        public function myScript1()

        {

       

            // constructor code

            //makes ure that there is a Button/MovieClip named "redButton" on stage in the first frame of the timeline

            redButton.addEventListener(MouseEvent.CLICK, randomOutput);

       

        }

       

       

       

        private function randomOutput(e:MouseEvent):void

        {

           

           

            count += 100;

            //make sure that there is a TextField named "Counter" on stage in the first frame of the timeline

            Counter.text = count;

       

        }

   

    }

}

1 reply

Inspiring
March 9, 2015

You have imports inside the class definition, those go outside the class, inside the package namespace like the first MovieClip one. Also, you cannot have code like redButton.addEventListener outside of a method - put that inside the constructor. Also, you are never going to have more than 100 points showing, since you set it to 0 each time you call randomOutput... which is not at all random.

deborahb44958436
Inspiring
March 9, 2015

Thanks dmennenoh. but can you give me a sample code to correct my script? since I'm a newbee  I'd take me forever to fully understand and fix it.

deborahb44958436
Inspiring
March 11, 2015

once you create a variable/function in your class it becomes available across your whole movie.

Caveat: This is not true for DisplayObjects (Objects that are part of the DisplayList like TextFields, MovieClips, Sprites etc.,) DisplayObjects are (in general)  only available in the Scene/Frame where they are actually on stage

DisplayObjects that belong to different Scenes can therefore be named identically without flash throwing an error.

So you can call updateScore() from anywhere inside your movie, it will always use the same variable to increment your score and use a DisplayObject that is named "scorecounter" that is currently available on stage to display the score.

There is a very good series, that explains these kind of fundamentals for Beginners:

ActionScript 3 fundamentals: Functions | Adobe Developer Connection

PS:

It is strongly advised to avoid any kind of timeline-based programming (timeline-code/scenes etc.), since debugging that kind of code becomes quickly a nightmare, even in small projects.

Scenes are basically a relic of Actionscript1/2 that have no place in a Language that wants to be object-oriented. (AS3)


moccamaximum, Thank you for your patient and help, I do appreciate it.   I understand about your remark regarding timeline-based programming, that is why I use AS file, but it does't work in Scene 2, it's as if  Scene 2 is waiting for function or something to activate  the script.
I cannot let go working with Scenes, my app will have a lot of movement and I can't have a timeline that goes forever with endless layers on one scene, this is a nightmare scenario as well.

I think that I need to put a short update function script on frame one in scene2, but I haven't figured it out yet. BTW I am using the same text object with the same  instance name. I'm a little frustrated right now