View Full Version : Textfield: Programatic Instance Names
Pherank
March 21st, 2008, 06:10 PM
I have some menu code which creates a number of submenu items and I want to be able to load SWF content into a container MC on the stage onClick. These are the relevant sections of code:
function onSelectItem(event:MouseEvent):void {
switch (instanceName) {
case instance1 :
startLoad("roots.swf");
break;
case instance2 :
startLoad("arrival.swf");
break;
case instance3 :
startLoad("post-war.swf");
break;
case instance4 :
startLoad("doubts.swf");
break;
default :
startLoad("roots.swf");
}
}
//CODE FROM A DIFFERENT FUNCTION
id = int(event.target.name);
submenus = menuArray[id].length;
var menuSubHolder:Sprite = new Sprite();
menuSubHolder.name = "sub" + String(id);
menuSubHolder.y = event.target.y + 18;
for (var j:int = 0; j < submenus; j++) {
menuSubText = new TextField();
menuSubText.x = event.target.x + 18;
menuSubText.y = j * 18;
menuSubText.text = menuArray[id][j];
menuSubText.autoSize = TextFieldAutoSize.LEFT;
menuSubHolder.addChild(menuSubText);
// DISPLAY CONTENT ON CLICK
menuSubText.addEventListener(MouseEvent.CLICK, onSelectItem);
}
The problem I'm finding is that AS 3 assigns some unpredictable instance numbers to the submenu items. How can I assign my own instance names to each of the submenu textfields on creation?
android
March 21st, 2008, 07:04 PM
If i were you, I'd create an Array, to store all of the buttons, and the .swf files they are supposed to open. That way you are sure what links to what.
Alternatively, you could use a Dictionary. Like so:
var d = new Dictionary()
d[menuSubText] = "someSWFfile.swf"
menuSubText.addEventListener(MouseEvent.CLICK,mous eClicked);
public function mouseClicked(e:MouseEvent){
startLoad(d[e.currentTarget]);
}
Pherank
March 21st, 2008, 07:09 PM
Thanks Android. I didn't know about dictionary objects. How would you set it up to assign a different SWF for each submenu item? Do you have to create a dictionary for each item?
var a = new Dictionary();
var b = new Dictionary();
var c = new Dictionary();
var d = new Dictionary();
a[menuSubText] = "someSWFfile.swf"
b[menuSubText] = "someSWFfile.swf"
c[menuSubText] = "someSWFfile.swf"
d[menuSubText] = "someSWFfile.swf"
android
March 21st, 2008, 07:16 PM
Nope, you'd only need a single dictionary, dictionaries are like "Object"s, but they can use another object as a key.
var dict = new Dictionary();
for (var j:int = 0; j < submenus; j++) {
menuSubText = new TextField();
menuSubText.text = menuArray[id][j];
menuSubHolder.addChild(menuSubText);
dict[menuSubText] = "theRightSWFFile.swf" //Maybe this is menuArray[id][j] also?
// DISPLAY CONTENT ON CLICK
menuSubText.addEventListener(MouseEvent.CLICK, onSelectItem);
}
function onSelectItem(event:MouseEvent):void {
startLoad(dict[event.currentTarget]);
}
Pherank
March 21st, 2008, 07:30 PM
I'm not seeing how you would make a different assignment for each menu item - they can't all point to the same SWF. Also "dict" isn't accessible within this func:
function onSelectItem(event:MouseEvent):void {
startLoad(dict[event.currentTarget]);
}
Pherank
March 21st, 2008, 08:01 PM
Hey Android - this works:
dict[menuSubText] = menuArray[id][j] + ".swf";
(and I had to move the onSelectItem function within the same public function as the other code)
I have an interesting problem though - If there's a phrase on the menu button, like "The Bubble Bursts", there isn't going to be a file named "The Bubble Bursts.swf". So I guess I still need a switch statement to replace strings.
android
March 21st, 2008, 08:21 PM
Yes, or you need to put the target SWF files in your "menuArray".
menuArray[id][x] = {text:"The Bubble Bursts",file:"bubblebursts.swf"}
//Then assign like:
for (var j:int = 0; j < submenus; j++) {
menuSubText.text = menuArray[id][j].text
dict[menuSubText] = menuArray[id][j].file
}
Pherank
March 21st, 2008, 08:50 PM
I don't understand how to use this line:
menuArray[id][x] = {text:"The Bubble Bursts",file:"bubble.swf"};
Where is that placed? I just get errors on it.
android
March 22nd, 2008, 10:40 AM
Oh, i'm sorry.
I don't know where you make your menuArray, but i meant that you put this line where you make your menu array.
Right now your menuArray stores only a text right? But instead of storing a text, you could store an object that contains both the text and the link to the file.
var obj = new Object()
obj = {text: "The Bubble Bursts", file: "bubblebursts.swf"}
Now you would have to do this for every item in your menu, so it would be something like:
menuArray[id][0] = {text:"First item", file: "firstitem.swf"}
menuArray[id][1] = {text:"Second item", file: "seconditem.swf"}
//etcetera
//Then you can ask:
trace(menuArray[id][0].text) // gives "First item"
trace(menuArray[id][1].file // gives "seconditem.swf"
I don't know where you are getting your data from and what "id" stands for, but you'd have to fill that in yourself.
Pherank
March 22nd, 2008, 04:21 PM
OK, I think I see. I'm currently stuck trying to create a selected state for the menu items, but it doesn't seem doable with AS 3 and this particular menu code. Here's all the submenu code I'm working with (I'm leaving out other code for now):
function onClick(event:MouseEvent):void {
function onSelectItem(event:MouseEvent):void {
var defaultFormat:TextFormat = new TextFormat();
defaultFormat.color = default_color;
var newFormat:TextFormat = new TextFormat();
newFormat.color = selected_color;
//event.currentTarget.setTextFormat(newFormat); // No error but no result
//event.target.setTextFormat(newFormat); // No error but no result
//TextField(event.target).setTextFormat(newFormat); // No error but no result
//TextField(event.target).mouseEnabled = false;
menuSubText.setTextFormat(newFormat);
menuSubText.mouseEnabled = false;
//var activeitem = event.currentTarget;
switch (dict[event.currentTarget]) {
case "Post-War Optimism.swf" :
startLoad("post-war.swf");
break;
case "The Bubble Bursts.swf" :
startLoad("bubble.swf");
break;
default :
startLoad(dict[event.currentTarget]);
}
}
id = int(event.target.name);
submenus = menuArray[id].length;
var dict = new Dictionary();
var menubttn = new Dictionary();
if (itemArray[id] == "closed") {
var menuSubHolder:Sprite = new Sprite();
menuSubHolder.name = "sub" + String(id);
//trace(menuSubHolder.name);
menuSubHolder.y = event.target.y + 18;
for (var j:int = 0; j < submenus; j++) {
menuSubText = new TextField();
menuSubText.x = event.target.x + 18;
menuSubText.y = j * 18;
// Set default formatting before text assignment
menuSubText.embedFonts = true;
menuSubText.selectable = false;
menuSubText.antiAliasType = AntiAliasType.ADVANCED;
//
// DETERMINE FILENAME FOR EACH MENU ITEM
// (passed to onSelectItem function)
dict[menuSubText] = menuArray[id][j] + ".swf";
//
var format:TextFormat = new TextFormat();
format.font = new Frutiger().fontName;
format.font = "Frutiger";
format.color = default_color;
format.size = 12;
menuSubText.defaultTextFormat = format;
//
menuSubText.text = menuArray[id][j];
menuSubText.autoSize = TextFieldAutoSize.LEFT;
menuSubHolder.addChild(menuSubText);
//
menuSubText.addEventListener(MouseEvent.ROLL_OVER, RollOver);
menuSubText.addEventListener(MouseEvent.ROLL_OUT, RollOut);
menuSubText.addEventListener(MouseEvent.CLICK, onSelectItem);
if (submenus == (j + 1)) {
subMenuHeight = menuSubText.y + 18;
}
}
addChild(menuSubHolder);
menuSubHolderMask = new Sprite();
menuSubHolderMask.name = "mask" + String(id);
menuSubHolderMask.graphics.beginFill(0xFF0000);
// Adjust width of mask for longer text
menuSubHolderMask.graphics.drawRect(maskXCoord, 0, 160, subMenuHeight);
menuSubHolder.addChild(menuSubHolderMask);
TweenLite.from(menuSubHolderMask, 0.5, {height:0, ease:Strong.easeOut});
menuSubHolder.mask = menuSubHolderMask;
for (var k:int = 0; k < itemArray.length; k++) {
if (k != id) {
target = getChildByName("sub" + String(k));
if (k < id) {
if (itemArray[k] == "opened") {
subOpenedSign = target.height;
itemArray[k] = ["closed"];
TweenLite.to(getChildByName(String(id)), 0.5, {y:"-" + target.height, ease:Strong.easeOut});
TweenLite.to(getChildByName("sub" + String(id)), 0.5, {y:"-" + target.height, ease:Strong.easeOut});
for (var m:int = k + 1; m < id; m++) {
TweenLite.to(getChildByName(String(m)), 0.5, {y:"-" + target.height, ease:Strong.easeOut});
}
removeChild(target);
}
} else if (k > id) {
if (itemArray[k] == "closed") {
if (subOpenedSign > 0) {
var positiveNumber:int = subOpenedSign - subMenuHeight;
if (positiveNumber < 0) {
positiveNumber = subMenuHeight - subOpenedSign;
TweenLite.to(getChildByName(String(k)), 0.5, {y:"+" + positiveNumber, ease:Strong.easeOut});
} else {
TweenLite.to(getChildByName(String(k)), 0.5, {y:"-" + positiveNumber, ease:Strong.easeOut});
}
} else if (subOpenedSign == 0) {
TweenLite.to(getChildByName(String(k)), 0.5, {y:"+" + subMenuHeight, ease:Strong.easeOut});
}
}
if (itemArray[k] == "opened") {
subOpenedSign = target.height;
TweenLite.to(getChildByName(String(k)), 0.5, {y:"+" + subMenuHeight, ease:Strong.easeOut});
removeChild(target);
itemArray[k] = ["closed"];
}
}
}
}
subOpenedSign = 0;
itemArray[id] = ["opened"];
} else if (itemArray[id] == "opened") {
itemArray[id] = ["closed"];
target = getChildByName("sub" + String(id));
for (var l:int = id + 1; l < itemArray.length; l++) {
if (l != id ) {
TweenLite.to(getChildByName(String(l)), 0.5, {y:"-" + target.height, ease:Strong.easeOut});
TweenLite.to(getChildByName("sub" + String(l)), 0.5, {y:"-" + target.height, ease:Strong.easeOut});
}
}
removeChild(target);
}
}
Inside of function "onSelectItem" the line of code,
menuSubText.setTextFormat(newFormat); will highlight only the very last item in a submenu list no matter which item is clicked since it is referencing the whole menuSubText loop (which ends on the last item). I basically just need to say "this" as in AS 2 but using terms like
event.currentTarget.setTextFormat(newFormat); just don't work. I'm stumped now since I'm not getting any error message with these either. AS 3 acts like I'm saying the right thing but the formatting is not applied.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.