Please use the [ code ] tags to format code to preserve indentation and readability. Here, I've done it for you this time.
Original code, re-formatted for quasi-readability:
No offense intended, but that code hurts my brain. You have functions nested 3 deep for no particular reason. It looks like you're not familiar with classes and scoping.Code:package com.site.microsite{ import flash.display.*; import flash.events.*; public class WinathonMC extends MovieClip { public function WinathonMC() { var winNav:WinNav = new WinNav(); addChild(winNav); winNav.x = stage.stageWidth/2; winNav.y = 575; winNav.nav3spotlight0.rotation = 50; winNav.nav3spotlight1.rotation = 120; winNav.nav3spotlight2.rotation = 60; winNav.nav3spotlight3.rotation = 140; //spotlight variables var spotlightCW0:Boolean = true; var spotlightCW1:Boolean = true; var spotlightCW2:Boolean = true; var spotlightCW3:Boolean = true; var spotlight00rot:Number = winNav.nav3spotlight0.rotation; var spotlight01rot:Number = winNav.nav3spotlight1.rotation; var spotlight02rot:Number = winNav.nav3spotlight2.rotation; var spotlight03rot:Number = winNav.nav3spotlight3.rotation; //array that holds bulb instances and sets the index at 0 //var bulbclips:Array = [winNavigation.bulb0, winNavigation.bulb1, winNavigation.bulb2, winNavigation.bulb3]; //var index:int = 0; var bulbclips:Array = new Array(); for (var i:uint; i < 58; i++) { bulbclips.push("winNav.bulb"+i); var index:int = 0; trace("winNav.bulb"+i); } winNav.addEventListener(MouseEvent.MOUSE_OVER,onNavOver); winNav.addEventListener(MouseEvent.MOUSE_OUT,onNavOut); function onNavOver(evt:MouseEvent) { addEventListener(Event.ENTER_FRAME,onEnterFrame); function onEnterFrame() { // rotation of spotlight 1 on mouse event if (spotlight00rot > 70) { spotlightCW0 = false; } if (spotlight00rot < 30) { spotlightCW0 = true; } if (spotlightCW0 == true) { winNav.nav3spotlight0.rotation = (spotlight00rot += .4); } else { winNav.nav3spotlight0.rotation = (spotlight00rot -= .4); } // rotation of spotlight 2 on mouse event if (spotlight01rot > 120) { spotlightCW1 = false; } if (spotlight01rot < 70) { spotlightCW1 = true; } if (spotlightCW1 == true) { winNav.nav3spotlight1.rotation = (spotlight01rot += .6); } else { winNav.nav3spotlight1.rotation = (spotlight01rot -= .6); } // rotation of spotlight 3 on mouse event if (spotlight02rot > 120) { spotlightCW2 = false; } if (spotlight02rot < 50) { spotlightCW2 = true; } if (spotlightCW2 == true) { winNav.nav3spotlight2.rotation = (spotlight02rot += .6); } else { winNav.nav3spotlight2.rotation = (spotlight02rot -= .6); } // rotation of spotlight 4 on mouse event if (spotlight03rot > 160) { spotlightCW3 = false; } if (spotlight03rot < 90) { spotlightCW3 = true; } if (spotlightCW3 == true) { winNav.nav3spotlight3.rotation = (spotlight03rot += .4); } else { winNav.nav3spotlight3.rotation = (spotlight03rot -= .4); } //winNavigation.bulb20.play() bulbclips[index].play(); //advance bulb clip order //function advance():void { //(index < bulbclips.length-1) ? index++ : index=0; //advance the index to play next bulb //bulbclips[index].play(); //play the clip //} // ////start the clips playing //bulbclips[index].play(); } } function onNavOut(evt:MouseEvent) { addEventListener(Event.ENTER_FRAME,onEnterFrame); function onEnterFrame() { //rotation stopped for spotlight 1 on mouse out if (spotlightCW0 == true) { winNav.nav3spotlight0.rotation = (spotlight00rot -= .4); } else { winNav.nav3spotlight0.rotation = (spotlight00rot += .4); } //rotation stopped for spotlight 2 on mouse out if (spotlightCW1 == true) { winNav.nav3spotlight1.rotation = (spotlight01rot -= .6); } else { winNav.nav3spotlight1.rotation = (spotlight01rot += .6); } //rotation stopped for spotlight 3 on mouse out if (spotlightCW2 == true) { winNav.nav3spotlight2.rotation = (spotlight02rot -= .6); } else { winNav.nav3spotlight2.rotation = (spotlight02rot += .6); } //rotation stopped for spotlight 4 on mouse out if (spotlightCW3 == true) { winNav.nav3spotlight3.rotation = (spotlight03rot -= .4); } else { winNav.nav3spotlight3.rotation = (spotlight03rot += .4); } } } } } }
Yes, the basic technique I outlined at first should still work.
Instead of declaring ever more nested functions so that things will have access to local variables declared in other functions, use instance level variables.
Somewhat improved code:
This so far, is just a rewrite of your existing code. I'm afraid I don't see how you want the bulbs to tie in to the spotlights. I also don't quite get what it's supposed to be doing, so my refactoring was at a surface level.Code:package com.site.microsite{ import flash.display.*; import flash.events.*; import flash.utils.Dictionary; public class WinathonMC extends MovieClip { private var winNav:WinNav; private var bulbClips:Array; //ideally, these would go away in favor of an actual Spotlight class. private var spotPos:Dictionary; private var spotInc:Dictionary; private var spots:Array; public function WinathonMC() { addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event):void{ winNav = new WinNav(); addChild(winNav); winNav.x = stage.stageWidth/2; winNav.y = 575; winNav.nav3spotlight0.rotation = 50; winNav.nav3spotlight1.rotation = 120; winNav.nav3spotlight2.rotation = 60; winNav.nav3spotlight3.rotation = 140; //spotlight variables spots = [winNav.nav3spotlight0, winNav.nav3spotlight1, winNav.nav3spotlight2, winNav.nav3spotlight3]; spotPos = new Dictionary(); for (var i:int = 0; i < spots.length; i++){ spotPos[spots[i]] = true; } spotInc = new Dictionary(); spotPos[winNav.nav3spotlight0] = 0.4; spotPos[winNav.nav3spotlight1] = 0.6; spotPos[winNav.nav3spotlight2] = 0.6; spotPos[winNav.nav3spotlight3] = 0.4; bulbclips = new Array(); for (var i:uint; i < 58; i++) { bulbclips.push(winNav.getChildByName("bulb"+i) as MovieClip); trace("winNav.bulb"+i); } winNav.addEventListener(MouseEvent.MOUSE_OVER,onNavOver); winNav.addEventListener(MouseEvent.MOUSE_OUT,onNavOut); } private function onNavOver(evt:MouseEvent):void { addEventListener(Event.ENTER_FRAME,onEnterFrame); } private function onEnterFrame(e:Event):void{ // rotation of spotlight 1 on mouse event adjustRot(30, 70, spots[0]); // rotation of spotlight 2 on mouse event adjustRot(70, 120, spots[1]); // rotation of spotlight 3 on mouse event adjustRot(50, 120, spots[2]); // rotation of spotlight 4 on mouse event adjustRot(90, 160, spots[3]); } private function adjustRot(min:int, max:int, target:MovieClip):void{ // rotation of spotlight on mouse event if (target.rotation > max) { spotPos[target] = false; } if (target.rotation < min) { spotPos[target] = true; } if (spotPos[target]) { target.rotation += spotInc[target]; } else { target.rotation -= spotInc[target]; } } private function onNavOut(evt:MouseEvent):void { addEventListener(Event.ENTER_FRAME,onEnterFrameNavOut); } private function onEnterFrameNavOut(e:Event):void { for (var i:int = 0; i < spots.length; i++){ if (spotPos[spots[i]]){ spots[i].rotation -= spotInc[spots[i]]; }else{ spots[i].rotation += spotInc[spots[i]]; } } } }
Also, you will probably want navOver and navOut to remove the EnterFrame listeners that the other one adds. Otherwise you'll get both at once.




Reply With Quote
