A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: variable URLRequest

  1. #1
    Junior Member
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    14

    variable URLRequest

    Hi,
    I am trying to get this code to work but I need some help. If you click topic1 I want topic1.swf to load, click topic2 to load topic2.swf etc. What I am doing wrong with var url:String = "topic"[i]+".swf";?
    Please I hope someone can help me out on this.

    function tweenTopicUp(topics):void {
    var myTween:TweenLite = new TweenLite(topics, 0.6, {alpha:0.5, ease:Expo.easeOut});
    }

    function tweenTopicDown(topics):void {
    var myTween:TweenLite = new TweenLite(topics, 0.6, {alpha:1, ease:Expo.easeOut});
    }

    // Create container for swf
    var ldr:Loader = new Loader();
    var url:String = "topic"[i]+".swf";
    //var url:String = "topic2.swf";
    var urlReq:URLRequest = new URLRequest(url);
    stage.addChild(ldr);
    ldr.x = 0;
    ldr.y = 0;

    var topics:Array = [topic1, topic2, topic3, topic4];

    for (var i:int = 0; i < topics.length; i++) {

    topics[i].addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
    topics[i].addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
    topics[i].addEventListener(MouseEvent.CLICK, onMouseClick);

    function onMouseOver(event:MouseEvent):void {
    tweenTopicUp(event.target);
    }
    function onMouseOut(event:MouseEvent):void {
    tweenTopicDown(event.target);
    }
    function onMouseClick(event:MouseEvent):void {
    trace(event.currentTarget.name);
    ldr.load(urlReq);
    }
    }

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    A) i doesn't exist where you're using it.
    B) the syntax is wrong anyway.

    Code:
    function tweenTopicUp(topics):void {
     var myTween:TweenLite = new TweenLite(topics, 0.6, {alpha:0.5, ease:Expo.easeOut});
    }
    
    function tweenTopicDown(topics):void {
     var myTween:TweenLite = new TweenLite(topics, 0.6, {alpha:1, ease:Expo.easeOut});
    }
    
    // Create container for swf
    var ldr:Loader = new Loader();
    stage.addChild(ldr);
    ldr.x = 0;
    ldr.y = 0;
    
    var topics:Array = [topic1, topic2, topic3, topic4];
    
    for (var i:int = 0; i < topics.length; i++) {
     topics[i].addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
     topics[i].addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
     topics[i].addEventListener(MouseEvent.CLICK, onMouseClick);
    }
    
    function onMouseOver(event:MouseEvent):void {
     tweenTopicUp(event.target);
    }
    function onMouseOut(event:MouseEvent):void {
     tweenTopicDown(event.target);
    }
    function onMouseClick(event:MouseEvent):void {
     trace(event.currentTarget.name);
     var urlReq:URLRequest = new URLRequest(event.currentTarget.name+".swf"); //assumes you named them accordingly
     ldr.load(urlReq);
    }

  3. #3
    Junior Member
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    14
    Hi 5tons,
    you are correct and I noticed this too. Meanwhile I was trying to put something like this together which was sort of close. But this generated an Error #1069 output...?

    function onMouseClick(event:MouseEvent):void {
    trace(event.currentTarget.name);
    ldr.load(urlReq[event.currentTarget.name + ".swf"]);
    }
    Thank you very much for your quick reply and solution! It's working excellent.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    1069 is access of an undefined property (I just looked it up). Which makes sense because the syntax you were using was trying to access a property of urlReq which didn't exist. I don't know why you would think that urlReq.topic1.swf would be anything other than nonsense.

  5. #5
    Junior Member
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    14
    I quess that is just my lack of as3 knowledge. Basically I am a designer who likes to learn and get some things done in Flash. Thanx for the explanation.

  6. #6
    Junior Member
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    14
    I have added an unload function which seems to work just fine but I get this error:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at topic2_fla::scrollMC_1/thumbUp()

    the external swf running on its own gives no error at all. What could be the problem here?

    import gs.*;
    import gs.easing.*;

    // Create container for swf
    var ldr:Loader = new Loader();
    stage.addChild(ldr);
    ldr.x = 0;
    ldr.y = 0;

    function tweenTopicUp(topics):void {
    var myTween:TweenLite = new TweenLite(topics, 0.6, {alpha:0.5, ease:Expo.easeOut});
    }

    function tweenTopicDown(topics):void {
    var myTween:TweenLite = new TweenLite(topics, 0.6, {alpha:1, ease:Expo.easeOut});
    }

    var topics:Array = [topic1, topic2, topic3, topic4];

    for (var i:int = 0; i < topics.length; i++) {
    topics[i].addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
    topics[i].addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
    topics[i].addEventListener(MouseEvent.CLICK, onMouseClick);
    }

    function onMouseOver(event:MouseEvent):void {
    tweenTopicUp(event.target);
    }
    function onMouseOut(event:MouseEvent):void {
    tweenTopicDown(event.target);
    }
    function onMouseClick(event:MouseEvent):void {
    trace(event.currentTarget.name);
    var urlReq:URLRequest = new URLRequest(event.currentTarget.name+".swf");
    ldr.load(urlReq);
    ldr.unload();
    }

    stop();

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Well, you didn't post the code the error is actually occurring in, so I don't know. The function with the error is thumbUp, as the error message says.

    It is very weird to have ldr.load immediately followed by ldr.unload. Why would you want to do that?

  8. #8
    Junior Member
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    14
    I put the unload function there based on an example from the flash help.
    How should it be done in this case? I am sorry I am struggling with as3 but I want to understand. I appreciate your help.

    And here is the code of the external swf.

    import gs.*;
    import gs.easing.*;

    contentMC.multiline = true;
    contentMC.wordWrap = true;
    contentMC.autoSize = TextFieldAutoSize.LEFT;

    var url:String = "txt/expertise.txt";

    var ldr:URLLoader = new URLLoader();
    ldr.addEventListener(Event.COMPLETE, completeHandler);
    ldr.load(new URLRequest(url));

    function completeHandler(event:Event):void {
    contentMC.htmlText = URLLoader(event.currentTarget).data;
    }

    var yOffset:Number;
    var yMin:Number = 0;
    var yMax:Number = sb.track.height - sb.thumb.height;

    sb.thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumbDown);
    stage.addEventListener(MouseEvent.MOUSE_UP, thumbUp);

    function thumbDown(e:MouseEvent):void{
    stage.addEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
    yOffset = mouseY - sb.thumb.y;
    }

    function thumbUp(e:MouseEvent):void{
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
    }

    function thumbMove(e:MouseEvent):void{
    sb.thumb.y = mouseY - yOffset;
    if (sb.thumb.y <= yMin)
    sb.thumb.y = yMin;
    if (sb.thumb.y >= yMax)
    sb.thumb.y = yMax;
    var sp:Number = sb.thumb.y / yMax;
    var myTween:TweenLite = new TweenLite(contentMC, 1, {y: (-sp * (contentMC.height - masker.height)), ease:Expo.easeOut});
    e.updateAfterEvent();
    }

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It looks like thumbUp is being called after the movieclip has been removed from the displayList, making the value of "stage" null. You'll need to remove the event listeners when the item is removed from the stage. Or you can just check that stage != null before referencing it in the handlers (that will leave extra eventlisteners around, and cause a slow memory leak).

    I do not see the example in the Loader asdoc about load immediately followed by unload. I still don't quite know what it's supposed to do.

  10. #10
    Junior Member
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    14
    The unload is meant to empty the container mc. Otherwise it loads just another swf over the previous one if you click another topic.

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Well, yes. But placing it immediately after the load should, according to the livedocs, cancel the load in progress. Meaning that nothing would ever completely load. It's possible the asynchronous nature of the load call means that it's not technically loading at the time that unload is called, but you're taking quite a gamble there. At the very least, move unload to before load.

  12. #12
    Junior Member
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    14
    ok, so how should I solve this in a proper way?

    I put a trace(stage != null); on the mouse click and this return true everytime.

  13. #13
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It returns true right before throwing the error? I would not expect that.

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