I'm rendering display objects to the stage depending on the given XML elements, as you can see here:
PageRenderer.as
private static var curElements:Dictionary = new Dictionary();
//renders the current page
private static function renderCode(pageCode:XML):void
{
if (pageCode)
{
//loop all elements from top to bottom
for each (var child:XML in pageCode.descendants())
{
var render:DisplayObject = ElementRenderer.render(child);
if (render)
{
WebBuilder.mainDisplay.addChild(render);
curElements[child] = render;
}
}
}
}
So, each XML element has an associative rendered shape. If you have the XML element, you can access the shape like this: var shape:DisplayObject = curElements[xmlElement];
This works fine within the same class.
However, now I also have the ElementSelector class which deals with selection of shapes and reflects the actions done with the shape to the xml element. To do this, one needs to get the XML element when the shape has been clicked:
ElementSelector.as
private static var currentSelection:XML;
//fired when the stage has been clicked
private static function stageClicked(event:MouseEvent):void
{
//if the element selector has been enabled
if (enabled)
{
var allPages:Array = CodeHandler.getPageCodes();
var mainElement:XML = allPages[CodeHandler.getCurPageId()];
var targetElement:XML = CodeHandler.getDeepestElementAtPos(mainElement, 0, event.stageX, event.stageY)["xml"];
if ((targetElement.localName() != "page") && (targetElement != currentSelection))
{ //we have a new element selected
Utils.log("ElementSelector now selecting: " + targetElement.localName());
select(targetElement);
}
else if ((targetElement.localName() == "page") && (currentSelection))
{ //the selection has been lost
Utils.log("ElementSelector now dropping selection.");
deselect();
}
}
}
//sets the new selection
private static function select(element:XML):void
{
if (currentSelection) deselect();
currentSelection = element;
var curElements:Dictionary = PageRenderer.getElements();
var render:DisplayObject = curElements[element];
trace(render);
}
//drops the current selection
private static function deselect():void
{
currentSelection = null;
}
I added the StageClicked event function only so you have an idea of how my procedure works. That function itself works fine. The problem seems to lie within the select() method.
Now, the strange thing is, that curElements[element] returns undefined and render returns null.
I tried to debug it like this (at bottom of select method):
for (var key:Object in curElements)
{
if (key === element)
{
trace("key === element");
}
trace(curElements[key] === curElements[element]);
trace(curElements[key]);
trace(curElements[element]);
}
It returns:
key === element
false
[object Shape]
undefined
Why is this happening? If a === b, then dic[a] === dic[b], right? Well, apparently not.
So, the key really is there... and it's the same as the key it was set with.
Why isn't it returning the associative display object?