SOLVED: How To File Reference with Starling?

by bioRex21
Click the gradient.

If you have the FP debugger, after clicking the gradient you will see the error that Window Prompts such as "browse for file" should be triggered inside MOUSE click handler function (or mouse down).

But since this is Starling, I use TOUCH.END, so the error happens. Do I really need to create a button (that is outside Starling) for this?

SOLUTION:
Use Starling.current.nativeStage to listen for the click event to open the file select window.
(This doesn't do anything after you select a file, just wanted to know how to do it).
♥0 | Line 88 | Modified 2015-09-09 08:51:13 | MIT License
play

ActionScript3 source code

/**
 * Copyright bioRex21 ( http://wonderfl.net/user/bioRex21 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/wQM0
 */

package
{
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.ui.Multitouch;
    import flash.ui.MultitouchInputMode;
    import starling.core.Starling;

    public class Main extends flash.display.Sprite
    {
        private var _starling:Starling;    

        public function Main()
        {            
            this.addEventListener(flash.events.Event.ADDED_TO_STAGE, onStage);
        }        

        private function onStage(event:flash.events.Event):void
        {                
            Starling.multitouchEnabled = true;  
            _starling = new Starling(Scene1, stage);                   
            _starling.start();
        }
    }
}// end main





import flash.display.Loader;
import flash.net.FileFilter;
import flash.net.FileReference;
import starling.display.DisplayObject;
import starling.display.Image;
import starling.display.Quad;
import starling.display.Sprite;
import starling.events.Event;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;
import flash.events.Event;
import starling.core.Starling;


class Scene1 extends starling.display.Sprite
{

    private var fileRef:FileReference;
    private var loader:Loader;   
    
    public function Scene1()
    {

        if (stage)

            _init();

        else

            addEventListener(starling.events.Event.ADDED_TO_STAGE, _init);

    }    

    private function _init(e:starling.events.Event = null):void
    {

        removeEventListener(starling.events.Event.ADDED_TO_STAGE, _init);        

        var q:Quad = new Quad(stage.stageWidth, stage.stageHeight);
        q.setVertexColor(0, 0x000000);
        q.setVertexColor(1, 0xAA0000);
        q.setVertexColor(2, 0x00FF00);
        q.setVertexColor(3, 0x0000FF);
        addChild(q);

        q.alpha = 0.3;       

        this.addEventListener(TouchEvent.TOUCH, onMouseDown);   
        
        
        //In order to make FileReference know it was dispatched by a click,
        //the click listener needs to be attached to nativeStage.
        //THANK YOU WLAD FOR THE SOLUTION! 
        Starling.current.nativeStage.addEventListener('click', openFolder);

    }

    

    private function onMouseDown(e:TouchEvent):void
    {
        var touches:Vector.<Touch> = e.getTouches(this);
        
        if (touches.length == 1)
        {
            var touch:Touch = touches[0];           

            if (touch.phase == TouchPhase.ENDED)
            {
                

            }                       

        } //touch length  

    }//function

    

    private function openFolder(e:*):void
    {

        fileRef = new FileReference();
        fileRef.addEventListener(flash.events.Event.SELECT, selectHandler);        

        var arr:Array = [];
        arr.push(new FileFilter("Images", ".gif;*.jpeg;*.jpg;*.png"));
        fileRef.browse(arr);

    }

    

    private function selectHandler(e:*):void
    {

        fileRef.load();
        fileRef.addEventListener(flash.events.Event.COMPLETE, onDataLoaded);

    }    

    private function onDataLoaded(e:*):void
    {           
        trace("data loaded");
    }

} //class