Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Actionscript/preloading images/resizing/fading/using setInterval

Status
Not open for further replies.

webscripter

Programmer
Jul 29, 2002
266
US
I'm writing a script that will load images when a user clicks a list box of image names. No images are loaded unless the user clicks on the list box. I can get the first image to fade in but am having trouble on subsequent rounds. Using setInterval is pretty easy. I'm not sure where the script is losing it's performance.

Can anyone tell me where the problem lies?

stop();
alph = 0;
prealpha = 100;
function disappear(num, pic, itemcount){
var prevviewed = 0;
var flag = 0;
trace("in disappear("+num+", "+pic+")");
for(var i=0;i<itemcount;i++){

if(num != i){
if(this[&quot;pic_&quot;+i]){
var isprev = getProperty(this[&quot;pic_&quot;+i], _alpha);

if(isprev == 100 && flag ==0){
flag = 1;
prevviewed = i;
trace(&quot;prevviewed is &quot;+prevviewed);
}
}
}
}
fadin(num,pic, prevviewed);
}

function fadin(num,pic, prev){
//set the picture chosen to 100%
trace(&quot;in fadin(&quot;+num+&quot;, &quot;+pic+&quot;, prev:&quot;+prev+&quot;)&quot;);
if(this[&quot;pic_&quot;+num]){
fader2 = setInterval(bringin, 100, prev, this[&quot;pic_&quot;+num]);
}else{trace(&quot;image doesn't exist yet.&quot;);
loadMC(num, pic, prev);
}
}
function bringin(prv, obj ){
trace(&quot;in bringin(prv: &quot;+prv+&quot;, &quot;+obj+&quot;)&quot;);
alph += 10;
prealpha -= 10;
trace(this[&quot;pic_&quot;+prv]+&quot; ******* prealpha -10 :&quot;+prealpha);
setProperty(this[&quot;pic_&quot;+prv], _alpha, prealpha);
setProperty(obj, _alpha, alph);
updateAfterEvent();
if(alph >= 100){
setProperty(this[&quot;pic_&quot;+prv], _alpha, &quot;0&quot;);
setProperty(obj, _alpha, &quot;100&quot;);
updateAfterEvent();
trace(&quot;clear interval fadeint&quot;);
clearInterval(fader2);
}
}


function loadMC(num, pic){
trace(&quot;in loadMC(&quot;+num+&quot;, &quot;+pic+&quot;)&quot;);
if(pic != &quot;&quot;){
// trace(&quot;this visible false&quot;);
setProperty(this, _visible, false);
var j = num + 10;
trace(&quot;loading &quot;+pic+&quot; into &quot;+this[&quot;pic_&quot;+num]);
this.createEmptyMovieClip(&quot;pic_&quot;+num,j).loadMovie(pic);
preload(num, this[&quot;pic_&quot;+num]);
}else{trace(&quot;pic variable isn't set :&quot;+pic);}
}

function preload(num, obj) {
trace(&quot;in preload(&quot;+num+&quot;)&quot;);
//preloader._visible = true;
//preloader._bar._xscale = 0;
//preloader.display.text = &quot;0% loaded&quot;;
loadinter = setInterval(callback, 10, obj);
}

function callback(obj) {
trace(&quot;in callback(&quot;+obj+&quot;)&quot;);
//this traces as undefined, it should have been created above in loadMC()
var download = obj.getBytesTotal();
var downloaded = obj.getBytesLoaded();
var percentLoaded = Math.cell(downloaded/download*100);
// preloader.display.text = percentLoaded+&quot;% loaded&quot;;
// preloader.bar._xscale = percentLoaded;
updateAfterEvent();
if (downloaded == download && download>0) {
//preloader._visible = false;
trace(&quot;setpicture is called&quot;);
clearInterval(loadinter);
setpicture(obj, 275, 200, 0,0);
}
}

function setpicture(obj, bw, bh, xp, yp){
trace(&quot;in setpicture&quot;);
var w = getProperty(obj, _width);
var h = getProperty(obj, _height);
var x = 0;
var y = 0;
if(w <= bw){
}else{
h = Math.round(h*bw/w);
w = bw;
}
if(h < 200){
}else{
w = Math.round(w*200/h);
h = bh;
}
if(h <= bh && w <= bw){
var center = ( bh - h)/2;
y = center;
center = (bw - w)/2;
x = center;
}
setProperty(obj,_width, w);
setProperty(obj,_height, h);
x = xp+x;
setProperty(obj,_x,x);
y = yp+y;
setProperty(obj,_y,y);
setProperty(obj,_alpha,0);
alph = 0;
trace(&quot;this visible true&quot;);
setProperty(this, _visible, true);
fader1 = setInterval(inload, 100, obj);
}

function inload(obj){
trace(&quot;inload alph : &quot;+alph);
trace(&quot;inload obj.alpha: &quot;+getProperty(obj, _alpha));
alph += 10; setProperty(obj,_alpha,alph);
updateAfterEvent();
if(alph > 90){
trace(obj+ &quot; alpha is set to 100%&quot;);
setProperty(obj, _alpha,&quot;100&quot;);
clearInterval(fader1);
}
}
Thanks
Tricia
yorkeylady@earthlink.net
 
I've almost got it. But the percentages are off somewhere for the alphas.
stop();
var alph = 0;
var prealpha = 100;
var fader1 = &quot;&quot;;
var fader2 = &quot;&quot;;
var prevviewed = 0;
var flag = 0;
var selected = 0;
var inc = 5;
function disappear(num, pic, itemcount){
selected = num;
prealpha = 100;
alph = 0;
trace(&quot;in disappear(&quot;+num+&quot;, &quot;+pic+&quot;)&quot;);
for(var i=0;i<itemcount;i++){
if(num != i){
if(this[&quot;pic_&quot;+i]){
var isprev = getProperty(this[&quot;pic_&quot;+i], _alpha);
if(isprev == 100){
prevviewed = i;
// setProperty(this[&quot;pic_&quot;+i], _alpha, &quot;0&quot;);
}
}
}
}
fadin(num,pic);
}

//here is where we determine if the picture is fading in or needs loaded
function fadin(num,pic){
//set the picture chosen to 100%
trace(&quot;in fadin(&quot;+num+&quot;, &quot;+pic+&quot;, prev:&quot;+prevviewed+&quot;)&quot;);
if(this[&quot;pic_&quot;+num]){
if(prevviewed != selected){
fader2 = setInterval(bringin, 150, this[&quot;pic_&quot;+num]);
}
}else{trace(&quot;loading the image now.&quot;);
loadMC(num, pic);
}
}

function bringin(obj){
alph += inc;
prealpha -= inc;
trace(this[&quot;pic_&quot;+prevviewed]+&quot; ******* prealpha -10 :&quot;+prealpha);
setProperty(this[&quot;pic_&quot;+prevviewed], _alpha, prealpha);
setProperty(obj, _alpha, alph);
trace(&quot;setProperty _alpha &quot;+alph);
if(alph >= 90){
if(this[&quot;pic_&quot;+prevviewed]){setProperty(this[&quot;pic_&quot;+prevviewed], _alpha, &quot;0&quot;);}
setProperty(obj, _alpha, &quot;100&quot;);
// trace(&quot;clear interval fadeint&quot;);
clearInterval(fader2);
}
trace(&quot;updating properties&quot;);
updateAfterEvent();
}


function loadMC(num, pic){
trace(&quot;in loadMC(&quot;+num+&quot;, &quot;+pic+&quot;)&quot;);
if(pic != &quot;&quot;){
// trace(&quot;this visible false&quot;);
setProperty(this, _visible, false);
var j = num + 10;
trace(&quot;loading &quot;+pic+&quot; into &quot;+this[&quot;pic_&quot;+num]);
this.createEmptyMovieClip(&quot;pic_&quot;+num,j).loadMovie(pic);
preload(this[&quot;pic_&quot;+num]);
}else{trace(&quot;pic variable isn't set :&quot;+pic);}
}

function preload(obj) {
trace(&quot;in preload(&quot;+obj+&quot;)&quot;);
//preloader._visible = true;
//preloader._bar._xscale = 0;
//preloader.display.text = &quot;0% loaded&quot;;
//obj.watch('alpha',callback, obj);
loadinter = setInterval(callback, 150, obj);
}

function callback(obj){
trace(&quot;in callback(&quot;+obj+&quot;)&quot;);
//this traces as undefined, it should have been created above in loadMC()
var download = obj.getBytesTotal();
var downloaded = obj.getBytesLoaded();
var percentLoaded = Math.cell(downloaded/download*100);
// preloader.display.text = percentLoaded+&quot;% loaded&quot;;
// preloader.bar._xscale = percentLoaded;
if (downloaded == download && download>0) {
//preloader._visible = false;
trace(&quot;setpicture is called&quot;);
// obj.unwatch('alpha')
clearInterval(loadinter);
setpicture(obj, 275, 200, 0,0);
}
updateAfterEvent();
}

function setpicture(obj, bw, bh, xp, yp){
trace(&quot;in setpicture&quot;);
var w = getProperty(obj, _width);
var h = getProperty(obj, _height);
var x = 0;
var y = 0;
if(w <= bw){
}else{
h = Math.round(h*bw/w);
w = bw;
}
if(h < 200){
}else{
w = Math.round(w*200/h);
h = bh;
}
if(h <= bh && w <= bw){
var center = ( bh - h)/2;
y = center;
center = (bw - w)/2;
x = center;
}
setProperty(obj,_width, w);
setProperty(obj,_height, h);
x = xp+x;
setProperty(obj,_x,x);
y = yp+y;
setProperty(obj,_y,y);
setProperty(obj,_alpha,&quot;0&quot;);

// trace(&quot;this visible true&quot;);
setProperty(this, _visible, true);
fader1 = setInterval(inload, 150, obj);

}

function inload(obj){
trace(&quot;in inload(&quot;+obj+&quot;)&quot;);
alph += inc;
prealpha -= inc;
trace(this[&quot;pic_&quot;+prevviewed]+&quot; ******* prealpha -10 :&quot;+prealpha);
//make sure your not fading and unfading the same picture
setProperty(this[&quot;pic_&quot;+prevviewed], _alpha, prealpha);
trace(&quot;setProperty prevviewed &quot;+prevviewed+&quot;, &quot;+prealpha);
setProperty(obj, _alpha, alph);
trace(&quot;setProperty _alpha &quot;+alph);
if(alph >= 90 ){
if(this[&quot;pic_&quot;+prevviewed]){
trace(&quot;setProperty(&quot;+this[&quot;pic_&quot;+prevviewed]+&quot; : &quot;+0);
setProperty(this[&quot;pic_&quot;+prevviewed], _alpha, &quot;0&quot;);
}
setProperty(obj, _alpha, &quot;100&quot;);
trace(&quot;setProperty(&quot;+obj+&quot; : &quot;+100);
// trace(&quot;clear interval fadeint&quot;);
clearInterval(fader1);
}
trace(&quot;updating properties&quot;);
updateAfterEvent();
}
Thanks
Tricia
yorkeylady@earthlink.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top