|
-
using setInterval in objects
hi everyone,
I'm using Flash8. Hope you can help with my problem. Thank you!
I'm trying to create an setInterval on a Stock object. When i create a new instance of it in the Main timeline, it works.
However, I need to manage a few instances of these "Stock" objects, so I created a Manager class, and I create new instances of Stock object in the constructor of the Manager class. I create a new instance of the Manager class in the main timeline. The setInterval now does not work.
I suspect it could be a special object reference, but i've tried passing in "this", "_root[ID]" (ID is the unique name of my Stock object).. just doesn't work.
my code:
Frame 1 in Main
Code:
// creates 1st rectangle
var me0:Object = new Stock(10, 10);
// animates 1st rectangle
me0.playTV();
// 2nd rectangle is created and animated in the constructor of Manager class
var m = new Manager();
Manager.as
Code:
class Manager{
public function Manager() {
trace("Manager Created");
//2nd rectangle created here
var me1:Object = new Stock(10, 70);
//2nd rectangle animate here
me1.playTV();
}
}
Stock.as
Code:
import mx.transitions.Tween;
import mx.transitions.easing.*;
class Stock{
var ID:String;
var randomFactor:Number;
var xCoord:Number;
var yCoord:Number;
var iMCL:MovieClipLoader;
var timer_myTest:Number;
public function Stock(iX, iY) {
xCoord = iX;
yCoord = iY;
// force Unique Names/ID for each movie clip, otherwise will overwrite one another.
randomFactor = 1000000000000000;
ID = Math.floor((Math.random()*randomFactor))%randomFactor + "";
_root.createEmptyMovieClip(ID, _root.getNextHighestDepth());
_root[ID]._x = xCoord;
_root[ID]._y = yCoord;
drawRectangle(_root[ID], 50, 50, 0xCCFF00, 60);
}
public function getID():String { return ID; };
public function playTV() {
trace("** now playing **");
// somehow not working for me1
timer_myTest = setInterval(this, "mytest", 100);
}
private function mytest() {
trace("TEST this.ID = " + ID);
var twn_myTest = new Tween(_root[ID], "_x", Regular.easeIn, xCoord, xCoord+100, 1, true);
twn_myTest.onMotionFinished = function() {
trace("TEST setInterval - DONE");
}
clearInterval(timer_myTest);
}
private function drawRectangle(target_mc:MovieClip, boxWidth:Number, boxHeight:Number, fillColor:Number, fillAlpha:Number):Void {
with (target_mc) {
beginFill(fillColor, fillAlpha);
moveTo(0, 0);
lineTo(boxWidth, 0);
lineTo(boxWidth, boxHeight);
lineTo(0, boxHeight);
lineTo(0, 0);
endFill();
}
}
}
-
anyone has any idea how i can solve this problem? greatly appreciated.. thank you!
-
Hood Rich
its probably a scope issue.
try this:
PHP Code:
timer_myTest = setInterval(this, mx.utils.Delegate.create(this,mytest), 100);
"We don't estimate speeches." - CBO Director Doug Elmendorf
-
hi guys,
i manage to solve the problem already, thanks to a good Delegate Tutorial from
http://www.actionscript.org/tutorial...ss/index.shtml
quoted from Actionscript.ORG
Another neat way in which you can use Delegate is with setInterval. Internally when functions are called from an interval the scope is sometimes not passed along, so that trace(this); will show undefined. You can properly impose a scope on an interval using the following actioncript:
import mx.utils.Delegate;
function traceThis()
{
trace(this);
}
var myInt = setInterval(Delegate.create(this, traceThis), 1000);
-
thanks FlashLackey! =)
you beat me to the post heh.
-
Hood Rich
no problem. glad you got it going. im guessing you've just started using classes. you'll find now that you will use Delegate all over the place.
for what its worth, heres my favorite delegate trick:
PHP Code:
mc.onEnterFrame = mx.utils.Delegate.create(this, function ()
{
// code here.
});
makes it easier to group nested functions within functions in classes rather than making a bunch of dependent private functions.
"We don't estimate speeches." - CBO Director Doug Elmendorf
-
thanks much, FlashLackey.
I've been doing Java for a couple of years and now picking up Actionscript. Your latest example really saved my skin!
I was cracking my head the whole night over how to use inner functions in classes as they don't work and the object reference isn't passed into it.
Do you have any more Delegate examples to share? Thanks much!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|