Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
Levorto

Optimizing code?

Recommended Posts

Hello Greensock Forum

 

Our group is making an interactive video project using Flash for LEGOLAND - a Danish theme park.

You can see the current progress here: www.legoland.mmd.eal.dk.

 

I have been in charge of coding the functionality of the video.

 

How can I optimize the following code, especially around line 192, regarding CuePoints?

http://legoland.mmd....com/parallax.as

 

Could I make a function that does the skipping, that I can refer to?

 

 

In advance thank you so much for your time,

- kind regards, Morten Broesby-Olsen.

Link to post
Share on other sites

I didn't have time to optimize everything or test any of this code, but here is a much more condensed version of your code:

 

package com {

   //==================================================================================================================================//
   //    Importér eksterne filer.
   //==================================================================================================================================//
   import com.HummingBird;    

   import com.greensock.*;
   import com.greensock.layout.*;
   import com.greensock.events.LoaderEvent;
   import com.greensock.loading.ImageLoader;
   import com.greensock.loading.LoaderMax;
   import com.greensock.loading.SWFLoader;
   import com.greensock.loading.VideoLoader;
   import com.greensock.loading.display.ContentDisplay;

   import flash.display.Sprite;
   import flash.display.MovieClip;
   import flash.events.MouseEvent;
   import flash.events.Event;

   //==================================================================================================================================//

   public class parallax extends MovieClip {
       private var _skipContent:ContentDisplay;
       private var _curVideo:VideoLoader;

       public function parallax() {

           //==================================================================================================================================//
           //    Start af parallax-kode:
           //==================================================================================================================================//
           new HummingBird(plane, 30, 20, 0, .3);

           new HummingBird(indgang, 8, 6, 0, .3);

           new HummingBird(sky1, -30, -10, 0, .3);
           new HummingBird(sky2, -40, -10, 0, .3);
           new HummingBird(sky3, -40, -10, 0, .3);

           new HummingBird(menu, 10, 8, 0, .3);
           //==================================================================================================================================//

           //==================================================================================================================================//
           //    Her loades alle videoklip via ekstern class-funktion:
           //==================================================================================================================================//                    
           // Aktiver nødvendig loader:
           LoaderMax.activate([VideoLoader, SWFLoader]);

           // Opret en LoaderMax-begivenhed, opret derefter de vigtige onProgress, onComplete og onError-listeners
           var queue:LoaderMax = new LoaderMax({name:"Videomateriale", onProgress:_progressHandler, onComplete:_queueCompleteHandler, onError:_queueErrorHandler,
                maxConnections:3, bufferTime:10, autoAdjustBuffer:true, bufferMode: true, width: 715, height: 402, scaleMode:"proportionalInside", volume:1});

           // Indsæt flere loaders:            
           queue.append( new SWFLoader("skip.swf", {name:"skip", estimatedBytes:2000, container:this, scaleMode:"proportionalInside", x: 635, y: 358, width: 100, height: 50, alpha:0, autoPlay:false}) );
           queue.append( new SWFLoader("menu.swf", {name:"menu", estimatedBytes:2000, container:this, scaleMode:"proportionalInside", x: 635, y: 358, width: 100, height: 50, alpha:0, autoPlay:false}) );

           queue.append( new VideoLoader("video/mad_og_drikke.flv", {name:"mad_og_drikke", visible: false, autoPlay:false, alpha:0, estimatedDuration:7, estimatedBytes:4833935, container:this}) );
           queue.append( new VideoLoader("video/housekeeping.flv", {name:"housekeeping", visible: false, autoPlay:false, alpha:0, estimatedDuration:69, estimatedBytes:101711872, container:this}) );
           queue.append( new VideoLoader("video/information.flv", {name:"information", visible: false, autoPlay:false, alpha:0, estimatedDuration:7, estimatedBytes:4833935, container:this}) );
           queue.append( new VideoLoader("video/gronne_omraader.flv", {name:"gronne_omraader", visible: false, autoPlay:false, alpha:0, estimatedDuration:69, estimatedBytes:101711872, container:this}) );
           queue.append( new VideoLoader("video/butikker.flv", {name:"butikker", visible: false, autoPlay:false, alpha:0, estimatedDuration:7, estimatedBytes:4833935, container:this}) );
           queue.append( new VideoLoader("video/tojbutik.flv", {name:"tojbutik", visible: false, autoPlay:false, alpha:0, estimatedDuration:69, estimatedBytes:101711872, container:this}) );
           queue.append( new VideoLoader("video/rengoring.flv", {name:"rengoring", visible: false, autoPlay:false, alpha:0, estimatedDuration:7, estimatedBytes:4833935, container:this}) );
           queue.append( new VideoLoader("video/restaurant.flv", {name:"restaurant", visible: false, autoPlay:false, alpha:0, estimatedDuration:69, estimatedBytes:101711872, container:this}) );
           queue.append( new VideoLoader("video/aktiviteter.flv", {name:"aktiviteter", visible: false, autoPlay:false, alpha:0, estimatedDuration:7, estimatedBytes:4833935, container:this}) );
           queue.append( new VideoLoader("video/reception.flv", {name:"reception", visible: false, autoPlay:false, alpha:0, estimatedDuration:69, estimatedBytes:101711872, container:this}) );

           // Prioriter den video, som er vigtigst:
           LoaderMax.prioritize("mad_og_drikke");

           // Begynd indlæsning af køen:
           queue.load();
           trace("Begynd indlæsning af video-køen... ");
       }
       //==================================================================================================================================//

       //==================================================================================================================================//
       //    Håndtering af fremskridt i loading-processen:
       //==================================================================================================================================//
       private function _progressHandler(event:LoaderEvent):void {
           // Procent indlæst:
           var procent:Number = Math.ceil((event.target.progress)*100);
           trace("Fremskridt: " + procent.toString() + "%")
           //procent_txt.text = "Indlæser: " + procent.toString() + "%";            
           //fremskridt_mc.scaleX = event.target.progress;            

       }

       private function _gotoButton(event:MouseEvent):void {
           _curVideo = event.target.video;
           _curVideo.playVideo();
           TweenMax.to(_curVideo.content, 1, {autoAlpha:1});
       }

       private function _slut(event:LoaderEvent):void {
           var video:VideoLoader = event.target;
           TweenMax.to(video.content, 0.5, {autoAlpha:0, onComplete:_stopVideo, onCompleteParams:[video]});
           TweenMax.to(_skipContent, 0.5, {autoAlpha:0});
       }

       private function function _stopVideo(video:VideoLoader):void {
           video.videoPaused = true;
           video.gotoVideoTime(0);
       }

       private function _onCuePoint(event:LoaderEvent):void {
           if (event.data.name == "skip") {                
               addChild(_skipContent);
               TweenMax.to(_skipContent, 0.7, {autoAlpha:1});
           }
       }

       private function _skipVideo(event:MouseEvent):void {
           if (_curVideo != null) {
               TweenMax.to(_curVideo.content, 0.5, {autoAlpha:0, onComplete:_stopVideo, onCompleteParams:[_curVideo]});
               TweenMax.to(_skipContent, 0.5, {autoAlpha:0});
           }
       }
       //==================================================================================================================================//

       //==================================================================================================================================//
       //    Ved fuldendt indlæsning - gør dette:
       //==================================================================================================================================//
       private function _queueCompleteHandler(event:LoaderEvent):void {                        
           trace(event.target + " har fuldent indlæsning.");        

           _skipContent = LoaderMax.getContent("skip.swf");
           _skipContent.addEventListener(MouseEvent.CLICK, _skipVideo);

           var videos:Array = ["mad_og_drikke","housekeeping","information","gronne_omraader","butikker","tojbutik","rengoring","restaurant","aktiviteter","reception"];
           var btn:MovieClip, video:VideoLoader;

           for (var i:int = 1; i <= 10; i++) {
               btn = menu["button" + i];
               btn.buttonMode = true;
               video = LoaderMax.getLoader(videos[i-1]);
               btn.video = video; //assign it to a property so that we can look it up in the _gotoButton() method by event.target.video.
               btn.addEventListener(MouseEvent.CLICK, _gotoButton);
               video.addASCuePoint(2.5, "skip", {message:"Her kan man skippe videoen."});
               video.addEventListener(VideoLoader.VIDEO_COMPLETE, _slut);            
               video.addEventListener(VideoLoader.VIDEO_CUE_POINT, _onCuePoint);
           }

       }
       //==================================================================================================================================//

       //==================================================================================================================================//
       //    Ved fejl i koden: Hvad gik galt?
       //==================================================================================================================================//
       private function _queueErrorHandler(event:LoaderEvent):void {
           trace("Fejl skete ved: " + event.target + ": " + event.text);
       }        
       //==================================================================================================================================//                
   }    
}

 

Anytime you find yourself writing a bunch of repetitive code that only changes the variable name (like button1.addEventListener(...), button2.addEventListener(...), etc.) that's a clue that you should utilize a loop and do things dynamically. Also, I'd recommend staying away from nested functions. Leverage the event.target in event handlers to sense which object was interacted with.

 

Anyway, hopefully that helps nudge you in the right direction. Cheers!

  • Like 1
Link to post
Share on other sites

Thank you so much, Jack!

 

I am so impressed with everything you guys do. Amazing support and innovation. I will definitely recommend Greensock to anyone I come in contact with, in regards to Flash - and now also Javascript!

 

See below post...

Link to post
Share on other sites

UPDATE: The small scale test worked out! I will hopefully be able to understand and implement the code before the 6th of june. Crossing my fingers and putting my thinking hat on.

 

EDIT2: I found out that one of the video's was bugged, and didn't contain anything. That fixed the problem!

 

EDIT3: Fixed this too! Thank you again!

Link to post
Share on other sites

In regards to the code above, how do I make sure the buttons only activate when they finished loading?

Link to post
Share on other sites

It looks to me like they wouldn't activate until they loaded because you're not adding the MouseEvent.CLICK listeners until the queueCompleteHandler() method gets called (which happens after the queue completes loading). Or are you asking how to only make them visible after the load? Maybe I'm not understanding the question.

Link to post
Share on other sites

All my above issues have been resolved √ - Yay!

 

BUT... I simply cannot get the code to work on Safari! …

I have tried several workarounds, but for some reason, the buttons are not working.

Crossing my fingers that it is possible to resolve this :-/

 

The code in question:

http://legoland.mmd.eal.dk/com/intro.as

http://legoland.mmd.eal.dk/com/parallax.as

 

And the website for good measure:

http://legoland.mmd.eal.dk/

Link to post
Share on other sites

We don't typically do general troubleshooting like that here in the forums, but if you have a specific question about a GreenSock tool, we'd be happy to help. I'd recommend adding trace() statements or some other way of checking to make sure that your CLICK event listeners are being added. If not, work backwards from there and see where the code is stopping. It's usually best to create a very simplified version of your file and build up from there until it breaks.

Link to post
Share on other sites

Thank you again :-) You have been more than helpful!

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×