Forums: Flash:

 

ActionScript timer - setInterval?

first 2 last
 

mclarkson ActionScript timer - setInterval?

My AS skills are admittedly slight, but I'm trying to adapt an Flash / XML image viewer that I built for my web page. I modified code from the web which I almost understand.

I want to change the script to change images based on a timer, rather than a keystroke. I've been trying to use setInterval but clearly I don't know what the heck I'm doing, as all I can get it to do is hang my program indefinitely.

Maybe I'm putting it in the wrong place? My first thought was to put it in here

this.onEnterFrame = function() {
////MAYBE PUT IT HERE? ////////////////
filesize = picture.getBytesTotal();
loaded = picture.getBytesLoaded();
preloader._visible = true;
if (loaded != filesize) {
preloader.preload_bar._xscale = 100*loaded/filesize;
} else {
preloader._visible = false;
if (picture._alpha<100) {
picture._alpha += 10;
}
}

};


But that ain't gettin' me nowhere.

I'm not entirely sure how to work the thing. I've been trying this
setInterval( nextImage(){ trace("interval called"); }, 8000 );


with the hope of having it call the function nextImage() after 8 seconds. But no.

Any hints? Danke.

Could you, would you, with a goat?
quote
 

Storm

Without adjusting your code at all.....


function nextImage():Void {
trace("going to load image number: "+count);
// your code
filesize = picture.getBytesTotal();
loaded = picture.getBytesLoaded();
preloader._visible = true;
if (loaded != filesize) {
preloader.preload_bar._xscale = 100*loaded/filesize;
} else {
preloader._visible = false;
if (picture._alpha<100) {
picture._alpha += 10;
}
// end your code
}
if (count>=maxCount) {
clearInterval(intervalId);
}
count++;
}

intervalId = setInterval(this, "nextImage", duration);


intervalId will trigger every eight seconds and do it 10 times. Change those as you see fit. In your example, it may never "count out" so get rid of that.

BUT always run clearInterval before you leave to navigate somewhere else (close image viewer as an example).

Also, look up the Tween class to do your alpha fade.

 

mclarkson

Okay. Um ...

Why will intervalId do anything? It looks like a simple variable.

Why will it do it 10 times?

Who calls the nextImage() function? How does it get run in the first place?










And thanks. big grin

Could you, would you, with a goat?
quote
 

Arsis

[i]intervalId[/] gives you a reference to the instance of the interval if you want to do stuff like close it before it has comeplete: clearInterval( intervalId )

You cannot define the number of repetitions using an interval which is why Storm has added count>=maxCount where, in your case, you would set count to 0 and maxCount to 10 outside of your function.

nextImage could be invoked at any time though in this case it is invoked by the interval set in the last line of AS (at a rate equal to duration).

 

Storm

admittedly.....sometimes Flash programming is what it is.

intervalId is only a holder in th system for the class function setInterval which runs every eight seconds (duration) and every eight seconds it calls whatever you call "nextImage" function.

That function could simply go:


function nextImage():Void {
title.text = "I am repeating myself now."
}

kind of like a 12s conversation.....

it stops at 10 because I said if count>=maxCount

 

Storm

and arse beat me to it...going to bed....all yours

 

mclarkson

You guys are clearly talking over my head, here.

Storm, you replaced my function

this.onEnterFrame = function()


with
function nextImage():Void 


Why? Does this replace the onEnterFrame action? Because I have a separate nextImage() function. Is that wrong?

So, nextImage() is being called from this code:
intervalId = setInterval(this, "nextImage", duration);

??

What does this refer to in the function call. What is this, at this point? The main timeline? Or...?

Why is "nextImage" like that, as opposed to "nextImage()"? Isn't this the call to the nextImage function? The Flash help seems to say that the 'code' goes in there, but if I were coding it, I'd say nextImage(); rather than nextImage

I hadn't planned on having a limit; is there any reason I should or are you just throwing that in as an additional example?

Can you tell that this is not my strong suit?

Do you wish I'd go away?

Could you, would you, with a goat?
quote
 

Storm

going backwards....

Yes

Yes

The Latter

shrug.gif

smile

Don't worry it's fine. We'll get you there and starting from the beginning.

You chose onEnterFrame to do a loop. You chose it. I "suggested" (subtly) that setInterval would be a better idea than onEnterFrame. It's a little less resource hungry. Since the title of your thread said "setInterval" and you tried to use setInterval in the bottom, I assumed you wanted setInterval and not onEnterFrame. You can use whatever your little bearded heart desires but you can't use both greedy mcgreedpants. Pick one.

...enough humming and hawwing....I decided for you and we're going to use setInterval. You don't need onEnterFrame.

So, we need to set up a function for it to do the work. I called it nextImage because it's going to load...well....the next image....clever, right? It's no different than setting up any function and does not have to be wrapped in a timer'ish function such as onEnterFrame. Just declare it like any other


function nextImage() {

}


Easy!!! Ignore the :Void because we don't even need it in AS2.

We made a function and we need a timer......setInterval it is!! We use intervalId = because we assign it to a 'holder' that we can identify it to remove it with clearInterval later or to seperate it from any other ones we might have running.

this - is the timeline that it should look for the function in (and since they're both together it would be THIS timeline)

nextImage - is not NextImage() because we're not specifically calling a function here, we are passing a parameter TO THE setInterval class which will call the function....not us. We pass it a "String" instead of calling a function. Make sense?

duration - the final parameter that the setInterval class wants from us. We don't get to decide....it's pre-written by Macromedia for us. We get no say, so deliver it what it wants.

This will work and it will trigger endlessly until you use clearInterval(intervalId).

But it won't do anything because there's nothing IN the function. The function is triggering, but there's nothing in it. You're now wasting resources....congrats!!

So, now you need to put something in your function called nextImage.

Do it. Do it now.....decide what it needs to do and go from there.

Does that help?

The count >= maxCount was there to give you some error checking in case it didn't work. You want to "try" 10 times in my example before giving up and deleting itself.

Where I want to clear your brain is you don't want to think of this as "Well, I don't want to limit them to 10 images, so fuck off!" -- You need to think of this as "I want to change this current image to the next one, did it work? Yes, ok delete itself, No? try again until you try 10 times and then delete itself."



 

mclarkson

Thank you, Sir!

Since I haven't posted all the code up here, this doesn't tie in with what I've got but for the first time I actually understand what the code I've cut-and-pasted-and-modified is actually doing!

Setting up setInterval properly was key, of course. Thanks so much for that.

So I've got things working so that it rolls thru the pictures as it's supposed to, using a stripped-down version of my old code. Now that I understand what that code's doing, I'll take a look at modifying it along the lines suggested here.

Given that this will be running in a web page banner, do I need to clear the interval? I can't imaging where I'd do it.

Could you, would you, with a goat?
quote
 

Storm

I was thinking about your project and if you're using a timer for the progress of the loading of each image, then onEnterFrame will give you a smoother progressbar because it updates every frame versus the setInterval which would usually be in seconds.

If the functionality just repeats endlessly, then you probably won't need clearInterval because they'll be launching another page and the banner will be gone anyway.

 

mclarkson

I'm currently displaying the photo in via onEnterFrame, as per my code above, and using setInterval to call nextImage(), which just changes which image is being shown, looping endlessly - 1,2,3,1,2,3...

That's working great for what I need to do, if there's no strong reason not to do it.


On a related not, should I be able to load a URL for a button from the XML, as well?

Could you, would you, with a goat?
quote
 

Storm

perfect...that's what I though.


You can use XML for whatever you need to do. I do everything from xml. I even load in my hex colours from xml files these days.

 

mclarkson

The XML code, again, I only vaguely understand. Here's what I'm using:

function loadXML(loaded) {

if (loaded) {

xmlNode = this.firstChild;
image = [];
total = xmlNode.childNodes.length;
for (i=0; i<total; i++) {
image[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
}
firstImage();
}
else {
content = "file not loaded!";
}

}
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("ads.xml");


If I wanted to add, let's say, the delay to the XML sheet, what would I do? Pick it up right before the for loop?

I am totally baffled by all the .childNodes[i].childNodes[0].firstChild.nodeValue stuff. :|

Could you, would you, with a goat?
quote
 

Storm

childNodes simply means counting down through the path


<xml>
<childNode forMC="this is childNode 0">childNodes[0].nodeValue</childNode>
<childNode forMC="this is childNode 1">childNodes[0].nodeValue</childNode>
</xml>


anything that is contained in a node becomes a child of the one containing it. Values set between the tags are again a child of that. NodeValue is whatever it sends.

Do an XML.load then Debug the Movie, click on the timeline with the code, view variables tab, and find the XML object.....then read all the branching info there.


----
The delay? Are you meaning the duration?

You can make a node in the xml and read that as a number and spit that into the duration = part.

<appConfig>
<duration>2000</duration>
</appConfig>

 

mclarkson

Originally posted by: Storm
childNodes simply means counting down through the path <...> anything that is contained in a node becomes a child of the one containing it. Values set between the tags are again a child of that. NodeValue is whatever it sends.


I'm sad about how little sense that makes to me. frown

Could you, would you, with a goat?
quote
 

Storm

<tag>

</tag>

you know HTML right?

That's one tag....one node....open and close. Got that far?

 

mclarkson

yesss...

Could you, would you, with a goat?
quote
 

Storm

alrighty....and thanks for showing up to class today!!

let's say in HTML, you insert a body tag.....
<html>
<body>
</body>
</html>

You've just put a node inside a node. Body becomes a child of HTML.

Same with XML.

<data>
<item>
</item>
</data>

item is a childNode of data and if I counted, it would be childNode[0] because it's the firstChild as well. 0 is always the first number in computer integers....which I'm sure you know but I'm saying it anyway!!

stop pulling on Your Mom's nipples in class....I warned you once already!!

you with me still?

 

DontBogartMe

Please sir! Mark Clarkson stole my lunch! He said he didn't but I know he's got it hidden in his beard.

 

Storm

Keep it down back there!!

You know I am not going into that beard to find it! Remember first semester?!!


Did your parents give you any money? You can have my apple then.
I don't trust any of you punk kids anyway, so it's probably laced with strychnine.

 
first 2 last
 

Forums: Flash: ActionScript timer - setInterval?

 
New Post
 
You must be logged in to post