vrijdag 7 januari 2011

Weird loss of keyboard focus in game.. revisited

Yes!
I know what caused it.
It had to do with the scoping of the loader var.
You can keep a private var Loader in your main class and when you reload it goes like this:
import flash.display.Loader;
 import flash.events.Event;
 public class Container extends MovieClip
 {
  private var currentPage:Loader;//=new Loader();
  public function Container()
  {
   pageLayer=new MovieClip();
   stage.addChild(pageLayer);
   requestNewPage("splash");
  }
  private function requestNewPage(pageName:String):void 
  {
   var mc:MovieClip;
   var url=pagePath+pageName+".swf";
   trace("requestNewPage: "+url);
   progressMc.visible=true;
   currentPage=new Loader();
   currentPage.contentLoaderInfo.addEventListener(Event.COMPLETE, pageLoaded);
   currentPage.load(new URLRequest(url), new LoaderContext(true));
  }
   public function pageCallback(a=null,b=null,c=null,d=null)
  {
   //trace("pageCallback: "+a);
   switch(a)
   {
    case "requestNewPage":
     requestNewPage(b);
    break;
    case "startGame":
     requestNewPage("game");
    break;
    default:
     trace("pageCallback undefined action: "+a);
    break;
   }
  }
  private function pageLoaded(loadEvent:Event):void
  {
   var mc:MovieClip=MovieClip(currentPage.content);
   pageLayer.addChild(mc);
   mc.setUmbellical(stage,pageCallback);
  }
Now in the splash page, you create a public function called setUmbellical and receive the pageCallback.
If you need to communicate with the class that loaded you, call pageCallback..
In this setup calling it with arguments ("requestNewPage","filename") will cause the next page to be loaded, but WITHOUT the strange loss of focus!

Geen opmerkingen:

Een reactie posten