A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Passing variable from loaded swf to parent with mouse event

  1. #1
    Mourning Morning. groupof1's Avatar
    Join Date
    Feb 2008
    Location
    Middle of somewhere
    Posts
    194

    Passing variable from loaded swf to parent with mouse event

    This has been driving me nuts. The short of it is that I have a main movie that loads smaller swfs. In the main movie, there is a textbox that holds descriptions of the smaller clips.

    What I am trying to do is this:
    When the user clicks a button in the loaded swf, a string variable "feedback" replaces the text in the description text in the parent clip.

    Here is the code I have. I have been searching this for a while now and have tried various things, so this may be way off base. The clips load fine and everything, I just can't figure this one piece out.

    I am only showing the relevant code because there is a LOT of other working code. If anyone needs more info, let me know.

    Main Clip
    Code:
    clipLoader.contentLoaderInfo.addEventListener( Event.INIT, onLoaderInit );
    	
    	function onLoaderInit( e:Event ):void {
    		Object( loader.content ).init("descriptions"); 
    		//Passing the var to loaded swf
    		trace( Object(loader.content).feedback ); 
    		//calling a remote variable "feedback" from loaded swf
    	}
    Smaller Clips (all have similar code)
    Code:
    correct.addEventListener(MouseEvent.MOUSE_DOWN, correctAnswer);
    
    var feedback:String= descriptions; 
    //descriptions is the variable from the main clip 
    //this may not be necessary to assign here
    
    function correctAnswer(e:MouseEvent):void {
        feedback = answers[answers.length-1];
    	function init( param:String ):void {
    		trace( param );
    	}
    }
    I may be way off on this code so any help would be much appreciated.
    What the world needs now is a nice hot bath.
    _________________________________________________
    Battles: Silverx2

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You described what you want, and you told us that it isn't doing that, but you did not describe what it is doing.

    Here's some problems:
    1. You define init inside correctAnswer. It is undefined outside that function. Move it out.

    2. You have no way of notifying your loading clip that an answer has been selected. You should dispatch an event for this. If you create your own event class, that event can hold whatever data you want it to, including the feedback. If you don't, then just access the event.target where you handle it, and get the feedback from that.

    3. descriptions is undefined at the time you attempt to assign it. I do not understand where you thing you are passing anything into the loaded swf. If you did fix init, you could get the parameter you pass, but where are you planning on setting feedback, and to what?

  3. #3
    Mourning Morning. groupof1's Avatar
    Join Date
    Feb 2008
    Location
    Middle of somewhere
    Posts
    194
    Let me see if I can answer these one at a time.

    Quote Originally Posted by 5TonsOfFlax View Post
    You described what you want, and you told us that it isn't doing that, but you did not describe what it is doing.

    Here's some problems:
    1. You define init inside correctAnswer. It is undefined outside that function. Move it out.
    I moved init out of correctAnswer. Here is the entire code for the loaded swf.
    Also, this is in a clip on the stage of the loaded swf. I for got to mention that.
    Code:
    stop();
    
    correct.addEventListener(MouseEvent.MOUSE_DOWN, correctAnswer);
    incorrect1.addEventListener(MouseEvent.MOUSE_DOWN, incorrectAnswer);
    incorrect2.addEventListener(MouseEvent.MOUSE_DOWN, incorrectAnswer);
    incorrect3.addEventListener(MouseEvent.MOUSE_DOWN, incorrectAnswer);
    
    var feedback:String;
    var i:uint = 0;
     
    function incorrectAnswer(e:MouseEvent):void {
    	if (i < 2 ) {
        	feedback = answers[i];
    		feedbackText.text = (feedback);
    		init (feedback);
    		i++;
    	} else {
        	feedback = answers[i];
    		feedbackText.text = (feedback);
    		init (feedback);
    		i=0;
    	}
    }
    
    function correctAnswer(e:MouseEvent):void {
        feedback = answers[answers.length-1];
    	//This sets the feedback to a text box inside the clip
            feedbackText.text = (feedback); 
    	init (feedback); 
    	i=0;
    }
    
    function init( param:String ):void {
    		trace( param );
    	}
    
    var answers:Array = [
       "Incorrect. Please try again",
       "Remember that the control knob powers the unit. Try Again",
       "That is incorrect. Turning the control knob clockwise powers up the unit.",
       "Correct. Turning the control knob clockwise powers up the unit."
       ];

    2. You have no way of notifying your loading clip that an answer has been selected. You should dispatch an event for this. ...create your own event class... If you don't, then just access the event.target where you handle it, and get the feedback from that.
    I have another issue using Classes and would like to avoid it if possible in this case. It has to do with the file structure of the LMS I have to load this into and I have no way around it. I did try to add an eventListener in the main movie. See below.

    3. descriptions is undefined at the time you attempt to assign it. I do not understand where you thing you are passing anything into the loaded swf. If you did fix init, you could get the parameter you pass, but where are you planning on setting feedback, and to what?
    I don't need to pass anything into the swf. I cut descriptions out. I want to send "feedback" to the loading swf. There is a text field on the stage called descriptionText. Here is the code I have at this point and the error I am getting.

    Code:
    	clipLoader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
    	
    	function onLoaderInit( e:Event ):void {
    	  e.target.content.addEventListener(MouseEvent.CLICK,onClickListener);		
    		// trying it without these lines
    		//Object( loader.content ).init("descriptions"); 
    		//Passing the var to loaded swf
    		//trace( Object(loader.content).feedback ); 
    		//calling a remote variable "feedback" from loaded swf
    	}
    
    	function onClickListener(e:MouseEvent):void {
    //line 212 --> descriptionText.text = (e.currentTarget.feedback);
    	}
    The error I get when I click the buttons in the loaded clip is as follows.

    TypeError: Error #2007: Parameter text must be non-null.
    at flash.text::TextField/set text()
    at XTS_fla:MainTimeline/playClip/XTS_flanClickListener()[XTS_fla.MainTimeline::frame1:212]

    I hope this helps. I know enough Flash and AS3 to get into binds like this that are above my current skill level.
    What the world needs now is a nice hot bath.
    _________________________________________________
    Battles: Silverx2

  4. #4
    Mourning Morning. groupof1's Avatar
    Join Date
    Feb 2008
    Location
    Middle of somewhere
    Posts
    194
    Also, you specifically Flax have helped me out in the past. So I do appreciate your help. Thanks.
    What the world needs now is a nice hot bath.
    _________________________________________________
    Battles: Silverx2

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You said the posted code in the loaded swf is in a clip which is inside the loaded swf rather than in the main timeline of the loaded swf? If that's the case, then the content does not have a feedback property, since you've defined feedback on a different object. The cleanest way to handle this is still with events and bubbling. In the loaded:

    Code:
    stop();
    
    correct.addEventListener(MouseEvent.MOUSE_DOWN, correctAnswer);
    incorrect1.addEventListener(MouseEvent.MOUSE_DOWN, incorrectAnswer);
    incorrect2.addEventListener(MouseEvent.MOUSE_DOWN, incorrectAnswer);
    incorrect3.addEventListener(MouseEvent.MOUSE_DOWN, incorrectAnswer);
    
    var feedback:String;
    var i:uint = 0;
    
    function setFeedback(fback:String):void{
      feedback = fback;
      feedbackText.text = (fback);
      dispatchEvent(new Event("ANSWER_SELECTED", true);
    }
     
    function incorrectAnswer(e:MouseEvent):void {
      if (i < 2 ) {
        setFeedback(answers[i]);
        //init (feedback);
        i++;
      } else {
        setFeedback(answers[i]);
        init(feedback);
        i=0;
      }
    }
    
    function correctAnswer(e:MouseEvent):void {
      setFeedback(answers[answers.length-1]);
      init (feedback); 
      i=0;
    }
    
    function init( param:String ):void {
      trace( param );
    }
    
    var answers:Array = [
       "Incorrect. Please try again",
       "Remember that the control knob powers the unit. Try Again",
       "That is incorrect. Turning the control knob clockwise powers up the unit.",
       "Correct. Turning the control knob clockwise powers up the unit."
       ];
    In the loading:
    Code:
    addEventListener("ANSWER_SELECTED", handleAnswer);
    
    function handleAnswer(e:Event):void{
      var something:MovieClip = MovieClip(e.target);
      descriptionText.text = (something.feedback);
    }
    Last edited by 5TonsOfFlax; 04-27-2011 at 03:58 PM. Reason: whoops, mixed my solutions.

  6. #6
    Mourning Morning. groupof1's Avatar
    Join Date
    Feb 2008
    Location
    Middle of somewhere
    Posts
    194

    resolved

    Quote Originally Posted by 5TonsOfFlax View Post
    You said the posted code in the loaded swf is in a clip which is inside the loaded swf rather than in the main timeline of the loaded swf? If that's the case, then the content does not have a feedback property, since you've defined feedback on a different object. The cleanest way to handle this is still with events and bubbling.

    In the loaded:
    Code:
    stop();
    
    correct.addEventListener(MouseEvent.MOUSE_DOWN, correctAnswer);
    incorrect1.addEventListener(MouseEvent.MOUSE_DOWN, incorrectAnswer);
    incorrect2.addEventListener(MouseEvent.MOUSE_DOWN, incorrectAnswer);
    incorrect3.addEventListener(MouseEvent.MOUSE_DOWN, incorrectAnswer);
    
    var feedback:String;
    var i:uint = 0;
    
    function setFeedback(fback:String):void{
      feedback = fback;
      dispatchEvent(new Event("ANSWER_SELECTED", true));
    }
     
    function incorrectAnswer(e:MouseEvent):void {
      if (i < 2 ) {
        setFeedback(answers[i]);
        init (feedback);
        i++;
      } else {
        setFeedback(answers[i]);
        init(feedback);
        i=0;
      }
    }
    
    function correctAnswer(e:MouseEvent):void {
      setFeedback(answers[answers.length-1]);
      init (feedback); 
      i=0;
    }
    
    function init( param:String ):void {
      trace( param );
    }
    
    var answers:Array = [
       "Incorrect. Please try again",
       "Remember that the control knob powers the unit. Try Again",
       "That is incorrect. Turning the control knob clockwise powers up the unit.",
       "Correct. Turning the control knob clockwise powers up the unit."
       ];
    In the loading:
    Code:
    addEventListener("ANSWER_SELECTED", handleAnswer);
    
    function handleAnswer(e:Event):void{
      var something:MovieClip = MovieClip(e.target);
      descriptionText.text = (something.feedback);
    }
    That did it. Awesome. See, I didn't even know to look for Event Bubbling. If I had known what it was called, I probably could have saved myself some headache and you some time. Thank you once again. Flax =
    What the world needs now is a nice hot bath.
    _________________________________________________
    Battles: Silverx2

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