To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 08-28-2006, 04:41 AM   #1
shawntim
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();
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();
		}
	}
}
shawntim is offline   Reply With Quote
Old 08-28-2006, 04:45 AM   #2
shawntim
Junior Member
 
Join Date: Oct 2001
Posts: 21
anyone has any idea how i can solve this problem? greatly appreciated.. thank you!
shawntim is offline   Reply With Quote
Old 08-28-2006, 04:47 AM   #3
FlashLackey
Citizen of the Universe
 
FlashLackey's Avatar
 
Join Date: Aug 2006
Posts: 147
its probably a scope issue.

try this:

PHP Code:
timer_myTest = setInterval(this, mx.utils.Delegate.create(this,mytest), 100);
__________________
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
FlashLackey is offline   Reply With Quote
Old 08-28-2006, 05:09 AM   #4
shawntim
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);
shawntim is offline   Reply With Quote
Old 08-28-2006, 05:10 AM   #5
shawntim
Junior Member
 
Join Date: Oct 2001
Posts: 21
thanks FlashLackey! =)

you beat me to the post heh.
shawntim is offline   Reply With Quote
Old 08-28-2006, 05:14 AM   #6
FlashLackey
Citizen of the Universe
 
FlashLackey's Avatar
 
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:
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.
__________________
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
FlashLackey is offline   Reply With Quote
Old 08-29-2006, 12:18 AM   #7
shawntim
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!
shawntim is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:26 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.