Search code examples
actionscript-3airmapquest

put a "back" button in front on an embed map


I've integrate a map from MapQuest in my Adobe Air app (in AS3).

The map is taking all the screen as I want.

BUT, I'd like to add a "back" button in order to go back to the previous menu.

Here's my code :

var NoumeaNord:TileMap = new TileMap("KEYcode");

            //set the size of the map
            NoumeaNord.size = new Size(800, 533);

            //add the map to the sprite.
            addChild(NoumeaNord);

              NoumeaNord.addControl(new SMLargeZoomControl());
              NoumeaNord.addControl(new MouseWheelZoomControl());
              NoumeaNord.addShape(new Poi(new      LatLng(-22.2758000,166.4580000)));
              NoumeaNord.setCenter(new LatLng(-22.2758000,166.4580000),15);


         function addBackBtn():void{

            var back:MovieClip;
                    back = new backBtn
            addChild(back);
            back.x = 0;
            back.y = 400;
            setChildIndex(back,0);
}

Don't know why but the BackBtn won't be in front of the map ! I've tried with setChildIndex(back,-1); but it makes an error : "RangeError: Error #2006: index is off limit".

Any idea ?


Solution

  • In this situation I use holders like this:

    private var bgHolder:Sprite;
    private var contentHolder:Sprite;
    private var menuHolder:Sprite;
    

    Somewhere in the constructor I run a method that will set my holders. For example:

    function setHolders() 
    {
        bgHolder = new Sprite();
        addChild(bgHolder);
    
        contentHolder = new Sprite();
        addChild(contentHolder);
    
        menuHolder = new Sprite();
        addChild(menuHolder);
    }
    

    Then I just add my content to the desired holder in any order between destination holder and I can always be sure to add my content to the correct "layer";

    So just add your back button to the menuHolder:

    menuHolder.addChild(back);
    

    And don't worry about adding it below the map or out of index, because the map, in this case, would be added to the contentHolder!

    Hope that helps!