View Full Version : Create variables with loop???
REEFˇ
August 16th, 2008, 05:05 PM
var labels:Array = new Array("label 1", "label 2");
for (var i:int = 0; i < 2; i++) {
myItem+[i] = new ContentMenuItem(labels[0]);
myItem+[i].addEventListener(ContextMenuEvent.MENU_ITEM_SELEC T, doSomething);
myCustomMenu.customItems.push(myItem+[i]);
}Possible? I want variables to end up myItem0, myItem1, etc.
Krilnon
August 16th, 2008, 05:12 PM
What's wrong with something like this?:
var labels:Array = new Array("label 1", "label 2");
for (var i:int = 0; i < 2; i++) {
var myItem:ContentMenuItem = new ContentMenuItem(labels[0]);
myItem.addEventListener(ContextMenuEvent.MENU_ITEM _SELECT, doSomething);
myCustomMenu.customItems.push(myItem);
}
Are you trying to avoid the classic value-of-i problem, or what?
The answer to the question posed in your topic kind of depends on what you're willing to consider a variable. I would only consider things declared with var to be variables, but other people would also call dynamically defined object properties variables, etc. There definitely isn't a way to declare the var sort of variables within a loop, though, because they need to be declared at/before compile-time.
REEFˇ
August 16th, 2008, 05:23 PM
Okay my final problem is what I've yet to understand in AS3. How do I do something unique for every item in the menu thats chosen? They're all running itemChosen and so they all do the same thing. If I could pass a value into itemChosen and then the function could react from there...things would be easier. Any ideas?!
REEFˇ
August 16th, 2008, 05:49 PM
I cant seem to figure out how to make each menuItem have a different function. Look how long the code is when I dont use for loops, uggh:
package com.website.utils
{
import flash.display.Sprite;
import flash.events.ContextMenuEvent;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
public class CustomMenu extends Sprite
{
private var menuLabels:Array = new Array("lab1", "lab2", "lab3", "lab4", "lab5", "lab6");
private var labelsLength:int = menuLabels.length;
private var stageMenu:ContextMenu = new ContextMenu();
public function CustomMenu(level:Object):void {
stageMenu.hideBuiltInItems();
var menuItem0:ContextMenuItem = new ContextMenuItem(menuLabels[0]);
var menuItem1:ContextMenuItem = new ContextMenuItem(menuLabels[1]);
var menuItem2:ContextMenuItem = new ContextMenuItem(menuLabels[2]);
var menuItem3:ContextMenuItem = new ContextMenuItem(menuLabels[3]);
var menuItem4:ContextMenuItem = new ContextMenuItem(menuLabels[4]);
var menuItem5:ContextMenuItem = new ContextMenuItem(menuLabels[5]);
menuItem0.addEventListener(ContextMenuEvent.MENU_I TEM_SELECT, menuAction0);
menuItem1.addEventListener(ContextMenuEvent.MENU_I TEM_SELECT, menuAction1);
menuItem2.addEventListener(ContextMenuEvent.MENU_I TEM_SELECT, menuAction2);
menuItem3.addEventListener(ContextMenuEvent.MENU_I TEM_SELECT, menuAction3);
menuItem4.addEventListener(ContextMenuEvent.MENU_I TEM_SELECT, menuAction4);
menuItem5.addEventListener(ContextMenuEvent.MENU_I TEM_SELECT, menuAction5);
stageMenu.customItems.push(menuItem0);
stageMenu.customItems.push(menuItem1);
stageMenu.customItems.push(menuItem2);
stageMenu.customItems.push(menuItem3);
stageMenu.customItems.push(menuItem4);
stageMenu.customItems.push(menuItem5);
menuItem5.separatorBefore = true;
level.contextMenu = stageMenu;
}
private function menuAction0(event:ContextMenuEvent):void {
trace(0);
}
private function menuAction1(event:ContextMenuEvent):void {
trace(1);
}
private function menuAction2(event:ContextMenuEvent):void {
trace(2);
}
private function menuAction3(event:ContextMenuEvent):void {
trace(3);
}
private function menuAction4(event:ContextMenuEvent):void {
trace(4);
}
private function menuAction5(event:ContextMenuEvent):void {
trace(5);
}
}
}
Krilnon
August 16th, 2008, 06:09 PM
public function CustomMenu(level:Object):void {
stageMenu.hideBuiltInItems();
for(var i:int = 0; i < 6; i++){
var menuItem:ContextMenuItem = new ContextMenuItem(menuLabels[i]);
menuItem.addEventListener(ContextMenuEvent.MENU_IT EM_SELECT, this['menuAction' + i]);
stageMenu.customItems.push(menuItem);
if(i == 5) menuItem.separatorBefore = true;
}
level.contextMenu = stageMenu;
}
Some people who really hate accessing object properties dynamically like this would instead recommend something like putting all of the functions in an array beforehand. You could do that, too, but you're never going to notice a difference in performance and the amusingly subjective ideological gains from such an approach are kind of slim anyway.
REEFˇ
August 16th, 2008, 06:19 PM
Thats fine with me. Performance is what I care about really, as long as its all the same I'm fine with that kind of referencing (don't really understand why it sucks, but I'm sure you developers have your quirks).
& Thanks kril, that works good. Just thought I didn't need different functions for each item. AS3 really is demanding...
Krilnon
August 16th, 2008, 06:28 PM
Just thought I didn't need different functions for each item. AS3 really is demanding...
You probably don't need different functions for what you're doing. You can access the ContextMenuItem with event.target. If you wanted to, you could use an Array or Dictionary to store the value of i that was originally used, but it is probably enough to have access to the actual menu item.
REEFˇ
August 16th, 2008, 06:28 PM
@Kril, thats exactly what I want to do. But I cant seem to set a property to menuItem (Im guessing since its a contextMenuItem) and then use event.target.property to run different things. I get errors and whatnot, so I'm assuming it's a waste of time.
Okay, to share with those who need a custom context menu class, here is my final code.
package com.yourwebsite.utils
{
import flash.display.Sprite;
import flash.events.ContextMenuEvent;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
public class CustomMenu extends Sprite
{
private var menuLabels:Array = new Array("Home", "About", "Garbage Bags", "Delicious Meat", "Bacon Chocolate");
private var labelsLength:int = menuLabels.length;
private var menuItem:ContextMenuItem;
private var stageMenu:ContextMenu = new ContextMenu();
public function CustomMenu(targetObject:Object):void {
stageMenu.hideBuiltInItems();
for (var i:int = 0; i < labelsLength; i++) {
menuItem = new ContextMenuItem(menuLabels[i]);
menuItem.addEventListener(ContextMenuEvent.MENU_IT EM_SELECT, this["menuAction" + i]);
stageMenu.customItems.push(menuItem);
i == labelsLength - 1? menuItem.separatorBefore = true : null;
}
targetObject.contextMenu = stageMenu;
}
private function menuAction0(event:ContextMenuEvent):void {
}
private function menuAction1(event:ContextMenuEvent):void {
}
private function menuAction2(event:ContextMenuEvent):void {
}
private function menuAction3(event:ContextMenuEvent):void {
}
private function menuAction4(event:ContextMenuEvent):void {
}
}
}
// Usage on the stage
var rightClickMenu:CustomMenu = new CustomMenu(this);
// or for movieclips for example
var specialBoxMC:yourBox = new yourBox();
addChild(specialBoxMC);
var specialBoxMCMenu:CustomMenu = new CustomMenu(specialBoxMC);I'm guessing you pros can look at this, laugh at how ugly it is and probably can improve it 100x since I'm new to this. I'm trying though, and in no way am I saying "here use this other newbies". Just putting this up incase someone needs a quick answer through a search. Improvements to this is greatly appreciated!
Krilnon
August 16th, 2008, 06:41 PM
But I cant seem to set a property to menuItem (Im guessing since its a contextMenuItem) and then use event.target.property to run different things. I get errors and whatnot, so I'm assuming it's a waste of time.
Well, it probably is a waste of time, but you could use a property of the item to determine its identity. For example, the caption property would exist and could tell you exactly which item had been clicked. However, it doesn't really make sense to have one function that controls buttons with completely different functions. If, for example, your right click menu had 'jump' and 'talk' menu items, then it would make sense to have separate functions jump and talk, instead of having something like:
switch(event.target.caption){
case 'jump' :
// jumping code
break;
case 'talk' :
// talking code
break;
}
At some point, there will definitely be some trade-offs between having clear code and being able to stuff everything into a loop.
REEFˇ
August 16th, 2008, 06:45 PM
Yep, I've been told that before. Its been kind of drilled into my head that uglier/longer code is worse code, I'm guessing its not always true. And having different functions actually does seem more fun to come back to and debug/change than having to edit a switch/case for example.
*edit: thanks for teaching me about the caption property! that's gonna be really helpful. All this time I thought ContextMenuItem instances didn't have properties. See, it's little things like that which make me feel better about coding in the future.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.