Link to home
Start Free TrialLog in
Avatar of Brant Snow
Brant Snow

asked on

Need to kill and reinitialize onEnterFrame in actionscript 2.0

K I attached the code below.  Look all the way to the bottom.  Basically i have a carosel that rotates around according to the mouse function, just look at the bottom of the code.  What I need to do is have the carosel stop or slow down if someone moves their mouse outside of the flash box.  I sent a on event handler is their mouse nears the border to decrease the speed but how do i kill the Event Handler at a certain point so it doesnt eat up cpu memory and how do I reinitialize it when the user puts their mouse back over the swf so that if they live again it will slow down again instead of the event handler just being dead from the first time.
import mx.utils.Delegate;
import mx.transitions.Tween;
import mx.transitions.easing.*;
 
var numOfItems:Number;
var radiusX:Number = 300;
var radiusY:Number = 40;
var centerX:Number = Stage.width / 2;
var centerY:Number = Stage.height / 2;
var speed:Number = 0.01;
var speeddecrease:Number = 2500;
var speeddecrease2:Number = 5000;
var perspective:Number = 60;
var home:MovieClip = this;
theText._alpha = 0;
theText.embedFonts = true;
 
var tooltip:MovieClip = this.attachMovie("tooltip","tooltip",10000);
tooltip._alpha = 0;
 
var xml:XML = new XML();
xml.ignoreWhite = true;
 
xml.onLoad = function()
{
	var nodes = this.firstChild.childNodes;
	numOfItems = nodes.length;
	for(var i=0;i<numOfItems;i++)
	{
		var t = home.attachMovie("item","item"+i,i+1);
		t.angle = i * ((Math.PI*2)/numOfItems);
		t.onEnterFrame = mover;
		t.toolText = nodes[i].attributes.tooltip;
		t.content = nodes[i].attributes.content;
		t.destination = nodes[i].attributes.destination;
		t.icon.inner.loadMovie(nodes[i].attributes.image);
		t.r.inner.loadMovie(nodes[i].attributes.image);
		t.icon.onRollOver = over;
		t.icon.onRollOut = out;
		t.icon.onRelease = released;
	}
}
 
function over()
{
	//BONUS Section
	var sou:Sound = new Sound();
	sou.attachSound("sover");
	sou.start();
	
	home.tooltip.tipText.text = this._parent.toolText;
	home.tooltip._x = this._parent._x;
	home.tooltip._y = this._parent._y - this._parent._height/2;
	home.tooltip.onEnterFrame = Delegate.create(this,moveTip);
	home.tooltip._alpha = 100;
}
 
function out()
{
	delete home.tooltip.onEnterFrame;
	home.tooltip._alpha = 0;
}
 
function released()
{
	//BONUS Section
	var sou:Sound = new Sound();
	sou.attachSound("sdown");
	sou.start();
 
	
 
	home.tooltip._alpha = 0;
	for(var i=0;i<numOfItems;i++)
	{
		var t:MovieClip = home["item"+i];
		t.xPos = t._x;
		t.yPos = t._y;
		t.theScale = t._xscale;
		delete t.icon.onRollOver;
		delete t.icon.onRollOut;
		delete t.icon.onRelease;
		delete t.onEnterFrame;
		if(t != this._parent)
		{
			var tw:Tween = new Tween(t,"_xscale",Strong.easeOut,t._xscale,0,1,true);
			var tw2:Tween = new Tween(t,"_yscale",Strong.easeOut,t._yscale,0,1,true);
			var tw3:Tween = new Tween(t,"_alpha",Strong.easeOut,100,0,1,true);
		}
		else
		{
			var tw:Tween = new Tween(t,"_xscale",Strong.easeOut,t._xscale,100,1,true);
			var tw2:Tween = new Tween(t,"_yscale",Strong.easeOut,t._yscale,100,1,true);
			var tw3:Tween = new Tween(t,"_x",Strong.easeOut,t._x,200,1,true);
			var tw4:Tween = new Tween(t,"_y",Strong.easeOut,t._y,170,1,true);
			var tw5:Tween = new Tween(theText,"_alpha",Strong.easeOut,0,100,1,true);
			theText.htmlText = t.content;
			var s:Object = this;
			tw.onMotionStopped = function()
			{
				s.onRelease = unReleased;
			}
		}
	}
}
 
function unReleased()
{
	//BONUS Section
	var sou:Sound = new Sound();
	sou.attachSound("sdown");
	sou.start();
	
	delete this.onRelease;
	var tw:Tween = new Tween(theText,"_alpha",Strong.easeOut,100,0,0.5,true);
	for(var i=0;i<numOfItems;i++)
	{
		var t:MovieClip = home["item"+i];
		if(t != this._parent)
		{
			var tw:Tween = new Tween(t,"_xscale",Strong.easeOut,0,t.theScale,1,true);
			var tw2:Tween = new Tween(t,"_yscale",Strong.easeOut,0,t.theScale,1,true);
			var tw3:Tween = new Tween(t,"_alpha",Strong.easeOut,0,100,1,true);
		}
		else
		{
			var tw:Tween = new Tween(t,"_xscale",Strong.easeOut,100,t.theScale,1,true);
			var tw2:Tween = new Tween(t,"_yscale",Strong.easeOut,100,t.theScale,1,true);
			var tw3:Tween = new Tween(t,"_x",Strong.easeOut,t._x,t.xPos,1,true);
			var tw4:Tween = new Tween(t,"_y",Strong.easeOut,t._y,t.yPos,1,true);
			tw.onMotionStopped = function()
			{
				for(var i=0;i<numOfItems;i++)
				{
					var t:MovieClip = home["item"+i];
					t.icon.onRollOver = Delegate.create(t.icon,over);
					t.icon.onRollOut = Delegate.create(t.icon,out);
					t.icon.onRelease = Delegate.create(t.icon,released);
					t.onEnterFrame = mover;
				}
			}
		}
	}
}
 
 
function moveTip()
{
	home.tooltip._x = this._parent._x;
	home.tooltip._y = this._parent._y - this._parent._height/2;
}
 
xml.load("icons.xml");
 
function mover()
{
	this._x = Math.cos(this.angle) * radiusX + centerX;
	this._y = Math.sin(this.angle) * radiusY + centerY;
	var s = (this._y - perspective) /(centerY+radiusY-perspective);
	this._xscale = this._yscale = s*100;
	this.angle += this._parent.speed;
	this.swapDepths(Math.round(this._xscale) + 100);
}
 
this.onMouseMove = function()
{
	
	speed = (this._xmouse-centerX)/speeddecrease;
	
		
}
 
this.onEnterFrame = function()
{
	if (this._xmouse < 50 or this._xmouse > 930 or this._ymouse < 40 or this._ymouse > 260)
	{speed = speed - (speed/15);
	}
	
}

Open in new window

Avatar of blue-genie
blue-genie
Flag of South Africa image

why don't you try using a mouselistener instead of enterframe.
ASKER CERTIFIED SOLUTION
Avatar of julianopolito
julianopolito
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial