I’m having trouble trying to cache a movieclip that contains an external image that I have successfully loaded.
My problem is that the fade function that is running when after the image is duplicated doesn’t appear to be affecting the mc that contains the cached image.
Is there a handler that I can use to ensure that the bitmap is attached inside the movie clip before I run my fade in, or do I have to beware of a timing issue and delay running the setInterval for the fade in?
bmppopulatedClip.onLoadInit = functiontoRun; or something?
Thanks for any help.
***Here’s my code that doesn’t work:***
Stage.scaleMode = 'noScale';
Stage.align = 'TL';
import flash.display.*;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.geom.Transform;
import flash.geom.ColorTransform;
import flash.filters.ColorMatrixFilter;
//pseudo "framerate"
//derived from from diving 1 second by targetFrameRate (30) then rounding off.
//== animspeed
var SW:Number = Stage.width;
var SH:Number = Stage.height;
var milliseconds:Number = 1000;
var targetFrameRate:Number = 30;
var firstrun:Boolean = true;
var completed:Boolean = true;
var errorState:Boolean;
var animspeed:Number = Math.round((milliseconds / targetFrameRate));
var mcListener:Object = new Object();
stop();
trace (":::::: initVars ran");
var intervalID:Number;
function fadeIn(clip:MovieClip, upperBound:Number) {
trace (":::::: fadeIn ran");
//should be called with a setInterval command
var revealIncrement:Number = 5;
if (clip._alpha <= upperBound) {
clip._alpha += revealIncrement;
trace("reveal: " +clip+" "+ clip._alpha);
//assumes that fadeIn has been called with a setInterval command
updateAfterEvent();
}
if (clip._alpha >= (upperBound - revealIncrement)) {
clip._alpha = 100;
clearInterval(intervalID);
quickShow(clip);
}
}
function softReveal(clip:MovieClip, upperBound:Number, intervalDuration:Number):Void {
trace (":::::: SoftReveal ran");
//creates a setInterval that fades in a movie clip passed in as "clip"
clearInterval(intervalID);
intervalID = setInterval(fadeIn, intervalDuration, clip, upperBound);
}
function doStuff(clip:MovieClip):Void{
//assumes "holder movieclip is present in the "this" scope
//caches the holder clip
cloneClip(clip, "holder",1);
}
function displayEventReady(success):Void {
trace (":::::: displayEventReady ran");
if (success) {
trace("cool, here's the image");
doStuff(holder);
}
else {
trace("An error occurred. The displayEvent did not commence.");
}
}
function populatebannerbg():Void {
trace (":::::: populatebannerbg ran");
var mcl:MovieClipLoader = new MovieClipLoader();
mcl.addListener(mcListener);
mcl.loadClip("image.png",holder);
mcListener.onLoadInit = displayEventReady;
//we're done with the listener now, we can delete it.
removeListener(mcListener);
}
function quickShow(clip:MovieClip) {
trace (":::::: quickShow ran");
//quickly hides and disables a movie clip
clip.enabled = true;
clip._visible = true;
clip._alpha = 100;
}
function quickHide(clip:MovieClip) {
trace (":::::: quickHide ran");
//quickly hides and disables a movie clip
trace(clip + " was hidden from view::::::");
clip.enabled = false;
clip._visible = false;
clip._alpha = 0;
}
function cloneClip(clip:MovieClip,clipName:String, doCachedAsClip:Number):Void {
trace (":::::: cloneClip ran");
//creates a bitmap of the passed clipName, and sets cacheing for masking (0 false, 1 true)
//ensures that the clone is at the same level as the parent.
var clipRef:MovieClip = this[clipName];
quickHide(clipRef);
if (clipRef != undefined) {
if (doCachedAsClip) {
var clipCloned:MovieClip = clipRef._parent.createEmptyMovieClip(clipName + "clone", clipRef._parent.getNextHighestDepth());
var clipBMP:BitmapData = new BitmapData(clipRef._width, clipRef._height, true, 0x00FFFFFF);
clipBMP.draw(clipRef);
clipCloned.attachBitmap(clipBMP,clipCloned._parent.getNextHighestDepth(),"always",false);
trace(clipCloned && (clipCloned._width > 0));
if (clipCloned && (clipCloned._width > 0)) {
quickHide(clipCloned);
clipCloned.cacheAsBitmap = true;
softReveal(clipCloned,100,animspeed);
}
removeMovieClip(clipRef);
}
else {
trace("nah, don't cache this clipName as a bitmap");
}
}
else {
trace("nothing was cloned");
}
}
function drawBounds() {
trace (":::::: drawBounds ran");
//creates a clipping rectangle the size of the banner
boundMask.cacheAsBitmap = true;
holder.setMask(boundMask);
}
function initWhatever():Void {
trace (":::::: initWhatever ran");
if (firstrun) {
this.createEmptyMovieClip("holder",this.getNextHighestDepth());
//drawBounds();
populatebannerbg();
firstrun = false;
}
}
initWhatever();
***Here’s my code that works with an image:***
Stage.scaleMode = 'noScale';
Stage.align = 'TL';
import flash.display.*;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.geom.Transform;
import flash.geom.ColorTransform;
import flash.filters.ColorMatrixFilter;
//pseudo "framerate"
//derived from from diving 1 second by targetFrameRate (30) then rounding off.
//== animspeed
var SW:Number = Stage.width;
var SH:Number = Stage.height;
var milliseconds:Number = 1000;
var targetFrameRate:Number = 30;
var firstrun:Boolean = true;
var completed:Boolean = true;
var errorState:Boolean;
var animspeed:Number = Math.round((milliseconds / targetFrameRate));
var intervalID:Number;
var mcListener:Object = new Object();
stop();
function fadeIn(clip:MovieClip, upperBound:Number) {
//should be called with a setInterval command
var revealIncrement:Number = 5;
if (clip._alpha <= upperBound) {
clip._alpha += revealIncrement;
trace("reveal: " + clip._alpha);
//assumes that fadeIn has been called with a setInterval command
updateAfterEvent();
}
if (clip._alpha >= (upperBound - revealIncrement)) {
clip._alpha = 100;
clearInterval(intervalID);
quickShow(clip);
}
}
function softReveal(clip:MovieClip, upperBound:Number, intervalDuration:Number):Void {
//creates a setInterval that fades in a movie clip passed in as "clip"
clearInterval(intervalID);
intervalID = setInterval(fadeIn, intervalDuration, clip, upperBound);
}
function displayEventReady(success):Void {
if (success) {
trace("cool, here's the image");
//assumes "holder movieclip is present in the "this" scope
softReveal(holder,100,animspeed);
}
else {
trace("An error occurred. The displayEvent did not commence.");
}
}
function populateWithImg():Void {
var mcl:MovieClipLoader = new MovieClipLoader();
mcl.addListener(mcListener);
mcl.loadClip("img.png",holder);
//we're done with the listener now, we can delete it.
removeListener(mcListener);
}
function quickShow(clip:MovieClip) {
//quickly hides and disables a movie clip
clip.enabled = true;
clip._visible = true;
clip._alpha = 100;
}
function quickHide(clip:MovieClip) {
//quickly hides and disables a movie clip
clip.enabled = false;
clip._visible = false;
clip._alpha = 0;
}
function initWhatever():Void {
if (firstrun) {
this.createEmptyMovieClip("holder",this.getNextHighestDepth());
quickHide(holder);
populateWithImg();
mcListener.onLoadInit = displayEventReady;
firstrun = false;
}
}
initWhatever(this);