Not all loaders work in Flash AS3 - actionscript-3

Hi ive been working on some project that needs to have a login system. I got it linked now with the database the problem is the other Loaders doesnt work in my system please help! Thanks in advance :)
When I click Login and Register it works fine and goes to the designated link of a diff .swf file:
but after logging in, these server buttons doesnt work on the .swf they were linked
This is my code :
package
{
/*
always extend a class using movieclip instead of sprite when using flash.
*/
import flash.display.MovieClip;
import flash.events.*;
import flash.net.*;
import flash.text.*;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.ProgressEvent;
public class main extends MovieClip {;
var loadit = new Loader();
public function main():void
{
/*
buttonMode gives the submit button a rollover
*/
submit_button.buttonMode = true;
register_button.buttonMode = true;
quit_button.buttonMode = true;
server1_button.buttonMode = true;
server2_button.buttonMode = true;
/*
what this says is that when our button is pressed, the checkLogin function will run
*/
submit_button.addEventListener(MouseEvent.MOUSE_DOWN, checkLogin);
quit_button.addEventListener(MouseEvent.MOUSE_DOWN, gotoLogin);
register_button.addEventListener(MouseEvent.MOUSE_DOWN, gotoRegister);
server1_button.addEventListener(MouseEvent.MOUSE_DOWN, gotoLobby);
server2_button.addEventListener(MouseEvent.MOUSE_DOWN, gotoLobby);
/*
set the initial textfield values
*/
username.text = "";
password.text = "";
}
function gotoLobby(event:MouseEvent)
{
trace("you clicked me");
loadit = new Loader();// here it is instantiated
addChild(loadit);
loadit.load(new URLRequest("lobby.swf"));
}
function gotoLogin(event:MouseEvent)
{
trace("you clicked me");
loadit = new Loader();// here it is instantiated
addChild(loadit);
loadit.load(new URLRequest("login.swf"));
}
function gotoRegister(event:MouseEvent)
{
trace("you clicked me");
loadit = new Loader();// here it is instantiated
addChild(loadit);
loadit.load(new URLRequest("register.swf"));
}
/*
function to process our login
*/
public function processLogin():void
{
/*
variables that we send to the php file
*/
var phpVars:URLVariables = new URLVariables();
/*
we create a URLRequest variable. This gets the php file path.
*/
var phpFileRequest:URLRequest = new URLRequest("http://localhost/Perdigana/controlpanel.php");
/*
this allows us to use the post function in php
*/
phpFileRequest.method = URLRequestMethod.POST;
/*
attach the php variables to the URLRequest
*/
phpFileRequest.data = phpVars;
/*
create a new loader to load and send our urlrequest
*/
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
phpLoader.addEventListener(Event.COMPLETE, showResult);
/*
now lets create the variables to send to the php file
*/
phpVars.systemCall = "checkLogin";
phpVars.username = username.text;
phpVars.password = password.text;
/*
this will start the communication between flash and php
*/
phpLoader.load(phpFileRequest);
}
/*
function to show the result of the login
*/
public function showResult(event:Event):void
{
/*
this autosizes the text field
***** You will need to import flash's text classes. You can do this by adding:
import flash.text.*;
...to your list of import statements
*/
status.autoSize = TextFieldAutoSize.LEFT;
/*
this gets the output and displays it in the result text field
*/
status.text = "" + event.target.data.systemResult;
if (event.target.data.systemResult != "Incorrect username or password. Please try again!")
{
status.visible = false;
username.visible = false;
password.visible = false;
username_lbl.visible = false;
password_lbl.visible = false;
password_lbl.visible = false;
password_lbl.visible = false;
register_button.visible = false;
submit_button.visible = false;
server1_button.visible = true;
server2_button.visible = true;
server3_button.visible = true;
quit_button.visible = true;
}
}
/*
the function to checkLogin
*/
public function checkLogin(e:MouseEvent):void
{
if (username.text == "" || password.text == "")
{
/*
if username or password fields are empty set error messages
*/
if (username.text == "")
{
status.text = "Please enter your username";
status.visible = true;
}
else if (password.text == "")
{
status.text = "Please enter your password";
status.visible = true;
}
}
else
{
processLogin();
}
}
}
}
when I click the servers theyre getting the correct output as "you clicked me" but stays on that swf please help!

Related

Actionscript 3 Code Keeps Giving Error 1009

I managed to apply this code, which fetches a text file and places it into a dynamic text box. Here's the code:
// reference code from permadi
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.utils.ByteArray;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.display.MovieClip;
import fl.controls.ProgressBarMode;
import fl.controls.TextArea;
var mFileReference:FileReference;
stop();
// Setup button to handle browsing
browseButton.buttonMode = true;
browseButton.mouseChildren = false;
browseButton.addEventListener(MouseEvent.CLICK, onBrowseButtonClicked);
// Hide progress bar;
progressBar.visible = false;
// This function is called when the BROWSE button is clicked.
function onBrowseButtonClicked(event:MouseEvent):void
{
trace("onBrowse");
mFileReference=new FileReference();
mFileReference.addEventListener(Event.SELECT, onFileSelected);
var swfTypeFilter:FileFilter = new FileFilter("Text Files","*.txt; *.html;*.htm;*.php");
var allTypeFilter:FileFilter = new FileFilter("All Files (*.*)","*.*");
mFileReference.browse([swfTypeFilter, allTypeFilter]);
}
// This function is called after user selected a file in the file browser dialog.;
function onFileSelected(event:Event):void
{
trace("onFileSelected");
// This callback will be called when the file is uploaded and ready to use
mFileReference.addEventListener(Event.COMPLETE, onFileLoaded);
// This callback will be called if there's error during uploading;
mFileReference.addEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
// Optional callback to track progress of uploading;
mFileReference.addEventListener(ProgressEvent.PROGRESS, onProgress);
// Tells the FileReference to load the file;
mFileReference.load();
// Show progress bar;
progressBar.visible = true;
progressBar.mode = ProgressBarMode.MANUAL;
progressBar.minimum = 0;
progressBar.maximum = 100;
browseButton.visible = false;
}
// This function is called to notify us of the uploading progress
function onProgress(event:ProgressEvent):void
{
var percentLoaded:Number = event.bytesLoaded / event.bytesTotal * 100;
trace("loaded: "+percentLoaded+"%");
progressBar.setProgress(percentLoaded, 100);
}
// This function is called after the file has been uploaded.;
function onFileLoaded(event:Event):void
{
var fileReference:FileReference = event.target as FileReference;
// These steps below are to pass the data as DisplayObject
// These steps below are specific to this example.
var data:ByteArray = fileReference["data"];
codeText.text = data.toString();
browseButton.visible = true;
progressBar.visible = false;
mFileReference.removeEventListener(Event.COMPLETE, onFileLoaded);
mFileReference.removeEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
mFileReference.removeEventListener(ProgressEvent.PROGRESS, onProgress);
}
function onFileLoadError(event:Event):void
{
// Hide progress bar
progressBar.visible = false;
browseButton.visible = true;
mFileReference.removeEventListener(Event.COMPLETE, onFileLoaded);
mFileReference.removeEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
mFileReference.removeEventListener(ProgressEvent.PROGRESS, onProgress);
trace("File load error");
}
It works fine, I added the loaded file to a string, but now I want Flash to read the string and send it to the related frames corresponding to it. Here's the code:
stage.addEventListener(Event.ENTER_FRAME, updateFunction);
function updateFunction(e:Event)
{
if (codeText.text == "Preference 01")
{
gotoAndStop(50);
}
else if (codeText.text == "Preference 02")
{
gotoAndStop(51);
}
else if (codeText.text == "Preference 03")
{
gotoAndStop(52);
}
else if (codeText.text == "Preference 04")
{
gotoAndStop(53);
}
else
{
gotoAndStop(54);
}
}
I Keep Getting Error 1009 About A Null Object, I Tried For Hours But I Think I Need Some Help.
If all you want is to go to a different frame based on what you loaded, simply remove your updateFunction and replace your onFileLoaded() function with the following:
function onFileLoaded(event:Event):void {
var fileReference:FileReference = event.target as FileReference;
// These steps below are to pass the data as DisplayObject
// These steps below are specific to this example.
var data:ByteArray = fileReference["data"];
browseButton.visible = true;
progressBar.visible = false;
mFileReference.removeEventListener(Event.COMPLETE, onFileLoaded);
mFileReference.removeEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
mFileReference.removeEventListener(ProgressEvent.PROGRESS, onProgress);
codeText.text = data.toString();
switch (codeText.text) {
case "Preference 01":
gotoAndStop(50);
break;
case "Preference 02":
gotoAndStop(51);
break;
case "Preference 03":
gotoAndStop(52);
break;
case "Preference 04":
gotoAndStop(53);
break;
default:
gotoAndStop(54);
}
}
Please be sure to enable Debug mode and compile in that mode so you can get specifics like line numbers on your errors. Also, to avoid BotMaster's complaints in the future, be sure to read https://stackoverflow.com/help/mcve

TypeError: Error #1009 in AS3 flash cs6

I have got this error while working on my flash:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Options()
This is my Options class:
package {
import flash.display.MovieClip;
import fl.managers.StyleManager;
import flash.text.TextFormat;
import fl.events.ComponentEvent;
import fl.events.*;
import flash.events.MouseEvent;
import fl.controls.*;
import flash.net.SharedObject;
import flash.events.Event;
public class Options extends MovieClip
{
//DECLARE CLASS VARIABLES//
//DECLARE COMPONENT VARIABLES
private var cComponentFmt:TextFormat;
//DECLARE SAVE DATA VARIABLES
private var cPlayerData:Object;
private var cSavedGameData:SharedObject;
//DECLARE SOUND VARIABLES
public function Options()
{
//created the SharedObject using the getLocal() function
cSavedGameData = SharedObject.getLocal("savedPlayerData");
//set component formats
setComponents();
//initialize player's data
setPlayerData();
//set default message display upon entering the setup page
msgDisplay.text = "Introduce yourself to the fellow minions!";
//add event listeners
nameBox.addEventListener(MouseEvent.CLICK, clearName);
nameBox.addEventListener(ComponentEvent.ENTER, setName);
}
private function setComponents():void
{
//set the TextFormat for the components
cComponentFmt = new TextFormat();
cComponentFmt.color = 0x000000; //black colour
cComponentFmt.font = "Comic Sans MS"; //set default "Comic Sans MS"
cComponentFmt.size = 16;
//apply the new TextFormat to ALL the components
StyleManager.setStyle("textFormat", cComponentFmt);
}
private function setName(evt:ComponentEvent):void
{
trace("Processing text input box..");
// player pressed the ENTER key
cPlayerData.pName = nameBox.text;
msgDisplay.text = "Welcome, " + cPlayerData.pName + "! \nEnjoy many hours of fun with us!";
saveData();
}
private function clearName(evt:MouseEvent):void
{
// player CLICKED in the nameBox
nameBox.text = "";
}
private function setPlayerData():void
{
//all variables that relate to the player
cPlayerData = new Object();
//options related variables
cPlayerData.pName = "Papoi";
cPlayerData.sndTrack = "none";
//game related variables
cPlayerData.pScore = 0;
//save the player's data
saveData();
}
private function saveData():void
{
//savedPlayerData = cPlayerData is the name=value pair
cSavedGameData.data.savedPlayerData = cPlayerData;
//force Flash to update the data
cSavedGameData.flush();
//reload the newly saved data
loadData();
}
private function loadData():void
{
//gets the data stored in the SharedObject
//this particular line not found in the options.as
cSavedGameData = SharedObject.getLocal("savedPlayerData","/",false);
//now stores the save data in the player object
cPlayerData = cSavedGameData.data.savedPlayerData;
}
}
}
Does anyone know why this particular error exists?
And also, I want to make the cPlayerData.pName to be "Papoi" if a name is not entered in the nameBox. How am I to make that happen? Cuz right now, I tried by setting the cPlayerData.pName to "Papoi" by default, but it doesn't work. Hmm..
Your problem is in the constructor function so maybe the component “msgDisplay” and/or the component “nameBox” are/is not initialized completely yet while you are trying to access one of its properties...
A good practice is to access your objects only when they are fully initialized, that can be done using the event “AddedToSatge” which will not be fired before all children’s are initialized..
Note: even if it is not the source of your problem, it is a good thing to do always because it will save you from other problems and bugs related to the same issue.
UPDATE:
The problem was in your loadData() function, because you have changed the localPath of your SharedObject inside that function body (it is not the same as used in saveData() function), then your loaded data will always be null, and that was what you see in the error message. you just need to delete that line from loadData function. see my updated code below.
package
{
import flash.display.MovieClip;
import fl.managers.StyleManager;
import flash.text.TextFormat;
import fl.events.ComponentEvent;
import fl.events.*;
import flash.events.MouseEvent;
import fl.controls.*;
import flash.net.SharedObject;
import flash.events.Event;
public class Options extends MovieClip
{
//DECLARE CLASS VARIABLES//
//DECLARE COMPONENT VARIABLES
private var cComponentFmt:TextFormat;
//DECLARE SAVE DATA VARIABLES
private var cPlayerData:Object;
private var cSavedGameData:SharedObject;
//DECLARE SOUND VARIABLES
public function Options()
{
if (stage)
{
init();
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
public function init(e:Event = null):void
{
// it is important to remove it coz you don't need it anymore:
removeEventListener(Event.ADDED_TO_STAGE, init);
//created the SharedObject using the getLocal() function
cSavedGameData = SharedObject.getLocal("savedPlayerData");
//set component formats
setComponents();
//initialize player's data
setPlayerData();
//set default message display upon entering the setup page
msgDisplay.text = "Introduce yourself to the fellow minions!";
//add event listeners
nameBox.addEventListener(MouseEvent.CLICK, clearName);
nameBox.addEventListener(ComponentEvent.ENTER, setName);
}
private function setComponents():void
{
//set the TextFormat for the components
cComponentFmt = new TextFormat();
cComponentFmt.color = 0x000000;//black colour
cComponentFmt.font = "Comic Sans MS";//set default "Comic Sans MS"
cComponentFmt.size = 16;
//apply the new TextFormat to ALL the components
StyleManager.setStyle("textFormat", cComponentFmt);
}
private function setName(evt:ComponentEvent):void
{
trace("Processing text input box..");
// player pressed the ENTER key
// remove the whitespace from the beginning and end of the name:
var playerNameWithoutSpaces:String = trimWhitespace(nameBox.text);
// check if the user did not enter his name then default name is "Papoi":
if (playerNameWithoutSpaces == "")
{
cPlayerData.pName = "Papoi";
}
else
{
cPlayerData.pName = nameBox.text;
}
//// This will replace the default message :
//// msgDisplay.text = "Welcome, " + cPlayerData.pName + "! \nEnjoy many hours of fun with us!";
// This will add the welcome message to the default message :
msgDisplay.text += "\nWelcome, " + cPlayerData.pName + "! \nEnjoy many hours of fun with us!";
saveData();
}
private function clearName(evt:MouseEvent):void
{
// player CLICKED in the nameBox
nameBox.text = "";
}
private function setPlayerData():void
{
//all variables that relate to the player
cPlayerData = new Object();
//options related variables
cPlayerData.pName = "Papoi";
cPlayerData.sndTrack = "none";
//game related variables
cPlayerData.pScore = 0;
//save the player's data
saveData();
}
private function saveData():void
{
//savedPlayerData = cPlayerData is the name=value pair
cSavedGameData.data.savedPlayerData = cPlayerData;
//force Flash to update the data
cSavedGameData.flush();
//reload the newly saved data;
loadData();
}
private function loadData():void
{
//gets the data stored in the SharedObject
//this particular line not found in the options.as
//// delete the next line, no need to set it every time :
//// cSavedGameData = SharedObject.getLocal("savedPlayerData","/",false);
//now stores the save data in the player object
cPlayerData = cSavedGameData.data.savedPlayerData;
}
//────────────────────────────────────────────
private function trimWhitespace($string:String):String
{
if ($string == null)
{
return "";
}
return $string.replace(/^\s+|\s+$/g, "");
}
//────────────────────────────────────────────
}
}

FLASH AS3 - Dynamicly Loaded Slideshow From Txt File

I have done a simple code for a slideshow that dynamicly loads images from a subfolder, depending on what is listet in a text file.
the code:
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Loader;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.Bitmap;
import flash.display.BitmapData;
public class slideshow extends MovieClip {
public var listLoader:URLLoader;
public var newImgList:Array;
public var imgX:int = 0;
public var container:MovieClip;
public function slideshow() {
container = new MovieClip();
stage.addChild(container);
listLoader = new URLLoader(new URLRequest("./slideshow.txt"));
listLoader.addEventListener(Event.COMPLETE, initImages);
}
public function initImages(event:Event):void {
var imgList:Array = listLoader.data.replace(/^\s+/,"").replace(/\s+$/,"").split(/\s+/);
newImgList = new Array();
for(var line:int = 0; line < imgList.length; line++ ) {
if(imgList[line].indexOf(".png") != -1 || imgList[line].indexOf(".jpg") != -1) {
newImgList.push(imgList[line]);
}
}
loadImage();
}
public function loadImage():void {
for(var loaderNum = 0; loaderNum < newImgList.length; loaderNum++) {
var imgLoader = new Loader();
imgLoader.load(new URLRequest("./slideshow/" + newImgList[loaderNum]));
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgInit);
}
}
public function imgInit(event:Event):void {
var imgBmp:BitmapData = event.target.content.bitmapData;
var img:Bitmap = new Bitmap(imgBmp);
img.height = 150;
img.scaleX = img.scaleY;
img.y = 0;
img.x = imgX;
imgX = (imgX + img.width + 10);
container.addChild(img);
}
}
well actually it works fine for me except for that the images are displayed in a almost random order.
i guess the code is loading some images too slow, so some of them are added to the movieclip although they are loaded after the one that should go next.
so what i mean :
1.png is loaded
1.png is added
2.png is loaded
3.png is loaded
3.png is added
2.png is added
so my question:
is there any other propper/better way to make that slideshow load images from a textfile where just the full names of the images ( that are in a subfolder ) are listet ?
thanks for any suggestions.
g.r.
Queue your image loading to have them ordered.
Rough example:
var queue:Array = []; // Populated from your text/whatever file.
// If you want the images to load from first to last you will need
// to use queue.reverse() once you get the filenames.
/**
* Beings loading the next image in queue.
* Ignored if the queue has no remaining items.
*/
function loadNext():void
{
if(queue.length > 0)
{
var imgSrc:String = queue.pop();
var ldr:Loader = new Loader();
ldr.load(new URLRequest(imgSrc));
ldr.addEventListener(Event.COMPLETE, _done);
}
}
/**
* Called once a Loader instance has finished loading a resource.
* #param e Event.COMPLETE.
*/
function _done(e:Event):void
{
e.target.removeEventListener(Event.COMPLETE, _done);
container.addChild(e.target as DisplayObject);
// Begin loading the next image in queue.
loadNext();
}

AS3 disable editable/selectable for textInput inside of a Datagrid

I am currently trying to disable the selectable / editable / or change the textInput to dynamic to get my desired result.
I've got a custom datagrid with dropdowns and text input areas. However, if there is no data in my Model # column, I do not want to allow for any entry in the corresponding PurchasePrice cell.
col1 = new DataGridColumn("Model");
col1.headerText = "Model #";
c2.consumables_dg.addColumn(col1);
col1.width = 60;
col1.editable = false;
col1.sortable = false;
col1.cellRenderer = AlternatingRowColors_editNum_PurchasePrice;
col1 = new DataGridColumn("PurchasePrice");
col1.headerText = "Purchase Price";
c2.consumables_dg.addColumn(col1);
col1.width = 60;
col1.editable = false;
col1.sortable = false;
col1.cellRenderer = AlternatingRowColors_editNum_PurchasePrice;
I've tried various items in the AlternatingRowColors_editNum_PurchasePrice, but nothing seems to work as of yet. Please Look at what I've tried in my else statement of the if(__enbaled)
package{
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import fl.controls.TextInput;
import flash.text.TextFieldType;
import flash.events.Event;
import fl.controls.listClasses.ListData;
import fl.controls.listClasses.ICellRenderer;
public class AlternatingRowColors_editNum_PurchasePrice extends TextInput implements ICellRenderer
{
protected var _data:Object;
protected var _listData:ListData;
protected var _selected:Boolean;
private var __enabled:Boolean = false;
public static var _stage;
public static var _alignment;
private var tf:TextFormat;
public function AlternatingRowColors_editNum_PurchasePrice()
{
tf = new TextFormat();
if(__enabled){
if(_alignment == 2){
tf.align = TextFormatAlign.RIGHT;
}else if(_alignment == 1){
tf.align = TextFormatAlign.CENTER;
}else{
tf.align = TextFormatAlign.LEFT;
}
restrict = "0-9.";
addEventListener(Event.CHANGE, textChange);
}else{
//this.selectable = false; // did not work
//textField.selectable = false; // did not work
//textField.type = dynamic; // did not work
//textField.type = TextFieldType.DYNAMIC; // did not work
//this.mouseEnabled = false; // did not work
//this.tabEnabled = false; // did not work
//textField.mouseEnabled = false; // did not work
//textField.tabEnabled = false; // did not work
//selectable = false; // did not work
//this.selectable = false; // did not work
//_enabled = false; // did not work
//-----------------------------------------------------------
// *** Corresponding Entry to enable was placed above ***
//-----------------------------------------------------------
}
super();
}
public function textChange(Event):void{
//trace(_data.Discount);
_data.PurchasePrice = text;
}
public function get data():Object
{
return _data;
}
public function set data(value:Object):void
{
_data = value;
text = value.PurchasePrice;
if(value.Model != "") __enabled = true;
if (value.id % 2 == 0) {
setStyle("upSkin", AlternateColor1 );
} else {
setStyle("upSkin", AlternateColor2 );
}
}
public function get listData():ListData
{
return _listData;
}
public function set listData(value:ListData):void
{
_listData = value;
}
public function get selected():Boolean
{
return _selected;
}
public function set selected(value:Boolean):void
{
_selected = value;
}
public function setMouseState(state:String):void
{
}
override protected function drawLayout():void
{
textField.setTextFormat(tf);
super.drawLayout();
}
}
}
Am I just approaching this the wrong way? Instead of trying to stop the mouse, tab, or convert the text field to dynamic, should I be attempting something else in order to get the desired result?
Thanks,
jc
The easiest approach is to change the TextField type itself and then change it's selectable property:
//disable input
tf.selectable = false;
tf.type = TextFieldType.DYNAMIC;
//enable input
tf.selectable = true;
tf.type = TextFieldType.INPUT;
Also don't forget to set the mouseEnabled & tabEnabled properties as appropriate/needed for your fields.
Be sure to import the TextFieldType at the top of your class / page / frame as:
import flash.text.TextFieldType;

Use MouseEvent for getting Object`s public variables

I have a simple problem which is not easy at the moment. this is a text field class which add a text field to a movieclip (passed from root class) and also save(buffer) some data (like xml file name) in it`s public variables:
package src {
import flash.display.Shape;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class menuitem00 extends Shape {
public var activateFlag:Boolean = false;
public var name00:String = "";
public var xml00:String = "";
public var txt00:TextField = new TextField();
private var OM:MovieClip;
private var id00:Number = 0;
public function menuitem00(n:int ,OE:MovieClip ,xmlf:String):void {
trace (" Text Field Object with buffer ");
OM = OE; id00 = n; xml00 = xmlf;
}
public function init():void{
// textfield
txt00.selectable = false;
txt00.autoSize = TextFieldAutoSize.LEFT;
txt00.defaultTextFormat = new TextFormat("Impact", 36,0x66AAFF);
txt00.text = name00;
txt00.border = true;
txt00.borderColor = 0x00FF00;
txt00.sharpness = 100;
txt00.x = 0;
txt00.y = (id00 * txt00.height) + 1;
txt00.z = 0;
OM.addChild(txt00);
}
}
}
.. now I use a for loop to add instance of that class to a movie clip on stage:
for (var i:int = 0; i<Bunker[0]["trans0size"]; i++) {
menuData[i] = new menuitem00(i, menu_mc, Bunker[0]["transfile0" + i]);// pass i , a movieclip named menu_mc and an xml file name
menuData[i].name00 = Bunker[0]["transtitle0" + i]; // pass text
menuData[i].activateFlag = true; // set actveFlag to true
menuData[i].init(); // run init() inside the instance. it adds textfield to the menu_mc and also set name00 as Text for the textField
}
also I add mouse event for clicking on the stage,
stage.addEventListener(MouseEvent.CLICK, clk, false, 0, true);
public function clk(evt:MouseEvent):void{ //// mouse
trace (" Name: " + evt.target.text);
//trace (evt.target.xml00);
}
HERE IS MY PROBLEM --> I want to get "xml00" var from that instance on the stage by mouse click but, trace (evt.target.xml00); is not working .. also trace (evt.target.name00); is not working. I can get everything like .alpha or .text from txt00 but not other variables in my object. Any idea ?
don't add the click-listener to the STAGE but to you object.
menuData[i].addEventListener(MouseEvent.CLICK, clk, ...);
and add the following line to your menuitem00 class:
this.mouseChildren = false;
so you can be sure that the evt.target is an object of this class and not a child (like the textfield or something else).
edit
if you want to keep the stage-listener, try this:
stage.addEventListener(MouseEvent.CLICK, clk, ...);
private function clk (evt:MouseEvent):void
{
if (evt.currentTarget is menuitem00)
{
var item:menuitem00 = evt.currentTarget as menuitem00;
trace(item.xml00); // or any other public variable
}
}
but still add the mouseChildren = false; to your class.
edit2
make the menuitem00 class a sprite (and rename it pls):
public class MenuItem extends Sprite {
private var _activateFlag:Boolean;
private var _xml:String;
private var _txt:TextField;
private var _id:Number;
public function MenuItem (n:int, xmlf:String) {
trace (" Text Field Object with buffer ");
_id = n;
_xml = xmlf;
_activeFlag = true;
this.mouseChildren = false;
// create txt
_txt = new TextField();
// do the formating here ...
this.addChild(_txt);
}
public function getXml():String {
return _xml;
}
}
in the for loop you would do sth like this:
for (var i:int = 0; i<Bunker[0]["trans0size"]; i++) {
var item:MenuItem = new MenuItem(i, Bunker[0]["transfile0" + i]);
item.addEventListener(MouseEvent.CLICK, clk, false, 0, true);
menu_mc.addChild(item);
menuData[i] = item;
}
private function clk (evt:MouseEvent):void {
var item:MenuItem = evt.target as MenuItem;
if (item != null) {
trace(item.getXml()); // use a getter and don't access the variable directly
}
}
and pls re-read your actionscript (or programming) books, you're doing some really bad things i your code that you shouldn't.