A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: html event function problem

  1. #1
    Member
    Join Date
    Feb 2007
    Posts
    53

    html event function problem

    Hi there, over the past couple of days ive been building a model of something in hopes of recreating it in my actual project, and have had the good fortune to recieve excellent answers from some of you guys which solved all my models problems, yet although ive learned a bit about it, i cant seem to implement them in my actual project, as the models were a bit simpler than the project and i erringly thought it wouldnt matter, but it does.. as3 has turned me from an intermediate writer of actionscript to a nitwit writer of actionscript.. so ive decided to abandon the models, Id really appreciate some help. Im trying to pass internal as3 commands through an xml-driven scroller widget which gets loaded into a 5th level holderMC (5th MC down from root) to an MC on the 3rd level down from root (2 MC's above holderMC) as event functions from htmltext.. basically im trying to get an external movieclip @ root.activezoom.menu2.activeclean.holderMC to talk to a clip in root.activezoom.menu2.vib... im at wits end..

    im loading the scrollbar widget into the 5th level (into holderMC) from the 4th like so:


    var swfLoader:Loader = new Loader();
    holderMC.addChild(swfLoader);
    var bgURL:URLRequest=new URLRequest("scroller.swf");
    swfLoader.contentLoaderInfo.addEventListener(Event .COMPLETE, loadProdComplete);
    swfLoader.load(bgURL);
    function loadProdComplete(e:Event):void {
    trace("file loaded");
    }

    in my xmlfile, there are multiple event functions in htmltext passed to the scroller

    <description><![CDATA[<a href='event:specialFunction'>click for black box</a><br></br><a href='event:redbox'>click for redbox</a><br></br><a href='event:yellowbox'>click for yellowbox</a>]]></description>


    the scrollers textfield has this listener attached to it:

    descriptionText.addEventListener(TextEvent.EVENT, linkHandler);


    which referrs to this function, also in the scroller.swf

    function linkHandler(linkEvent:TextEvent):void {
    switch (linkEvent.text) {
    case "specialFunction":
    magicalFunction();
    break;
    case "redbox":
    startredbox();
    break;
    case "yellowbox":
    startyellowbox();
    break;
    default:
    trace(linkEvent.text);
    }
    }


    which referr to these secondary functions, also in the scroller.swf:

    function magicalFunction(event:TextEvent):void {
    trace(event.currentTarget.stage.getChildAt(3));
    event.currentTarget.stage.getChildAt(3).clip.gotoA ndPlay(2);
    }

    function startredbox(event:TextEvent):void {
    trace(event.currentTarget.stage.getChildAt(3));
    event.currentTarget.stage.getChildAt(3).redbox.got oAndPlay(2);
    }

    function startyellowbox(event:TextEvent):void {
    trace(event.currentTarget.stage.getChildAt(3));
    event.currentTarget.stage.getChildAt(3).yellowbox. gotoAndPlay(2);
    }

    when i publish the scroller.swf i get error 1136..which translates into wrong amount of arguments. 1 neccesary. The problem is referring to the functionnames before the breaks in the linkhandler function..

    your help would be hugely appreciated!

    edit: in this post, the secondary functions appear to have a syntax error in their gotoAndPlay commands.. these dont appear in the actual code.. (strange i cant get rid of em if i retype it here either)
    Last edited by jan33; 02-08-2010 at 09:40 AM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Use [code] and [/code] to format your code on the forums.

    I answered this in the other thread. Your functions demand an event argument, but you are calling them without that argument. You can either remove that argument in the function, pass it on from the calling function, or give it a default null value.

    remove it from the function:
    Code:
    function magicalFunction():void{
    Pass it on from the calling function:
    Code:
    magicalFunction(linkEvent);
    give it a default null value:
    Code:
    function magicalFunction(event:TextEvent = null):void{
    Any one of these approaches should fix that error.

  3. #3
    Member
    Join Date
    Feb 2007
    Posts
    53
    thanks alot for your replies, it was like 4am when i gave up last night and didnt have the brainpower to formulate the problem anymore.. but in the meantime i've tried delete the argument like so

    Code:
    function startyellowbox():void {
    	trace(event.currentTarget.stage.getChildAt(3));
    	event.currentTarget.stage.getChildAt(3).flap_vib2.gotoAndPlay(2);
    }
    but got this compiler error 6 times

    1120: Zugriff auf eine nicht definierte Eigenschaft event.
    (1120: accessing an undefined property, event.. (referring to the third line of the function)

    then tried
    Code:
    function startyellowbox(event:TextEvent = null):void {
    	trace(event.currentTarget.stage.getChildAt(3));
    	event.currentTarget.stage.getChildAt(3).flap_vib2.gotoAndPlay(2);
    }
    but that gave me this traced error, the compiler errorpallette didnt say peep tho

    TypeError: Error #2007: Parameter type can not be 'null'.
    at flash.events::EventDispatcher/addEventListener()
    at scroller_fla::indexMc_1/ChangeNews()
    at scroller_fla::indexMc_1/MainFunc()
    at scroller_fla::indexMc_1/XMLLoadComplete()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()



    then i tried

    Code:
    function linkHandler(linkEvent:TextEvent):void {
    	switch (linkEvent.text) {
    		case "specialFunction":
    		     magicalFunction(linkEvent);
    			break;
    		case "redbox":
    			 startredbox(linkEvent);
    			break;
    		case "yellowbox":
    			startyellowbox(linkEvent);
    			break;
    		default:
    			trace(linkEvent.text);
    	}	
    }
    and this time also no compiler errors, but the trace window said

    TypeError: Error #2007: Parameter type darf nicht 'null' sein.
    at flash.events::EventDispatcher/addEventListener()
    at download3_fla::indexMc_1/ChangeNews()
    at download3_fla::indexMc_1/MainFunc()
    at download3_fla::indexMc_1/XMLLoadComplete()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

    im too much of a noob to read between these lines... no clue where the problem is..

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'm sorry, I didn't read your code closely enough. In the other thread, your functions did not use the event, so giving it a default null value was valid. These functions do use the event, so a default null value is not appropriate. Neither is removing the argument. So you must pass the event on, as you did in the last attempt above.

    The error you are getting is not about that code. It is about the code where you are adding an event listener within ChangeNews(). That code does not seem to be posted here.

  5. #5
    Member
    Join Date
    Feb 2007
    Posts
    53
    oh, i see, okay... uh-oh.. changeNews is part of the scroller widget i bought ..this widget is pretty complicated to me, i did change something in there now that you mention it. changenews looks like this.. this is actually where also where the descriptionText textbox gets its linkHandler listener..
    Code:
    descriptionText.addEventListener(TextEvent.EVENT, linkHandler);
    Code:
    function ChangeNews() {
    	/*var closeBtnRidius = Number(xml.@closeBtnRidius);
    	var closeBtnCrossColor = uint(String(xml.@closeBtnCrossColor).replace(/#/, '0x'));
    	var closeBtnBorderColor = uint(String(xml.@closeBtnBorderColor).replace(/#/, '0x'));
    	var closeBtnBgColor = uint(String(xml.@closeBtnBgColor).replace(/#/, '0x'));*/
    
    	var btnBgMcShadow:DropShadowFilter = new DropShadowFilter();
    	btnBgMcShadow.distance=0;
    	btnBgMcShadow.angle=120;
    	btnBgMcShadow.color=uint(String(xml.@globalShadowColor).replace(/#/,'0x'));
    	btnBgMcShadow.alpha=1;
    	btnBgMcShadow.blurX=10;
    	btnBgMcShadow.blurY=10;
    
    	newsDetailMc.alpha=0;
    	with (newsDetailMc) {
    		titleText.text=String(xml.title);
    		titleText.setTextFormat(titleTextFormat);
    		titleText.embedFonts=true;
    		titleText.antiAliasType=AntiAliasType.ADVANCED;
    		titleText.wordWrap=true;
    		titleText.selectable=false;
    		titleText.autoSize=TextFieldAutoSize.LEFT;
    		titleText.mouseWheelEnabled=false;
    		if (String(xml.@titleColor)!='') {
    			titleText.textColor=uint(String(xml.@titleColor).replace(/#/,'0x'));
    		}
    
    		titleText.x=0;
    		titleText.y=titleText.textHeight-titleText.height;
    		titleText.width=widgetWidth-2*widgetLeftPadding-3;//* closeBtnRidius;
    
    		//titleText.height = newsSingleMc.titleText.textHeight;
    		//titleText.border = true;
    
    		//close button mc
    		/*with (closeBtnMc.graphics)
    		{
    		clear();
    		lineStyle(1, closeBtnBorderColor);
    		beginFill(closeBtnBgColor);
    		drawCircle(0, 0, closeBtnRidius);
    		endFill();
    		}
    		
    		closeBtnMc.filters = [btnBgMcShadow];
    		closeBtnMc.x = widgetWidth - widgetLeftPadding - 2* closeBtnRidius;
    		closeBtnMc.y = 2* closeBtnRidius - widgetTopPadding;
    		
    		var crossPng = new crossPngDemo();
    		Tweener.addTween(crossPng, {_color:closeBtnCrossColor, time:0.1 });
    		//crossPng.x =  - crossPng.width/2;
    		//crossPng.y =  - crossPng.height/2;
    		closeBtnMc.addChild(crossPng);
    		
    		closeBtnMc.useHandCursor = true;
    		closeBtnMc.buttonMode = true;
    		closeBtnMc.addEventListener(MouseEvent.ROLL_OVER, CloseBtnMcOver);
    		closeBtnMc.addEventListener(MouseEvent.ROLL_OUT, CloseBtnMcOut);
    		closeBtnMc.addEventListener(MouseEvent.CLICK, CloseBtnMcClick);
    		*/
    		dateText.text=String(xml.category);
    		dateText.embedFonts=true;
    		dateText.antiAliasType=AntiAliasType.ADVANCED;
    		dateText.wordWrap=true;
    		dateText.selectable=false;
    		dateText.autoSize=TextFieldAutoSize.LEFT;
    		dateText.mouseWheelEnabled=false;
    		if (String(xml.@categoryColor)!='') {
    			dateText.textColor=uint(String(xml.@categoryColor).replace(/#/,'0x'));
    		}
    
    		//dateText.setTextFormat(dateTextFormat);
    		dateText.x=newsDetailMc.titleText.x;
    		dateText.y=newsDetailMc.titleText.y+newsDetailMc.titleText.height-5;
    		dateText.width=widgetWidth-2*widgetLeftPadding;
    		//dateText.height = newsSingleMc.dateText.textHeight;
    		//dateText.border = true;
    
    		descriptionText.htmlText=String(xml.description);
    		descriptionText.embedFonts=true;
    		descriptionText.antiAliasType=AntiAliasType.ADVANCED;
    		descriptionText.wordWrap=true;
    		descriptionText.selectable=false;
    		descriptionText.autoSize=TextFieldAutoSize.LEFT;
    		descriptionText.mouseWheelEnabled=false;
    		
    		
    		descriptionText.addEventListener(TextEvent.EVENT, linkHandler);
    		if (String(xml.@descriptionColor)!='') {
    			descriptionText.textColor=uint(String(xml.@descriptionColor).replace(/#/,'0x'));
    		}
    
    
    		descriptionText.x=newsDetailMc.titleText.x;
    		descriptionText.y=newsDetailMc.dateText.y+newsDetailMc.dateText.height;
    		descriptionText.width=widgetWidth-2*widgetLeftPadding-scrollBtnWidth-10;
    		//descriptionText.height = newsDetailMc.descriptionText.textHeight;
    		//descriptionText.border = true;
    		//widgetHeight - newsDetailMc.descriptionText.y - widgetTopPadding
    		
    		
    	}
    
    	//draw scroll bar bg
    	scrollBarHeight=widgetHeight-newsDetailMc.titleText.height-newsDetailMc.dateText.height-2*widgetTopPadding+5;
    	scrollBarMc.graphics.clear();
    	scrollBarMc.graphics.beginFill(scrollBarColor);
    	scrollBarMc.graphics.drawRoundRect(0,0,scrollBarWidth,scrollBarHeight,0,0);
    	scrollBarMc.graphics.endFill();
    
    
    
    	//align scroll button and bar
    	scrollBtnMc.x=Math.round(widgetWidth-scrollBtnWidth-2*widgetLeftPadding);
    	scrollBarMc.x = (scrollBtnMc.width - scrollBarMc.width)/2 + scrollBtnMc.x;
    	scrollBarMc.y=newsDetailMc.descriptionText.y;
    	scrollBtnMc.y=scrollBarMc.y;
    
    	descriptionMaskMc.x=0;
    	descriptionMaskMc.y=scrollBarMc.y;
    
    	//add event listener for scroll button and scroll bar.
    	if (newsDetailMc.descriptionText.height>scrollBarMc.height) {
    		newsDetailMc.descriptionText.width=widgetWidth-2*widgetLeftPadding-scrollBtnWidth-10;
    		scrollBarMc.visible=true;
    		scrollBtnMc.visible=true;
    
    		newsDetailMc.addEventListener(Event.ENTER_FRAME, MoveNews);
    		newsDetailMc.addEventListener(MouseEvent.MOUSE_WHEEL, MouseWheel);
    
    		scrollBtnMc.addEventListener(MouseEvent.MOUSE_DOWN, StartDragBtn);
    		scrollBtnMc.addEventListener(MouseEvent.ROLL_OVER, BtnOver);
    		scrollBtnMc.addEventListener(MouseEvent.ROLL_OUT, BtnOut);
    	} else {
    
    		newsDetailMc.descriptionText.width=widgetWidth-2*widgetLeftPadding;
    		scrollBarMc.visible=false;
    		scrollBtnMc.visible=false;
    
    		newsDetailMc.removeEventListener(Event.ENTER_FRAME, MoveNews);
    		newsDetailMc.removeEventListener(MouseEvent.MOUSE_WHEEL, MouseWheel);
    
    		scrollBtnMc.removeEventListener(MouseEvent.MOUSE_DOWN, StartDragBtn);
    		scrollBtnMc.removeEventListener(MouseEvent.ROLL_OVER, BtnOver);
    		scrollBtnMc.removeEventListener(MouseEvent.ROLL_OUT, BtnOut);
    	}
    
    	//draw main mask for news
    	descriptionMaskMc.graphics.clear();
    	descriptionMaskMc.graphics.beginFill(0xffffff);
    	descriptionMaskMc.graphics.drawRoundRect(0,0,newsDetailMc.descriptionText.width + 2,scrollBarHeight + 3,0,0);
    	descriptionMaskMc.graphics.endFill();
    	descriptionMaskMc.alpha=0.5;
    	newsDetailMc.descriptionText.mask=descriptionMaskMc;
    
    	Tweener.addTween(newsDetailMc, {alpha: 1, time: .5, transition:"easeInSine"});
    
    }
    Last edited by jan33; 02-08-2010 at 12:12 PM.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It is probably that descriptionText line.

    TextEvent does not define an EVENT property. I would have expected a compile error from that. Try using TextEvent.LINK.

    Code:
    descriptionText.addEventListener(TextEvent.LINK, linkHandler);

  7. #7
    Member
    Join Date
    Feb 2007
    Posts
    53
    that did it, youre awesome, that published scroller.swf 100% clean and error-free..

    though now, when i load the external scroller.swf into my mainfile's holderMC, the tracer window reports this

    Code:
    file loaded
    RangeError: Error #2006: Der angegebene Index liegt außerhalb des zulässigen Bereichs.
    
    	at flash.display::DisplayObjectContainer/getChildAt()
    	at download3_fla::indexMc_1/magicalFunction()
    	at download3_fla::indexMc_1/linkHandler()
    (translates to RangeError: Error #2006: The Index provided lies outside of the permitted area)

    my guess is that

    Code:
    function startyellowbox(event:TextEvent):void {
    	trace(event.currentTarget.stage.getChildAt(3));
    	event.currentTarget.stage.getChildAt(3).flap_vib2.gotoAndPlay(2);
    }
    is wishfull thinking.. specifically i dont know exactly if getChildAt(3) is in any way accurate, nor what exactly the two lines of the code that are the actual function indeed do.. feels like a targeting problem..

    what is

    Code:
    	trace(event.currentTarget.stage.getChildAt(3));
    	event.currentTarget.stage.getChildAt(3).flap_vib2.gotoAndPlay(2);
    even telling it to do?

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes, that line looks suspicious. Normally, the root is the only child of the stage.

    Does the instance you are trying to access (at index 3) have a name? If so, try something like this:
    Code:
    MovieClip(MovieClip(root).getChildByName("whateverName")).flap_vib2.gotoAndPlay(2);
    This style of "targeting" is bad because the child clips need to know about and control the structure of the ancestor clips. Instead, you could dispatch an event that the relevant parent would listen for and react to. Make sure the event has bubbling set to true.

    Another way to go about it would be to pass a reference to the particular clip that you actually want to manipulate down to the child which needs to manipulate it. I would still create a method in that clip to handle the implementation so that the child doesn't have to use gotoAndPlay.

    Let's say you set a variable called yellowbox in scroller.

    scroller:
    Code:
    var yellowBox:MovieClip;
    And when scroller is loaded, you set yellowbox to whatever instance it actually is:
    Code:
    function loadProdComplete(e:Event):void {
      trace("file loaded");
      var content:MovieClip = MovieClip(swfLoader.content);
      content.yellowBox = flap_vib2;  //adjust this to point to the right item.
    }
    flap_vib2 should define a function to implement the activation. In flap_vib2:
    Code:
    function activate():void{
      gotoAndPlay(2);
    }
    And then startyellowbox could use that instead:
    Code:
    function startyellowbox(event:TextEvent):void {
      yellowBox.activate();
    }
    All this would be a lot cleaner if each of these clips were actually an instance of a class that you define, but it should work with frame script too.

  9. #9
    Member
    Join Date
    Feb 2007
    Posts
    53
    ..does it have a name... yes. hmm.. little difficult to explain..
    ill try it this way.. im trying to get magicalFunction, defined in the external scroller.swf, to make a filmclip labeled flap_vib2, which is buried a few clips deep in my my mainmovie, gotoAndPlay(2);

    in AS2 terminiology i would define the path like
    Code:
    _root.activezoom.menu2.flap_vib2.gotoAndPlay(2);
    (keeping in mind that scroller.swf itself is being loaded into _root.activezoom.menu2.flap_active.holderMC)


    (its a menu, the flaps are ..well.. flaps with text & pics on them.. that come rushing out when theres mouseovers over the menuitems.. i want to activate one flap from inside another flap, since the menutexts and all text on the page is xml driven, i need to get the eventfunction to get passed from the htmltext inside the xml file, making the whole thing pretty complicated to me to begin with) (yellowbox and redbox and all that have no real meaning, just placebos for the actual paths.. i was trying to keep it simple but see ive failed utterly by doing so)
    Last edited by jan33; 02-08-2010 at 01:21 PM.

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I really would suggest only dispatching an event within startyellowbox (which means you could actually just dispatch that event within linkHandler). The root should listen for that event, and trigger any necessary action.

    Code:
    function linkHandler(linkEvent:TextEvent):void {
      dispatchEvent(new Event(linkEvent.text, true));
    }
    in main timeline:
    Code:
    addEventListener("yellowbox", startYellowBox);
    //add events for other boxes too
    
    function startYellowBox(e:Event = null):void{
      activezoom.menu2.flap_vib2.gotoAndPlay(2);
      //possibly abstract the implementation details out further from the logical operation, but it's not strictly necessary.
    }

  11. #11
    Member
    Join Date
    Feb 2007
    Posts
    53
    again.. youre awesome.
    Worked fine. Ill never use parent.parent again..this is the solution to all my as3 targeting problems, methinks.. thank you truckloads!

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