setInterval() and object method

Hi all,

Is there any way I can execute object’s method using setInterval() without instantiating new instance? For example,

var newInstance = new baseObject();
setInterval(newInstance.someMethod,100);

Thanks

May I ask what you are trying to do? There might be an alternative way other than setInterval.

With setInterval you need to call a function. So you could define the instance name within a parameter of the function.

Hi lostinbeta,

I was just trying to make a small preloader component which I will reuse here and there. I could use for loop or while loop to do the same thing but I thought it would be clearer to use setInterval method. My code goes something like this;

[AS]function Preloader(xPosition,yPosition,itsWidth,itsHeight){
this._x = xPosition;
this._y = yPosition;
this._width = itsWidth;
this._height = itsHeight;
_root.createEmptyMovieClip(“loader_mc”,1);
}
Preloader.prototype.createPreloader = function(){
loaded = _root.getBytesLoaded();
total = _root.getBytesTotal();
percent = Math.round( loaded/total );
wide = (preload._width * percent)/100;
loader_mc.beginFill(0x333333,100);
loader_mc.moveTo(this._x,this._y);
loader_mc.lineTo(wide+this._x,this._y);
loader_mc.lineTo(wide+this._x,this._height+this._y);
loader_mc.lineTo(this._x,this._height+this._y);
loader_mc.lineTo(this._x,this._y);
loader_mc.endFill();
loader_mc.clear();
}
var preload = new Preloader(100,100,200,3);
setInterval(preload.createPreloader,50);
/*
while(_root.getBytesLoaded() < _root.getBytesTotal()){
loaded = _root.getBytesLoaded();
total = _root.getBytesTotal();
percent = Math.round( loaded/total );
wide = (preload._width * percent)/100;
preload.createPreloader(wide);
}
*/
[/AS]

Oh man, you’re going all OOPy on me :-\ I shamefully admit I am 100% horrible at OOP.

But the thing is, you don’t really need OOP to do such a project. And you can easily use the onEnterFrame dynamic event handler instead of setInterval.

A prototype preloader like this has already been created, you can check it out in the Best of Kirupa section and use it as a base example.

http://www.kirupaforum.com/forums/showthread.php?s=&threadid=6794

Im sorry I wasn’t much help, but I neglected to realize in your first post and by the title that this was OOP related. I can understand OOP reading it, but writing it for me is a completely different story. And that makes debugging it just as bad.

I feel crummy now :frowning:

Thanks lostinbeta,

I guess I was not that off the track. I havent programmed in OOP for a while so this is pretty challenge for me too. AS is so similar to Java which is my favorite but there exists subtle diference and that difference confuse me sometime. I’ll post my version of preloader as soon as it’s done.

Oooohhh, Java, I always wanted to learn Java. I know Javascript, but thats like nothing…lol. I can’t OOP in that very well either. Im OOP illiterate I guess you can say, but I am self taught, so maybe thats why, I don’t know.

One tip I can actually debug with your code now is this…

Regular functions cannot use the “this” locator. That is prototype function specific, you will have to define the location statically, or as a parameter within your function… something like…

[AS]function Preloader(myMC, xPosition,yPosition,itsWidth,itsHeight){

    myMC._x = xPosition;
    myMC._y = yPosition;
    myMC._width = itsWidth;
    myMC._height = itsHeight;
    _root.createEmptyMovieClip("loader_mc",1);

}[/AS]

Thats a good tip. Thanks:)

No problem, glad I could help with something here …lol. :slight_smile:

OOP??? What’s OOP??? OOPs??? I am OOP hater… :evil:

OOP can be useful, if you know what your doing. But I don’t, so I guess I am with you on that one Cyan :evil:

:trout: :stuck_out_tongue:

Yup… I was totally lost and wasted ever since I saw pointer in C++… :evil:

I dont like pointer either
but C++ is ok. I hate C. C should be gone by now.

Um… C is less confusing than C++… Better language for somebody with small brain like me… :evil:

well, maybe C’s confusing to me because I never built a complete system using C. I tried once but it seemed hard to reuse code so I quit. C++ and java did the pretty good job so I didnt care much about C. Now more and more languages are available to us. Php,AS,Brew… Do they ever stop creating new standard and just let us live a normal life? I like AS much better than C and C++ anyways.

Do they ever stop creating new standard and just let us live a normal life?
I wish… :sigh: But then, if they do, life will be too boring… :beam:
I like AS much better than C and C++ anyways.
Ditto!!! Yet, I gotta add Lingo in front of AS…

Hehe… I can see that you are pretty much organized and well-prepared person just by going over the languages you like, and I see that I am totally opposite… Oh, well… I’ll survive… :wink:

I like AS better than C and C++ too. Oh wait, I don’t know those, I want to learn C++ and Java though. But AS’s downfall is that it doesn’t process information very efficiently. Java seems to do a much better job. When using multiple mathematical equations it doesn’t take much for Flash to be too CPU intensive, but i’ve seen Java applets and programs that do so much more without being half as CPU intensive. So that is kind of a drag.

Hey lostinbeta,

Java used be a major CPU sucker and now its getting better and better. I dont even realize it’s run on the VM. AS will surely follow the same path. Until then, we just sharpen our brains to optimize code.

I’ve got the new question for you. It’s still object related so I won’t launch a new thread. I have a component of preloader here. It’s packed in one class with couple of useful methods. How could I use this as external object or something like API does? I dont want to put the code in every fla.

flashcomponents.net put out a few VERY useful tutorials on how to create components. I haven’t really tried any of them, but I hear good things about them. Perhaps one of the tutorials lets you figure out how to install a component so you can select it from the components panel.

http://www.flashcomponents.net/tutorials.cfm?nav=4

I sooo wish I had pointers in Flash :frowning:

btw, for more on setInterval see
http://www.umbc.edu/interactive/flash/tutorials/setInterval.php