Inspired by the very enjoyable challenge from my friends at Apt, we’re doing our own. In a recent project I needed a contextMenu which allowed the user to select the number of pictures to show in a ad template. The choices change with the size of the ad, so every time the context menu is shown, it’s regenerated first. But for the challenge, you can skip the regenereation bit.
I’ll post my solution on tuesday (2.jun)
Here’s the starting point (within a function)
var myCM:ContextMenu = contextMenu
var cmi:ContextMenuItem;
var selFunction:Function = function (e:ContextMenuEvent = null):void {
var findRightNumberOfPicts:int
trace(findRightNumberOfPicts)
}
myCM.customItems = [];
myCM.customItems.push(new ContextMenuItem("Number of pictures: ", false, false))
var alt:Array = [1, 4, 5, 9];
for (var i:int = 0 ; i < alt.length ; i++) {
cmi = new ContextMenuItem(alt[i] + " pictures"+(alt[i] > 1 ? "s" : ""));
cmi.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, selFunction);
myCM.customItems.push(cmi)
}
The challenge is to get the right number of pictures in the selFunction. I.e. if you select “4 pictures” in the context menu, you should get “4″ traced out.
My solution:
Use a Dictionary instance to store data with ContextMenyItem instance as reference. (Code added to starting point is underlined). Not familiar with the built in Dictionary class? It’s much the same as an array as hash-table, but with instances as keys instead of strings or numbers. Can be very useful.
var myCM:ContextMenu = contextMenu var cmi:ContextMenuItem; var cmiDict:Dictionary = new Dictionary; var selFunction:Function = function (e:ContextMenuEvent = null):void { var findRightNumberOfPicts:int = cmiDict[e.target] trace(findRightNumberOfPicts) } myCM.customItems = []; myCM.customItems.push(new ContextMenuItem("Number of pictures: ", false, false)) var alt:Array = [1, 4, 5, 9]; for (var i:int = 0 ; i < alt.length ; i++) { cmi = new ContextMenuItem(alt[i] + " pictures"+(alt[i] > 1 ? "s" : "")); cmiDict[cmi] = alt[i]; cmi.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, selFunction); myCM.customItems.push(cmi) }

Sweet! I’ll have a crack at this, allthough I’m not really familiar to the ContextMenu. The trick is to get the event.target i reccon. This is sort of a hack to get the caption text and then just read the value. Maybe the ContextMenuItem can contain some data so that we won’t have to read out the caption?
var selFunction:Function = function (e:ContextMenuEvent):void {
var cmi:ContextMenuItem = e.target as ContextMenuItem;
var findRightNumberOfPicts:int = int(cmi.caption.split(” “)[0])
trace(findRightNumberOfPicts)
}
Thomash
Yes – that would work. But it’s a bit inelegant to parse the title – it’s easy to get into situations where future changes makes the parsing fail. Eg. if the captions started reading “Use 2 pictures”.
Yeah, you’re right!
This would also work, but would break if you remove the “Number of pictures: ” item:
var findRightNumberOfPicts:int = alt[myCM.customItems.indexOf(cmi)-1];
That would work – and it’s better that parsing the caption. But I do think I have an even better solution.