flash actionscript:
Anyone knows how to tell when an image was loaded completely by loadMovie? I wanted next image to be loaded after first one was loaded completely and displayed for 2 seconds. I am trying to do this for 2 days. Maybe a sample code... Sometimes getBytesTotal() returns -1. Why would this happen? Thank you. - Adrian.
Here...the following should help... //BEGIN: Load & Test Movie loadMovieNum("movie.swf?loaded=yes", 99); reTest99 = setInterval(testLoad99, 1000); function testLoad99(){ if(_level99.loaded == "yes"){ clearInterval(reTest99); runProceed(); } } //END: Load & Test Movie
I agree with Narven, the MovieClipLoader class and its events make it a lot easier to load external files and know when they have been loaded. It is only available But if you can't or don't want to use MCL... You need to use an onEnterFrame or setInterval to continuously test the getBytesLoaded and the getBytesTotal. As for why does getBytesLoaded sometimes return -1, why does friction always oppose the direction of motion? Because that is what it does! But now that we understand the behavior we can plan for it. When you check these things you should do something like this: if(myClip.getBytesLoaded>=myClip.getBytesTotal && myClip._width>0) That will make sure the clip is fully loaded and working. You could then do a setInterval to wait the two seconds and then call another load. var waitInterval=setInterval(myLoader,2000); And then have a function called myLoader like this: function myLoader(){ clearInterval(waitInterval); this.loadMovie(newNameOfSource); }
Thank you guys, I managed to make it by the MovieClipLoader after all. I had it by setInterval before I start this topic but things didnt work well because I had more complicated things like next iamge loading after previous one displayed for x seconds and a fade effect, etc. Now it works by LoadMovieClip ( I didnt understand how it works :) ) I copy/paste the LoadMovieClip samples and I modified the code. Thanks. - Adrian.
So for other users, when testing the bytes loaded compared to bytes total: if nothing is loaded yet, bytes total can be 0 and bytes loaded = 0 and in this case you can get confused thinking that loading was completed. Like Rothrock said , this can be fixed by adding: && myClip._width>0 to the condition when testing bytes loaded vs bytes total. if(myClip.getBytesLoaded>=myClip.getBytesTotal && myClip._width>0) I think this is a common problem that can appear in this case.
Actually the width test does what adrianTNT has made explicit and it has one other benefit. Under certain combinations of browser, flash plug-in player version, computer platform, state of the user's cache, and (as best I can tell!) the phase of the moon an external file can have all its bytesLoaded, but still not be addressable by actionscript. In these instances you wouldn't be able to do something like myLoadedClip._alpha=50. It just wouldn't work. I think it has something to do with waiting just one frame longer for the clip to finis initializing or some such. The best solution I learned (whoever taught me, thank you.) was to add that _width test. So there you go. It actually is even better for you than you knew.
I wanted to add a progress bar in my application and I don't know what to enter for the SOURCE, MODE and also don't know the code block that should update the progress. Can one of you help me with this? This is the main part of my code now: ============================================================ mclListener.onLoadStart = function(target_mc:MovieClip) { target_mc.startTimer = getTimer(); }; mclListener.onLoadComplete = function(target_mc:MovieClip) { //load my next image, etc. }; mclListener.onLoadInit = function(target_mc:MovieClip) { var timerMS:Number = target_mc.completeTimer-target_mc.startTimer; target_mc.createTextField("timer_txt", target_mc.getNextHighestDepth(), 0, target_mc._height, target_mc._width, 22); target_mc.timer_txt.text = "loaded in "+timerMS+" ms."; }; var image_mcl:MovieClipLoader = new MovieClipLoader(); image_mcl.addListener(mclListener); image_mcl.loadClip(my_image_list, image_mc); ========================================================= As I said, I don't know how the code that updates the progress should look like and I don't know what to enter for MODE (probably manual") and for SOURCE in progress bar parameters. I find the documentation for the progress bar very complicated, I cannot find a simple sample. Thank you.
If by progress bar you mean one of the components you should drop that. Most of the components add too much size to your files. They are often difficult to modify. Instead check out the onLoadProgress event in the MovieClipLoader class. I'm away from the help files at the moment, but I can give you some general tips for a simple pre-loader: Make a long rectangle movieclip. You might want to have the registration point be on the left edge of the bar, but then again, maybe not. Put it on the stage and call it myBar. At the beginning of your code do: myBar._visible=false. In the onLoadStart handler make it show: myBar._visible=true; Also make the bar narrow with: myBar._xscale=1; I think you could try zero, but I seem to recall that can cause problems. Not sure. Now if the onLoadProgress event you should use the sample from the help files to calculate the percent loaded. Once you know the percent loaded update the loader with: myBar._xscale=percentLoaded; Finally in the onLoadComplete be sure to hide the bar again with myBar._visible=false; Another idea would be to just have some cute (manly, exciting, dreamy, whatever) little animation. Again put it on the stage. You would start by telling it to be invisible and stop(). Then you would make it visible and tell it to play in the onLoadStart and hide it and stop it again with the onLoadComplete. In this case you don't even need to have an onLoadProgress event handler unless you want to. Makes sense?
yes, I wanted to use the progress bar component, but I understand it is complicated to use and you said it increases file size, I will just create a custom preloader then. I did that before (create a custom preloader).
In the same code as above, I am trying to trace the bytes loaded (with setInterval). When I publish I get error: ----------------------------------------------------- There is no property with the name 'getBytesLoaded'. trace(image_mcl.getBytesLoaded); -----------------------------------------------------
Check the help files to be sure, but I think when using the MovieClipLoader class there is a bytesLoaded and bytesTotal property. When using loadMovie or whatever, then there are a getBytesLoaded() and getBytesTotal() method. Why are you using setInterval? Is there a reason you don't want to use the onLoadProgress event? BTW, neither approach will work in the testing environment. When done from a local machine, the Flash Player loads external items in one chunk so the onLoadProgress event never gets called and the getBytesLoaded will go from 0 (maybe with a small jump to -1 or whatever) to all loaded.
Originally posted by: Rothrock Why are you using setInterval? Is there a reason you don't want to use the onLoadProgress event? Because I cant get it to work, I never used it before, and the samples don't seem to work. Originally posted by: Rothrock BTW, neither approach will work in the testing environment. When done from a local machine, the Flash Player loads external items in one chunk so the onLoadProgress event never gets called and the getBytesLoaded will go from 0 (maybe with a small jump to -1 or whatever) to all loaded. Why is that? I dont understand, is this a flash bug? At one point trying to trace bytes loaded I only got total size, maybe this is what you said above? Can you post a sample to trace bytes loaded for the code block above? I don't know if it should be image_mcl.getBitesLoaded image_mcl.bitesLoaded() image_mcl.bitesLoaded and I dont even know if is "image_mcl" or "image_mc" without the "L" at end because I have both clips, the way I copy/paste it from samples. It works but I dont know how to trace bites loaded. Thanks.
I'm away from my help files at the moment, but it should be something like: mclListener.onLoadProgress = function(target, bytesLoaded, bytesTotal) { trace("Loaded: "+bytesLoaded); trace("Total: "+bytesTotal); trace("Percent: "+bytesLoaded/bytesTotal*100); }; BTW, I'm not sure if you are missing it or just didn't paste it, but there needs to be a line that says: var mclListener=new Object(); It needs to be before the place where you start adding the event handlers. As for how Flash works when run from a local machine. That is just how it works. Files behave differently when loaded from a hard drive versus over the internet. So to test these things you will have to eventually put it on a website somewhere. Once you get the hang of it, you will come up with better names that are more meaningful and helpful to you. Here is a bit to help you understand, perhaps. In Flash everything is an object. Different objects have different properties and methods (things they can do). Some also have events associated with them. So for an instance of a MovieClip object there are properties like _x and _y position or _width. There are methods like gotoAndPlay() or stop(). An example of an event would be onRollOver(). Properties often start with an underscore to indicate they are a property. Likewise methods are often verbs to show the action. And finally events often start with the word on to show they depend on something happening. So how about the stove object? it has properties ? like number of burners ? and methods ? like broil ? and events ? like food is done. Okay, so there isn't really a stove object in Flash, but you get the idea? The reason food is done isn't a method isn't you can't just say "food is done now" it happens when it happens so you need an event handler ? a bit of code that just sits there and waits until the event happens. Some event handlers also return some information about the event when it happens ? and some don't. So the foodIsDone handler might return which burner or the internal temperature of the food or both! In our made up example it could look like this: myFoodListener=new Object(); myFoodListener.onFoodIsDone=function(burner,internalTemp){ hotdish=removeFoodFromBurner(burner); if(internalTemp>180){ placeOnTrivet(hotdish); } } myFoodListener.onFoodIsCooking=function(burner){ burner.stirFood(); } myStove=new Stove(); myStove.attachListener(myFoodListener); myStove.cookMeal("Fish",3); myStove.cookMeal("Brussel Sprouts",2); So this code creates an object called the myFoodListener. To that we add some events. After that we create a new stove object. Add the myFoodListener as a listener and finally start to cook a meal of fish on burner three and brussel sprouts on burner two. Does any of this help? I am a bit more than crazy. I guess this is a long way around to say that image_mcl is a MovieClipLoader object. And if you check the MovieClipLoader in the help file you will see that it doesn't have a getBytesLoaded property or method. (By the way that is bytes with a "y".) But that the onLoadProgress event does return the bytes loaded. image_mc is an instance of a movie clip object. The movieclip class does have a method getBytesLoaded(); (Notice the verb "get" stuck on there.) So you might also be able to do this within the onLoadProgress event. image_mc.getBytesLoaded(); But then you would be tied into only using your MovieClipLoader to load into image_mc and not any other clips. So notice that the onLoadProgress event also returns a reference to which clip it is that is being loaded into. In the example it is called target_mc.
That worked perfect, the code block you posted at beginning. That was what I was looking for, maybe I write it correctly few times but it didn't work before I uploaded the images on a server. I thought it should be same thing when I only simulated download. Yes, I had that code line you mentioned, var mclListener=new Object(); I did used "Bytes" instead of "Bites", I just write it wrong by mistake above than I pasted same word written wrong. The cooking explanations ware very useful too :) , I will have to learn more about listeners. It works perfect now, each image is loaded, displayed for 2 seconds, preloaded indicates correctly; now I will try to add a fade effect. Thank you for your time.
Glad that I didn't go completely off the end. Just remember that when this stuff is new to you that it was created by people and generally wont be beyond some kind of analogy to what you already know. The difficult part is when we expect Flash to magically know what we want to happen.
Don't see what you're looking for? Try a search.
|