I cant figure how to create an earth revolution around the sun and in the same moment moon orbiting earth. I can only create simple earth revolution around the sun (without moon in motion). Could anyone help me?
//Declaring moon and earth angle, this will cause rotation; var earthAngle:Number=0; var moonAngle:Number=0;
//Distance of earth from the sun; const earthDistance:int=96;
//Distance from moon from the earth; const moonDistance:int=24;
addEventListener(Event.ENTER_FRAME,updateMovie);
//Updating the movie; function updateMovie(e:Event):void { //Adding a little amount of rotation for orbit; earthAngle+=.02; moonAngle+=.08; //This will make the earth and moon orbits properly. earth.x=sun.x+(Math.cos(earthAngle)*earthDistance); earth.y=sun.y+(Math.sin(earthAngle)*earthDistance); moon.x=earth.x+(Math.cos(moonAngle)*moonDistance); moon.y=earth.y+(Math.sin(moonAngle)*moonDistance); }
The code is in AS3 and is just a sample, anyway if you have any problem with it just ask to me and I will explain to you every detail of the script
Would you show me how to create such system without AS?
I'm not really the best at animation, but you could try to animate the planet going in a circle using motion guides. The only problem with this is that the moon and earth will be treated as on object, and the hit boxes will be screwed up if you don't preform pixel perfect hit tests.
I think Carlytoon posted an excellent piece of code, but if you want to stay away from code, you could use timeline tweens, or as master565 suggested motion guides (which is essentially an elaborate timeline tween). Since it's my suggestion, I'll try and explain the tweening approach.
Basically, you'll want a nested structure, meaning you have movieclips inside movieclips. Add the sun movieclip, and inside it place the earth movieclip. The pivot point of the earth movieclip should be centered on the sun, but the actual movieclip itself should be at the right orbit, ofcourse. This way, you can rotate it around the sun. So, make a new keyframe at -say- frame 30, and rotate the earth 360 degrees. Next you right-click the frames in between the keyframes, and add a classic tween. If you scroll back and forth, you should see the earth rotating around the sun!
(By the way, if you want to avoid having a duplicate frame, which makes it seem like the animation pauses for a little while, right-click on the frame before the second keyframe, and add a keyframe there. Then remove the last keyframe, so that when the animation is at it's end and goes back to the beginning it doesn't show two frames with the earth at the same rotation)
Doing the moon is essentially the same, only inside the earth movieclip. Just add it, move the pivot point to the center of the earth, rotate, add keyframes and a tween.
Note that this does not include any realistic rotations of earth or the moon, if you want it very realistic I'd really recommend using code since it gives you a lot more control
I FAILED. I add movie clip (planet), then motion path. Afterwards I click this symbol and draw new symbol (moon) inside and new motion path. I tried about 30 times in different ways, no result.. Fun is over it is just depressing now.
Welcome to beginner programming and normal programming to a degree.
Keep trying, superation needs effort and constancy, I hope you can do it
Yep I agree. The code is pretty simple except for that math but if you just take it for what it is then even that isn't hard. It's not like you're doing derivatives.
Oh, I forgot to write that I finally did it! You can download the file here. Sun earth moon revolution PS I hope I am not breaking any rules of the nforum.