28
May
09

Open challenge: Dynamic ContextMenu

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)
}
Advertisements

4 Responses to “Open challenge: Dynamic ContextMenu”


  1. May 28, 2009 at 3:57 pm

    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

    • May 28, 2009 at 4:07 pm

      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”.

  2. May 28, 2009 at 4:26 pm

    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];


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: