Copy link to clipboard
Copied
I addChild'ed a TextField by using score:
TextField = new TextField
I am trying to change its text, by using :
score.text = "Score: "
But I get this error:
1119: Access of possibly undefined property text through a reference with static type TextField. |
Can someone tell me how to change it's text?
Copy link to clipboard
Copied
you should be using:
var score:TextField=new TextField();
score.text="Score: ";
Copy link to clipboard
Copied
That is exactly what I was using, and it still has the same error. Here is my Game.as where this occurs:
package
{
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flashx.textLayout.formats.BackgroundColor;
import fl.controls.Label;
import fl.controls.TextInput;
import flash.display.DisplayObjectContainer;
import GlobalVar;
import flash.text.TextField;
public class Game extends MovieClip
{
//Declaring timer names as new timers, (5) / (10) is interval, 1000 = 1 second
public var mainMenuTimer:Timer = new Timer(5);
public var menuButtonTimer:Timer = new Timer(5);
public var pauseTimer:Timer = new Timer(5);
public var myTimer:Timer = new Timer(5);
public var fadeTimer:Timer = new Timer(10);
//Declaring items that will be spawned, the capital letter ones are the Items, lowercase one is name of object once it has been spawned.
public var menublock:MenuBlock = new MenuBlock();
public var square:Square = new Square();
public var square2:Square2 = new Square2();
public var pausebutton:PauseButton = new PauseButton();
public var playbutton:PlayButton = new PlayButton();
public var background:Background = new Background();
public var menubutton:MenuButton = new MenuButton();
public var score:TextField=new TextField();
public function Game()
{
addChild( background );
addChild( square2 );
addChild( square );
addChild( score );
addChild( menublock );
addChild( playbutton );
addChild( pausebutton);
addChild( menubutton );
score.text="Score: ";
mainMenuTimer.addEventListener(TimerEvent.TIMER, timerListener_5);
menuButtonTimer.addEventListener(TimerEvent.TIMER, timerListener_4);
menuButtonTimer.start();
pauseTimer.addEventListener(TimerEvent.TIMER, timerListener_3); //listens for timers.
pauseTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
myTimer.start();
fadeTimer.addEventListener(TimerEvent.TIMER, timerListener_2);
fadeTimer.start();
square.addEventListener(MouseEvent.CLICK, fl_click);
pausebutton.addEventListener(MouseEvent.CLICK, fl_click_2);
playbutton.addEventListener(MouseEvent.CLICK, fl_click_3);
menubutton.addEventListener(MouseEvent.CLICK, fl_click_4);
}
private function timerListener(e:TimerEvent):void
{
square.edgeCorrect();
}
private function timerListener_2(e:TimerEvent):void
{
if (GlobalVar.vars.shouldFade == "false")
{
fadeTimer.stop();
}
square2.fadeOut(); //Excecutes 'public function fadeOut()' which is in the code of Square2.as
}
private function timerListener_3(e:TimerEvent):void
{
if (GlobalVar.vars.playing == false) { playbutton.checkPlaying();square.paused(); square2.paused(); fadeTimer.stop() }
if (GlobalVar.vars.playing == false && GlobalVar.vars.menu == true) {menublock.checkPlaying() }
if (GlobalVar.vars.playing == true && square2.alpha == 15 && square.alpha == 15) {square.alpha = 100; square2.alpha = 100 }
if (GlobalVar.vars.playing == true) {pausebutton.checkPlaying() }
}
private function timerListener_4(e:TimerEvent):void
{
if (GlobalVar.vars.showMenuButton == true) {menubutton.visible = true}
if (GlobalVar.vars.showMenuButton == false) {menubutton.visible = false}
}
private function timerListener_5(e:TimerEvent):void //listens to see if we are at MAIN menu, if so, it removes unnecessary objects.
{
if (GlobalVar.vars.mainMenu == true) {GlobalVar.vars.menu = false; GlobalVar.vars.playing = false; square.visible = false; square2.visible = false; score.visible = false; menubutton.visible = false;}
}
private function fl_click(event:MouseEvent):void //Square click
{
trace("square clicked")
square.changeParameters();
GlobalVar.vars.shouldFade = "true";
fadeTimer.start();
}
private function fl_click_2(event:MouseEvent):void //Pause button click
{
pausebutton.click();
GlobalVar.vars.mainMenu = false
}
private function fl_click_3(event:MouseEvent):void //Play button click
{
mainMenuTimer.stop()
if (GlobalVar.vars.mainMenu == true) {square2.visible = true ; square.visible = true; pausebutton.visible = true; score.visible = true; menubutton.visible = true;square.changeParameters();square2.changeParameters();square2.x = GlobalVar.vars.x; square2.y = GlobalVar.vars.y;square2.height = GlobalVar.vars.height; square2.width = square2.height;}
GlobalVar.vars.mainMenu = false
GlobalVar.vars.playing = true;
GlobalVar.vars.menu = false;
playbutton.visible = false;
menublock.visible = false;
playbutton.click();
}
private function fl_click_4(event:MouseEvent):void //Menu button click
{
mainMenuTimer.start();
menubutton.click();
}
}
}
I changed the text on the line after I added the children.
Copy link to clipboard
Copied
hi you can try this?
package
{
import flash.display.MovieClip;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flashx.textLayout.formats.BackgroundColor;
import fl.controls.Label;
import fl.controls.TextInput;
import flash.display.DisplayObjectContainer;
import GlobalVar;
import flash.text.TextField;
/**
* ...
* @author Faraday
*/
public class Game extends MovieClip
{
//Declaring timer names as new timers, (5) / (10) is interval, 1000 = 1 second
public var mainMenuTimer:Timer = new Timer(5);
public var menuButtonTimer:Timer = new Timer(5);
public var pauseTimer:Timer = new Timer(5);
public var myTimer:Timer = new Timer(5);
public var fadeTimer:Timer = new Timer(10);
//Declaring items that will be spawned, the capital letter ones are the Items, lowercase one is name of object once it has been spawned.
public var menublock:MenuBlock = new MenuBlock();
public var square:Square = new Square();
public var square2:Square2 = new Square2();
public var pausebutton:PauseButton = new PauseButton();
public var playbutton:PlayButton = new PlayButton();
public var background:Background = new Background();
public var menubutton:MenuButton = new MenuButton();
public var score:TextField=new TextField();
public function Game():void
{
if (stage != null) {
init();
}else {
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
private function init():void
{
addChild( background );
addChild( square2 );
addChild( square );
addChild( score );
addChild( menublock );
addChild( playbutton );
addChild( pausebutton);
addChild( menubutton );
score.text=String("Score: ");
mainMenuTimer.addEventListener(TimerEvent.TIMER, timerListener_5);
menuButtonTimer.addEventListener(TimerEvent.TIMER, timerListener_4);
menuButtonTimer.start();
pauseTimer.addEventListener(TimerEvent.TIMER, timerListener_3); //listens for timers.
pauseTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
myTimer.start();
fadeTimer.addEventListener(TimerEvent.TIMER, timerListener_2);
fadeTimer.start();
square.addEventListener(MouseEvent.CLICK, fl_click);
pausebutton.addEventListener(MouseEvent.CLICK, fl_click_2);
playbutton.addEventListener(MouseEvent.CLICK, fl_click_3);
menubutton.addEventListener(MouseEvent.CLICK, fl_click_4);
}
private function timerListener(e:TimerEvent):void
{
square.edgeCorrect();
}
private function timerListener_2(e:TimerEvent):void
{
if (GlobalVar.vars.shouldFade == "false")
{
fadeTimer.stop();
}
square2.fadeOut(); //Excecutes 'public function fadeOut()' which is in the code of Square2.as
}
private function timerListener_3(e:TimerEvent):void
{
if (GlobalVar.vars.playing == false) { playbutton.checkPlaying();square.paused(); square2.paused(); fadeTimer.stop() }
if (GlobalVar.vars.playing == false && GlobalVar.vars.menu == true) {menublock.checkPlaying() }
if (GlobalVar.vars.playing == true && square2.alpha == 15 && square.alpha == 15) {square.alpha = 100; square2.alpha = 100 }
if (GlobalVar.vars.playing == true) {pausebutton.checkPlaying() }
}
private function timerListener_4(e:TimerEvent):void
{
if (GlobalVar.vars.showMenuButton == true) {menubutton.visible = true}
if (GlobalVar.vars.showMenuButton == false) {menubutton.visible = false}
}
private function timerListener_5(e:TimerEvent):void //listens to see if we are at MAIN menu, if so, it removes unnecessary objects.
{
if (GlobalVar.vars.mainMenu == true) {GlobalVar.vars.menu = false; GlobalVar.vars.playing = false; square.visible = false; square2.visible = false; score.visible = false; menubutton.visible = false;}
}
private function fl_click(event:MouseEvent):void //Square click
{
trace("square clicked")
square.changeParameters();
GlobalVar.vars.shouldFade = "true";
fadeTimer.start();
}
private function fl_click_2(event:MouseEvent):void //Pause button click
{
pausebutton.click();
GlobalVar.vars.mainMenu = false
}
private function fl_click_3(event:MouseEvent):void //Play button click
{
mainMenuTimer.stop()
if (GlobalVar.vars.mainMenu == true) {square2.visible = true ; square.visible = true; pausebutton.visible = true; score.visible = true; menubutton.visible = true;square.changeParameters();square2.changeParameters();square2.x = GlobalVar.vars.x; square2.y = GlobalVar.vars.y;square2.height = GlobalVar.vars.height; square2.width = square2.height;}
GlobalVar.vars.mainMenu = false
GlobalVar.vars.playing = true;
GlobalVar.vars.menu = false;
playbutton.visible = false;
menublock.visible = false;
playbutton.click();
}
private function fl_click_4(event:MouseEvent):void //Menu button click
{
mainMenuTimer.start();
menubutton.click();
}
}
}
}
}
Copy link to clipboard
Copied
click file>publish settings>swf and tick 'permit debugging'. retest.
copy and paste the complete error message and highlight the line of code mentioned at the top of the error message.