A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Loading External swf - driving me mad!

  1. #1
    Junior Member
    Join Date
    Jun 2007
    Posts
    19

    Angry Loading External swf - driving me mad!

    Hi guys,
    I really hope someone is able to help me with this problem. Its driving me crazy . Ok so here it goes. I have 4 files, InsideCastle.fla, InsideCastle.as, PointBurst.as, and IntrotogameTESTING.fla. Im trying to run IntrotogameTESTING.swf and when you click on a button I am trying to open InsideCastle.swf.

    The code in InsideCastle is as follows:

    //import fl.accessibility.ButtonAccImpl();
    //ButtonAccImpl.enableAccessibility();
    import flash.net.URLRequest;
    import flash.events.*;
    var externalSWF:URLRequest = new URLRequest("InsideCastle.swf");
    var myLoader:Loader = new Loader();
    Castle_btn.addEventListener(MouseEvent.CLICK, onButtonClick);
    myLoader.contentLoaderInfo.addEventListener(Progre ssEvent.PROGRESS, swfLoading);
    myLoader.contentLoaderInfo.addEventListener(Event. COMPLETE, swfComplete);
    function onButtonClick(event:MouseEvent):void
    {
    myLoader.load(externalSWF);
    }
    function swfComplete(event:Event):void
    {
    addChild(myLoader);
    //myLoader.x = //you can adjust the x postion here
    //myLoader.y = //adjust the y position here
    }
    function swfLoading(eventprogressEvent):void
    {
    //you can do something here while the swf is loading...
    }
    It throws the following error:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at InsideCastle/scrollWithHero()
    at InsideCastle/gameLoop()
    The code in scrollWithHero is:
    public function scrollWithHero() {
    scrollWithHero != null;
    //if (scrollWithHero == null) {
    // addEventListener(Event.ADDED_TO_STAGE, scrollWithHero);
    // }

    var stagePosition:Number = gamelevel.x+hero.mc.x;
    var rightEdge:Number = stage.stageWidth-edgeDistance;
    var leftEdge:Number = edgeDistance;
    if (stagePosition > rightEdge) {
    gamelevel.x -= (stagePosition-rightEdge);
    if (gamelevel.x < -(gamelevel.width-stage.stageWidth)) gamelevel.x = -(gamelevel.width-stage.stageWidth);
    }
    if (stagePosition < leftEdge) {
    gamelevel.x += (leftEdge-stagePosition);
    if (gamelevel.x > 0) gamelevel.x = 0;
    }
    }
    And the code in gameLoop is:



    public function gameLoop(event:Event) {
    gameLoop != null;

    // get time differentce
    if (lastTime == 0) lastTime = getTimer();
    var timeDiff:int = getTimer()-lastTime;
    lastTime += timeDiff;

    // only perform tasks if in play mode
    if (gameMode == "play") {
    moveCharacter(hero,timeDiff);
    moveEnemies(timeDiff);
    checkCollisions();
    scrollWithHero();
    }
    }
    You have no idea how much I need help! Im officially going mad. If you need more code to get an idea just gimme a shout!
    Thanks in advance!

  2. #2
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    I have no idea why are you using these 2 lines: gameLoop != null; and scrollWithHero != null; ; but that doesn't really matters. Imho, the problem is in var rightEdge:Number = stage.stageWidth-edgeDistance; as far as I'm concerned, you don't have direct access to the stage that way. You'll need to pass the stage as a parameter to the class that will be using it ( if I'm not mistaking ).



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    InsideCastle IS loading. If it wasn't, it couldn't throw an error like that.

    I'm going to assume that you tested InsideCastle on its own, and things worked. Right?

    In that case you've probably been bitten by the fact that stage is null until the object is actually on the stage. When your class is a document class, stage is set from the beginning. But when it's instantiated via something else, it is not.

    I'm going to further guess that there is a timer or enterframe listener that calls gameLoop which is instantiated in the InsideCastle constructor. Don't do that. You need to delay any code which needs the stage until the stage is set. You can do this by adding a listener for the ADDED_TO_STAGE event and doing stage related initialization in the handler for that.

  4. #4
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    Woops, Yes, sorry, I forgot about the Document Class. If you are using a Document Class then forget about the stage problems and try focusing on what 5Tons said, in case you are not using a Document Class then pass the stage and, again, focus on what 5Tons said.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  5. #5
    Junior Member
    Join Date
    Jun 2007
    Posts
    19
    Thanks for replying, I would just like to point out that Im pretty new to cs3.

    PlenaryCreation: Yeah I am using document class. And the lines scrollWitHero != null etc. was just a test. Must take them out, thanks for the heads up.


    5TonsOfFlax: InsideCastle is indeed working on its own.
    Also I have this code after I define all the variables and before all the functions:

    public function Main() {
    if (stage == null) {
    addEventListener(Event.ADDED_TO_STAGE, startGameLevel);
    } else {
    startGameLevel();
    }
    }


    Ive decided to just attach the InsideCastle.as

    I tried changing the timer by adding the ADDED_TO_STAGE thing but I think I did it wrong. Thanks for your help so far.
    Attached Files Attached Files

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Looking at that class, I notice a few things.
    1) You actually do know what you're doing, and this is probably not the same old problem answered here every day.
    2) There must be code on the timeline of InsideCastle which is not represented in the class. There is nothing in the class that would call Main, so I'm guessing the call is in the frame script. If the frame script calls gameloop independently, that may be your problem. I'd probably just put in a debug statement to find out which particular object is null, then trace back from there.

    3) This is weird, and completely useless:
    Code:
    		public function gameLoop(event:Event) {
    			gameLoop != null;
    ...

  7. #7
    Junior Member
    Join Date
    Jun 2007
    Posts
    19
    Yeah in my main timeline I have this code:

    stop();
    Main();
    startInsideCastle();

    Thanks for helping, will let you know if anything changes.

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