Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 07-05-2005, 06:13 PM   #1
dr_nick_riviera
Registered User
 
Join Date: Jul 2005
Posts: 2
Default Dynamically Moving an Object Along a Circle Motion Guide

Hi everybody,

I'm trying to move an object along circlular motion guide dynamically using two buttons -- a left button, when pressed down, will move the object counter clockwise, and the right button clockwise.

Does anyone know how I can achieve this?

Thanks,

-Dr.NickRiviera
dr_nick_riviera is offline   Reply With Quote
Old 02-19-2009, 10:14 PM   #2
anticipatient
Registered User
 
Join Date: Feb 2009
Posts: 16
Default

Well you can create circular motion using sin for y position and cos for x position.

Where radX and radY are the radius of the circle (use different values to make an oval), and centerX and centerY are the coordinates for the center of the circle, your code would look like this:
ActionScript Code:
var angle:Number = 0; var radX:Number = 50; var radY:Number = 50; var centerX:Number = 200; var centerY:Number = 200; var orbitSpeed:Number = .05; onEnterFrame(){ object._x = centerX+Math.cos(angle)*radX; object._y = centerY+Math.sin(angle)*radY; var angle += orbitSpeed; }

If you're going to use the right mouse button in a flash movie, you should disable the context menu that pops up when you right click. I forget how to do that, but it shouldn't be too hard for you to find.

That aside, though, one way you could do this (and there may be better ways -- I'm a beginner) is an if() statement within the onEnterFrame function:
ActionScript Code:
var LMB:Boolean = false; onMouseDown = function(){ LMB = true; } onEnterFrame(){ if(LMB == true){     object._x = centerX+Math.cos(angle)*radX;     object._y = centerY+Math.sin(angle)*radY; } }

To make it stop moving when you let the button up, just add:
ActionScript Code:
onMouseUp = function(){ LMB = false; }

I don't know how to use the right mouse button, but say it was "onRightMouseDown," you'd add this:
ActionScript Code:
var RMB:Boolean = false; onRightMouseDown = function(){ RMB = true; LMB = false; }

And then you should also add, under onMouseDown,
ActionScript Code:
RMB = false;

Finally, you'd have to change things so that when the right button was pressed, the circle would go the other way. That would be as simple as switching "sin" and "cos"

I am new, so there may be mistakes in this, but the whole thing should look like this (there's probably an extra bracket or missing bracket in here too, so watch out

ActionScript Code:
var angle:Number = 0; var radX:Number = 50; var radY:Number = 50; var centerX:Number = 200; var centerY:Number = 200; var orbitSpeed:Number = .05; var LMB:Boolean = false; var RMB:Boolean = true; onMouseDown = function(){ LMB = true; RMB = false; } onRightMouseDown = function(){ LMB = false; RMB = true; } onEnterFrame(){ if(LMB == true){ object._x = centerX+Math.cos(angle)*radX; object._y = centerY+Math.sin(angle)*radY; var angle += orbitSpeed; } if(RMB == true){ object._x = centerX+Math.sin(angle)*radX; object._y = centerY+Math.cos(angle)*radY; var angle += orbitSpeed; } }
anticipatient is offline   Reply With Quote
Old 02-20-2009, 01:02 AM   #3
Xegnma
Member
 
Join Date: Sep 2007
Posts: 84
Default

A simple approach is to create a container clip for your object, embed the object in the container via attachMovie and then rotate the container clip.
You can do this using the authoring environment or dynamically using the code below.
Code:
var radius:Number = 10;

var container:MovieClip = this.createEmptyMovieClip("_container", this.getNextHighestDepth());
container.attachMovie("objectLinkageID","instanceName",container.getNextHighestDepth(), {_x:radius});
To set up your buttons you could do the following:
Code:
var increment:Number = 4;

cwButton.onPress = function(): Void {
    _parent.onEnterFrame = function(): Void {
        container._rotation += increment;
    }
}

ccwButton.onPress = function():Void {
    _parent.onEnterFrame = function(): Void {
        container._rotation -= increment;
    }
}

cwButton.onRelease = ccwButton.onRelease = function(): Void {
    delete _parent.onEnterFrame;
}
Xegnma is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:21 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.