Skip to main content
SuperGibaLogan
Known Participant
February 11, 2024
Question

(flash 8, as2) how do i make a movieclip orbit around another movieclip?

  • February 11, 2024
  • 1 reply
  • 1055 views

im working on a 2d solar system sandbox game and i need to make the planets orbit the sun, so how do i make them orbit the sun in as2?

This topic has been closed for replies.

1 reply

Vladin M. Mitov
Inspiring
February 11, 2024

To make this example to work, place on the stage movieclip instances, named _sun, _planet1, _planet2, _planet3 and _planet4, then paste the code on frame 1.

 

 

 

 

 

this.stop();

var planets:Object = createSystem( _sun );


function onEnterFrame(){
	for( var p:String in planets ){
		planets[ p ].nextStep();
	}
}


function createPlanetObject( mc:MovieClip, p:MovieClip, s:Number, r:Number ):Object{

	/*
		mc - movieclip to control
		p  - 'parent' movieclip 
		s  - speed (in radians)
		r  - orbit ellipse ratio (1 = circle)
	*/

	var obj:Object = {
		 body		:mc
		,parent		:p || null
		,radiusX	:distance( mc, p )
		,radiusY	:distance( mc, p ) * r
		,speed		:s
		,angle		:0
		,nextStep	:function(){
			this.angle += this.speed;
			this.body._x = this.parent._x + ( this.radiusX * Math.cos( this.angle ) );
			this.body._y = this.parent._y + ( this.radiusY * Math.sin( this.angle ) );
		}		
	};
	return obj;
}


function createSystem( sun:MovieClip ):Object{

	var planets:Object = {};

		planets.mercury = createPlanetObject( _planet1, sun, 0.04, 0.7 );
		planets.venus	= createPlanetObject( _planet2, sun, 0.03, 0.7 );
		planets.earth	= createPlanetObject( _planet3, sun, 0.01, 0.7 );
		
		// Orbits around another 'parent'
		planets.moon = createPlanetObject( _planet4, planets.earth.body, 0.05, 1 );
	
	return planets;
	
}

function distance( mc1:MovieClip, mc2:MovieClip ){
	return Math.sqrt( Math.pow(( mc2._x - mc1._x), 2) + Math.pow( ( mc2._y - mc1._y),  2 ) );
}

 

 

 

 

 

 

 

 

- Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation
SuperGibaLogan
Known Participant
February 11, 2024

the planets arent orbiting around the sun but instead another spot,

how do i make it s the planets always orbit around the sun? and drift off when i delete the sun

SGL
Vladin M. Mitov
Inspiring
February 12, 2024

Perhaps the conversation would be more constructive if you provided a more detailed description of your desired outcome. For instance, the idea that planets must adjust their behavior in the event that the central object they orbit disappears is novel. It might be worth considering the utilization of a 2D physics engine in this scenario?


- Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation