Skip to main content
Participant
August 6, 2015
Answered

Error 1120: undefined int - simple code

  • August 6, 2015
  • 1 reply
  • 700 views

hello!

I am creating a game, and I want to have it so that when a player clicks on a certain button, a particular stat increases (by whatever amount, depending on the button) and it will display that number in a dynamic text box (instance name: stat_text)

Each time I try to do this, though, the stat comes up as being undefined. I tried to fix this but it gives me the same error.

I'm also putting this code right on the timeline

first I just had:


stop();

import flash.events.MouseEvent;

stat_btn.addEventListener(MouseEvent.CLICK, stat_btnClick);

var stat:int=0;

stat_text.text="stat";

function test_btnClick(event:MouseEvent):void {

  stat += 1;

}

but it brings up the error:

1120: Access of undefined property stat_text.text="stat";

next i tried to create a constructor for it, telling it that when we enter this frame, the buttonPress event will start and if the button is pressed that stat will be +1, but it still says that stat is undefined.

I'm now guessing that I have to do something like stat = new stat(); but i don't really know why I'd be doing that or what it would be doing. Do I have to create a class for the stats?

(Side note: in some AS3 game tutorials, the codes just call on functions but don't even state them in the beginning -unless it's somewhere on the actual timeline. I feel like I'm missing something...)

Thank you!! I need help

This topic has been closed for replies.
Correct answer Paladin Jog

Hi! Orion! I found some mistake in your code:

1. stat_text.text="stat";

stat is a variable. If you put it into "", it will become a string.Yes, I see that stat_text.text must be a instance of String, but if you want to convert the stat to a String, you should use toString(), not put it into "".

It should be: stat_text.text=stat.toString();

2. stat_text.text=stat.toString(); must be move to bellow the stat += 1;

If not. the statement will be executed once only.

3. the name of function is inconsistency.

In stat_btn.addEventListener(MouseEvent.CLICK, stat_btnClick); you use stat_btnClick, but you use test_btnClick in the function. It should be stat_btnClick.

4. 1120: Access of undefined property stat_text.text="stat";

Are you sure that you put a dynamic text box (instance name: stat_text) in the stage in current frame?

The right code is bellow:

import flash.events.MouseEvent;

stop();

var stat:int = 0;

stat_btn.addEventListener(MouseEvent.CLICK, stat_btnClick);

function stat_btnClick(event: MouseEvent): void {

  stat += 1;

  stat_text.text = stat.toString();

}

btw, I think that it is better learn AS3 from base tutorials than from a game tutorials. Maybe you learn the <Adobe Flash Platform * Learning ActionScript 3.0‌> and <Adobe Flash Platform * ActionScript 3.0 Developer’s Guide> firstly.

1 reply

Paladin Jog
Paladin JogCorrect answer
Participating Frequently
August 6, 2015

Hi! Orion! I found some mistake in your code:

1. stat_text.text="stat";

stat is a variable. If you put it into "", it will become a string.Yes, I see that stat_text.text must be a instance of String, but if you want to convert the stat to a String, you should use toString(), not put it into "".

It should be: stat_text.text=stat.toString();

2. stat_text.text=stat.toString(); must be move to bellow the stat += 1;

If not. the statement will be executed once only.

3. the name of function is inconsistency.

In stat_btn.addEventListener(MouseEvent.CLICK, stat_btnClick); you use stat_btnClick, but you use test_btnClick in the function. It should be stat_btnClick.

4. 1120: Access of undefined property stat_text.text="stat";

Are you sure that you put a dynamic text box (instance name: stat_text) in the stage in current frame?

The right code is bellow:

import flash.events.MouseEvent;

stop();

var stat:int = 0;

stat_btn.addEventListener(MouseEvent.CLICK, stat_btnClick);

function stat_btnClick(event: MouseEvent): void {

  stat += 1;

  stat_text.text = stat.toString();

}

btw, I think that it is better learn AS3 from base tutorials than from a game tutorials. Maybe you learn the <Adobe Flash Platform * Learning ActionScript 3.0‌> and <Adobe Flash Platform * ActionScript 3.0 Developer’s Guide> firstly.

Orion-11Author
Participant
August 6, 2015

thank you so much for your reply!! yeah, sorry that was my bad. in my actual code, the button is test rather than stat, but i just changed the names to make it easier

unfortunately, now I have 2 errors:

1120: Access of undefined property stat_text.

1120: Access of undefined property stat_text.

robdillon
Participating Frequently
August 6, 2015

That problem is most likely because the textfield "stat_text" doesn't exist in the frame where the code is. Since Actionscript can't find an object with that instance name, it thinks that it is an undeclared variable.