A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: SWF loaded into container keyboard events not working

  1. #1
    Member
    Join Date
    Dec 2004
    Posts
    68

    SWF loaded into container keyboard events not working

    I am loading game levels swf's into a container swf.

    The game level swf doesn't register the keyboard events. The EnterFrame and MouseEvents work with the preface "this.addEventListener(MouseEvnet.CLICK, doSomething)", but not the KeyBoard Events.


    Container code:
    PHP Code:
    var levelOneMC:MovieClip
    var myloader:Loader= new Loader();
    myloader.load(new URLRequest("levelOne.swf"));


    myloader.contentLoaderInfo.addEventListener(Event.INITloadLevelOne);
    function 
    loadLevelOne(evt:Event){
        
    levelOneMC myloader.content as MovieClip;
        
    addChild(levelOneMC);

    levelOne code:
    PHP Code:
    //Works with just levelOne, doesn’t work in container
    //this.stage.addEventListener(KeyboardEvent.KEY_DOWN,test);

    //Works with just levelOne, doesn’t work in container
    //stage.addEventListener(KeyboardEvent.KEY_DOWN,test);

    //doesn’t work in either levelOne or in container
    //this.addEventListener(KeyboardEvent.KEY_DOWN,test);



    function test(e:KeyboardEvent){
        
    trace("this is working");

    Can any one help?

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Try
    PHP Code:
    var levelOneMC:MovieClip;
    var 
    myloader:Loader= new Loader();
    myloader.load(new URLRequest("levelOne.swf"));

    myloader.contentLoaderInfo.addEventListener(Event.INITloadLevelOne);
    function 
    loadLevelOne(evt:Event)
    {
        
    levelOneMC myloader.content as MovieClip;
        
    addChild(levelOneMC);
        
    stage.addEventListener(KeyboardEvent.KEY_DOWN,levelOneMC.test);


  3. #3
    Member
    Join Date
    Dec 2004
    Posts
    68
    Ta DA! Works. I had moment of panic when it didn't work the first time, but it was the stage focus.

    I take it keyboard events will not work in a loaded SWF. Eh, whatever works.

    Thanks fruitbeard.

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    HI,

    You might be better doing it this way if you want it to initaiate either inside of the container or outside of container.
    Container code:
    PHP Code:
    var levelOneMC:MovieClip;
    var 
    myloader:Loader= new Loader();
    myloader.load(new URLRequest("levelOne.swf"));

    myloader.contentLoaderInfo.addEventListener(Event.INITloadLevelOne);
    function 
    loadLevelOne(evt:Event)
    {
        
    levelOneMC myloader.content as MovieClip;
        
    addChild(levelOneMC);

    levelOne code:
    PHP Code:
    if (stage)
    {
        
    initiate();
    }
    else
    {
        
    addEventListener(Event.ADDED_TO_STAGEinitiate);
    }

    function 
    initiate(e:Event null):void
    {
        
    trace("Now loaded and ready for stage.");
        
    removeEventListener(Event.ADDED_TO_STAGE,initiate);
        
    stage.addEventListener(KeyboardEvent.KEY_DOWN,test);
    }

    function 
    test(e:KeyboardEvent):void
    {
        
    trace("this is working");


  5. #5
    Member
    Join Date
    Dec 2004
    Posts
    68
    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center