Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

why am I getting 1120: Access of undefined property ?

Participant ,
Mar 09, 2015 Mar 09, 2015

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();

}

TOPICS
ActionScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Mar 10, 2015 Mar 10, 2015

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);

       

        }

       

       

       

...
Translate
Enthusiast ,
Mar 09, 2015 Mar 09, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 09, 2015 Mar 09, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Mar 10, 2015 Mar 10, 2015

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;

       

        }

   

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 10, 2015 Mar 10, 2015

Bingo, Thank you moccamaximum it worked. Now I need and advice. I need this counter to work throughout the game. This game will have many scenes and obviously many buttons. also I want to create a reduction in points when clicking on the wrong buttons. Any advice in coding and otherwise in regard on how to keep the score throughout the game and how to code a reductions?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Mar 10, 2015 Mar 10, 2015

write a function that both keeps track of your counter and refresh sth. of the UI that shows it to your player.

private function updateCounter(_bonus:int):void{

  count += _bonus;

Counter.text = count;

}

now whenver sth happens in your game that changes the count call the function with an argument

//add 100

updateCounter(100);

//subtract 50

updateCounter(-50);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 10, 2015 Mar 10, 2015

ok, so I changed my code and it appears to work fine for both the counter and working inside the actionscript file. I still didn't figured out how to roll over the score results to the next scene. the score's text field have the same instance name it was actually copied and paste from scene 1. on scene 2 and up, how do I tell it to read the variable containing the score? here is the complete script from the AS file. I changed the names of the variable and the instance names. so here it is:

package {

  import flash.display.MovieClip

  import flash.events.MouseEvent;

  public class scoreMe2 extends MovieClip {

  public function scoreMe2() {

  //Scoreboard starts here:

  var score: uint;

  function init(): void {

  score = 100;

  scorecounter.text = "SCORE:" + score.toString();

  clip.buttonMode = true;

  clip.addEventListener(MouseEvent.CLICK, on_press1);

  gButton.buttonMode = true;

  gButton.addEventListener(MouseEvent.CLICK, on_press2);

  }

  //Positive button adding points

  function on_press1(event: MouseEvent): void {

  updateScore();

  }

  function updateScore(): void {

  score += 100;

  scorecounter.text = "SCORE:" + score.toString();

  }

  //Negative button subtracting points

  function on_press2(event: MouseEvent): void {

  score += -20;

  scorecounter.text = "SCORE:" + score.toString();

  }

  init();

  //Scoreboard ends here:

}

  }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Mar 10, 2015 Mar 10, 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)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 11, 2015 Mar 11, 2015
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines