TwelvestoneFlash

ActionScript timer - setInterval?


Sign in

  • Waiting for Godot ( 720 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 4.8 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.8 k posts )
    general front end design an...
  • Back End ( 9.6 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
Candy Beard
 
2009-06-28

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.

Storm
 
2009-06-29

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=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.

Candy Beard
 
2009-06-29

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. k

Arsis
 
2009-06-29

[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
 
2009-06-29

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
 
2009-06-29

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

Candy Beard
 
2009-06-29

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?

Storm
 
2009-06-29

going backwards....

Yes

Yes

The Latter

:shrug:

k

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."

Candy Beard
 
2009-06-30

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.

Storm
 
2009-06-30

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.

Candy Beard
 
2009-06-30

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?

Storm
 
2009-06-30

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.

Candy Beard
 
2009-07-01

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. :|

Storm
 
2009-07-01

childNodes simply means counting down through the path

childNodes[0].nodeValue childNodes[0].nodeValue

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.

2000

Candy Beard
 
2009-07-01

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. :(

Storm
 
2009-07-01

you know HTML right?

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

Candy Beard
 
2009-07-02

yesss...

Storm
 
2009-07-02

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

let's say in HTML, you insert a body tag.....

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

Same with XML.

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
 
2009-07-02

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

Storm
 
2009-07-02

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.

Candy Beard
 
2009-07-02

Let's say I want to add a delay to my XML, and that my XML currently looks like this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> ad1.jpg ad2.jpg

Would I change it to this? <?xml version="1.0" encoding="utf-8" standalone="yes"?> 6000 ad1.jpg ad2.jpg

And what, exactly, would I do with the AS code that reads this XML to pick up the delay? Showing me that line of code and where it goes will go a long way toward helping me understand where I'm at.

Also, I suspect that I have too many children in my XML setup, because in my original I had both an and a within the tag.

Storm
 
2009-07-03

It's a holiday starting now.....teacher is gone.

aww crap

ok, first of all you're better off separating config information from the images. I would simply add a or tag or something around your duration, that way you can split it up and send it off somewhere later.

And you can never have too many children. What you've got is nothing!

go back to my setInterval idea....

var duration:Number = xml.childNodes[0].etc..etc..nodeValue - depending on what you wnat to do with your xml. Then you use DURATION as that variable in your intervalId = setInterval() thing.

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 6000 ad1.jpg This is ad 1! ad2.jpg This is ad 2!

I even added an id ATTRIBUTE to one image tag just to show what else you can do if you wanted to.

load your xml then split off your config to one object and image list to another

var appConfigData:XML = xml.childNodes[0]; var imageListData:XML = xml.childNodes[1];

and then you can handle smaller chunks in Flash that way.

Candy Beard
 
2009-07-04

Originally posted by: Storm var duration:Number = xml.childNodes[0].etc..etc..nodeValue - depending on what you wnat to do with your xml.

See, that's exactly the part I don't understand, so _.etc..etc..nodeValue - _ means very little to me, and I have no idea how to translate that into code.

Storm
 
2009-07-04

you actually WRITE that

you want the number contained IN the duration tag to be the number used in the duration value of the setInterval call.

Like I said early on........

load your XML, run the movie in Debug mode, find the xml object in the debugger's variables tab and open and close the plus signs to see what the object contains and how to reference each piece.

[edit - i said I wouldn't work OR come here today...oh well]

var duration:Number; // we'll set this later

var dataXML:XML = new XML(); dataXML.ignoreWhite = true; dataXML.onLoad = function(success:Boolean){ if (success) { duration = dataXML.childNodes[0].childNodes[0].childNodes[0].childNodes[0].nodeValue; trace(duration); } else { trace("connection failed"); } }; dataXML.load("data.xml");

you have to COUNT the nodes as they become children.

the xml object you load has one child (hence childNodes[0]) it has 2 children and ...we're looking for config right now (hence childNodes[0] ..the first one) config has 1 child (hence the third childNodes[0]) it has one child which is the value 6000 (hence the fourth childNodes[0] and since you want the value in it (String) and not the node itself (XML) then you add nodeValue.

Try the debugger trick and see if you can follow that.

It's easier to do in AS3 but really isn't too tough once you realize how elementary it is.

You can also build an internal ImagesXML by making it equal to dataXML.childNodes[0].childNodes1

duration = dataXML.childNodes[0].childNodes[0].childNodes[0].childNodes[0].nodeValue;

is in this case saying..... duration = xmlfile.application.config.duration.containerholdingavalue.thevalue6000;

Candy Beard
 
2009-07-06

Originally posted by: Storm load your XML, run the movie in Debug mode, find the xml object in the debugger's variables tab and open and close the plus signs to see what the object contains and how to reference each piece.

My goodness but that would be easier if the debugger didn't insist on closing the tree every time anything happens.

TwelvestoneFlash

ActionScript timer - setInterval?