Actionscript 3 - mouse over events

LD147
LD147 used Ask the Experts™
on
I have a movie whereby I have an object in the middle of the movie window, and 10 smaller objects "orbit" it.  This all works fine and dandy (see code).  However, I now need to do something like "if the user mouses over one of the objects, the orbiting for all objects should stop, and do something (like zoom in, animate it, highlight it...something), and then navigate to a URL if the object is actually clicked.

I've been reading about mouse over events in AS3 but I'm having a hard time wrapping my mind around it all.  I'm sure AS3 is better than AS2, but it seems much harder (for a newbie like me anyway).  TIA! :)
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;

obj1.speed = .1;
obj1.radius = 245;
obj1.xcenter = 358;
obj1.ycenter = 258;
obj1.startAngle = 0;
obj1.angle = obj1.startAngle;

obj2.speed = .1;
obj2.radius = 245;
obj2.xcenter = 358;
obj2.ycenter = 258;
obj2.startAngle = 36;
obj2.angle = obj2.startAngle;

obj3.speed = .1;
obj3.radius = 245;
obj3.xcenter = 358;
obj3.ycenter = 258;
obj3.startAngle = 72;
obj3.angle = obj3.startAngle;

obj4.speed = .1;
obj4.radius = 245;
obj4.xcenter = 358;
obj4.ycenter = 258;
obj4.startAngle = 108;
obj4.angle = obj4.startAngle;

obj5.speed = .1;
obj5.radius = 245;
obj5.xcenter = 358;
obj5.ycenter = 258;
obj5.startAngle = 144;
obj5.angle = obj5.startAngle;

obj6.speed = .1;
obj6.radius = 245;
obj6.xcenter = 358;
obj6.ycenter = 258;
obj6.startAngle = 180;
obj6.angle = obj6.startAngle;

obj7.speed = .1;
obj7.radius = 245;
obj7.xcenter = 358;
obj7.ycenter = 258;
obj7.startAngle = 216;
obj7.angle = obj7.startAngle;

obj8.speed = .1;
obj8.radius = 245;
obj8.xcenter = 358;
obj8.ycenter = 258;
obj8.startAngle = 252;
obj8.angle = obj8.startAngle;

obj9.speed = .1;
obj9.radius = 245;
obj9.xcenter = 358;
obj9.ycenter = 258;
obj9.startAngle = 288;
obj9.angle = obj9.startAngle;

obj10.speed = .1;
obj10.radius = 245;
obj10.xcenter = 358;
obj10.ycenter = 258;
obj10.startAngle = 324;
obj10.angle = obj10.startAngle;

obj1.addEventListener(Event.ENTER_FRAME, doMovement);
obj2.addEventListener(Event.ENTER_FRAME, doMovement);
obj3.addEventListener(Event.ENTER_FRAME, doMovement);
obj4.addEventListener(Event.ENTER_FRAME, doMovement);
obj5.addEventListener(Event.ENTER_FRAME, doMovement);
obj6.addEventListener(Event.ENTER_FRAME, doMovement);
obj7.addEventListener(Event.ENTER_FRAME, doMovement);
obj8.addEventListener(Event.ENTER_FRAME, doMovement);
obj9.addEventListener(Event.ENTER_FRAME, doMovement);
obj10.addEventListener(Event.ENTER_FRAME, doMovement);

function doMovement(event:Event):void {
	
	// Move objects
	var currObj:Object = event.currentTarget;
	currObj.x = Math.cos(currObj.angle*Math.PI/180)*currObj.radius+currObj.xcenter;
	currObj.y = Math.sin(currObj.angle*Math.PI/180)*currObj.radius+currObj.ycenter;
	currObj.angle += currObj.speed;
	if(currObj.angle>359) {
		currObj.angle -= 360;
	}
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Engineering Manager / Architect
Commented:
Hello again LD147,

I took the liberty of refactoring a bit of the code, hope you don't mind... if there's anything you want me to elaborate further in, I'll gladly do so. I also have the *.fla available if you'd like to have it.

<code>

-V
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;

var startAngleArray:Array = new Array(0,36,72,108,144,180,216,252,288,324);
var targetArray:Array = new Array("http://www.experts-exchange.com","http://www.google.com","http://www.adobe.com","http://www.somePage.com","http://www.somePage.com","http://www.somePage.com","http://www.somePage.com","http://www.somePage.com","http://www.somePage.com","http://www.somePage.com");
var objArray:Array = new Array();
var totalItems:Number = 10;

for(var i:int = 1;i<=totalItems;i++) {
	var obj:MovieClip = this["obj"+i];
	obj.speed = .1;
	obj.radius = 245;
	obj.xcenter = 358;
	obj.ycenter = 258;
	obj.startAngle = startAngleArray[i-1];
	obj.urlTarget = targetArray[i-1];
	obj.angle = obj.startAngle;
	objArray.push(obj);
	obj.addEventListener(Event.ENTER_FRAME, doMovement);
	obj.addEventListener(MouseEvent.MOUSE_OVER, zoomInAndStop);
	obj.addEventListener(MouseEvent.MOUSE_OUT, zoomOutAndPlay);
	obj.addEventListener(MouseEvent.MOUSE_DOWN, goToTarget);
}

function killMovementAction():void {
	for(var i:int = 0; i<=objArray.length-1;i++) {
		objArray[i].removeEventListener(Event.ENTER_FRAME, doMovement);		
	}
}

function applyMovementAction():void {
	for(var i:int = 0; i<=objArray.length-1;i++) {
		objArray[i].addEventListener(Event.ENTER_FRAME, doMovement);		
	}
}

function doMovement(event:Event):void {
        // Move objects
        var currObj:Object = event.currentTarget;
        currObj.x = Math.cos(currObj.angle*Math.PI/180)*currObj.radius+currObj.xcenter;
        currObj.y = Math.sin(currObj.angle*Math.PI/180)*currObj.radius+currObj.ycenter;
        currObj.angle += currObj.speed;
        if(currObj.angle>359) {
                currObj.angle -= 360;
        }
}

function goToTarget(event:MouseEvent):void {
	var urlRequest:URLRequest = new URLRequest(event.currentTarget.urlTarget);
	navigateToURL(urlRequest,"_blank");
}

function zoomInAndStop(event:MouseEvent):void {
	var currObj:Object = event.currentTarget;
	currObj.scaleX = 1.5;
	currObj.scaleY = 1.5;
	killMovementAction();
}

function zoomOutAndPlay(event:MouseEvent):void {
	var currObj:Object = event.currentTarget;
	currObj.scaleX = 1;
	currObj.scaleY = 1;	
	applyMovementAction();
}

Open in new window

Author

Commented:
Hi Vulturous,

You're my hero! :)  That works really well!  I'm learning more about AS3 each day, and I definitely like the way you coded this, with many functions.  It makes sense.  :)

Author

Commented:
Perfect :)
Antonio EstradaEngineering Manager / Architect

Commented:
I'm glad you find this useful :).

AS3 is fantastic, and it's fun learning it. Good luck with this journey heh. I'll be around if you need assistance.

-V

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial