|
|
|
#1 |
|
Junior Member
Join Date: Oct 2001
Posts: 21
|
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(); 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();
}
}
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();
}
}
}
|
|
|
|
|
|
#2 |
|
Junior Member
Join Date: Oct 2001
Posts: 21
|
anyone has any idea how i can solve this problem? greatly appreciated.. thank you!
|
|
|
|
|
|
#3 |
|
Citizen of the Universe
Join Date: Aug 2006
Posts: 147
|
its probably a scope issue.
try this: PHP Code:
__________________
Stop Continental Drift! - Mission Accomplished! - Nation Proved Wrong - "[T]he subsidizing of bad decisions destroys one of the most effective sources of better decisions-- namely, paying the consequences of bad decisions." -Sowell |
|
|
|
|
|
#4 |
|
Junior Member
Join Date: Oct 2001
Posts: 21
|
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); |
|
|
|
|
|
#5 |
|
Junior Member
Join Date: Oct 2001
Posts: 21
|
thanks FlashLackey! =)
you beat me to the post heh. |
|
|
|
|
|
#6 |
|
Citizen of the Universe
Join Date: Aug 2006
Posts: 147
|
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:
__________________
Stop Continental Drift! - Mission Accomplished! - Nation Proved Wrong - "[T]he subsidizing of bad decisions destroys one of the most effective sources of better decisions-- namely, paying the consequences of bad decisions." -Sowell |
|
|
|
|
|
#7 |
|
Junior Member
Join Date: Oct 2001
Posts: 21
|
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! |
|
|
|
![]() |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | |
|
|