Swinger Class with Source
I am trying to get back into experimenting with different things since I have been so lazy about updates on the blog. Today I finished up a proof of concept that swings a MovieClip on an axis point. In the demo below you can play with the result. If you are interested in downloading the code you can do so here. I got the original code math formula from Blog About Flash. They provided a really nice and efficient way to handle the pendulum motion by simply using the Math.cos() method. My Swinger class basically takes that formula and allows you to work with some interaction and friction settings. As always - should you wish to add to the code let me know and I will post your updates. Hope you enjoy it.

This could be handy someday, I'm going to download and take a look at the code. Thanks!
Posted by
MikeTheVike |
12:06 PM
Looks great. Very cool to play with. The reflection is off though.
Posted by
Erik |
5:08 PM
Looks great. Very fun to play with. The reflection is off a bit though.
Posted by
Erik |
5:09 PM
Looks sweet, but yeah - the reflections is having a hard time keepin' up :)
Posted by
JBOY |
10:07 AM
Yea, thats strange... I just tossed my Reflection class on there for effect.
Posted by
Sarge |
12:48 PM
The reflection gets out of sync from the swinger because it is being updated on a interval timer and the swinger is changing the clip rotation in a tween (and then then reflection update happens 'some time' later).
It looks like the reflection was added as an after effect but just for kicks I made a version with changes to have the swinger class optionally create the reflection itself, and if so then to handle the update of the reflection whenever the swinger actually moves.
http://timmaffett.com/source/swingerSyncedReflection.html
sources here: http://timmaffett.com/source/swingerSyncedReflection.zip
the changers were minor and easy because the code was nice and clean, well architected...
Posted by
Tim |
1:09 PM
Very interesting, I think this would be nicer with a bouncing ball, thanks , Keep Kool, Peace
Posted by
Adérito |
5:22 PM
thanks this help me a lot, but i have a question. How can i apply to several objects? i want to use the swing effect on my buttons how can i apply it to other three?
Posted by
tzuchieh |
6:13 PM
Multiple objects would be easy and might look something like this:
import com.pixelfumes.Swinger;
var s:Swinger = new Swinger(shape_mc);
var s2:Swinger = new Swinger(another_mc);
Posted by
Ben |
10:57 PM
hi
i have another question. Could swinger has button function? I mean when click it will load external swf file. Is it possible?
Thanks
Posted by
tzuchieh |
1:46 PM
You could modify the class to handle the click event by adding a click event listener to it, sure.
Posted by
Ben |
2:04 PM
This is great thank you. I'm going to see if I can mod it to react when another mc hits it - as part of a kind of chain-reaction - or domino effect - animation I'm working on.
Posted by
Flash Developer Perth |
12:18 AM
Hi, this is excellent. I have some questions. How to make object swing without mouse interaction?
I want to make one object swing on load and after few seconds to stop slowly.
Thanks.
Posted by
Sasa, Sanja i Oleg |
7:44 AM
Great effect.
How can i do the rolling number effect?
Posted by
iliketo |
9:31 AM
AS3 converted version:
package com.pixelfumes
{
import flash.display.*;
import flash.events.*;
import fl.transitions.*;
import fl.transitions.easing.*;
import flash.utils.*;
import flash.net.*;
import flash.text.TextField;
public class SwingerAS3 extends MovieClip
{
private var clip:MovieClip;
private var force:Number = 20;
private var _force:Number = force;
private var n:Number = 0;
private var _n:Number = n;
private var friction:Number = .5;
private var _friction:Number = friction;
private var interactionOrigin:String = "left";
private var wait:Boolean = true;
private var kickTween:Tween;
public function SwingerAS3(m:MovieClip)
{
clip = m;
clip.addEventListener(MouseEvent.MOUSE_OVER, swing);
//clip.onRollOver = Delegate.create(this,swing);
}
public function setParamaters(o:Object):void
{
clip.removeEventListener(Event.ENTER_FRAME, renderSwing);
//delete clip.onEnterFrame;
kickTween.stop();
clip.rotationZ = 0;
force = o.force;
friction = o.friction;
_force = force;
_friction = friction;
wait = o.wait;
n = 0;
}
public function swing(event:MouseEvent):void
{
if (n == 0 || wait == false)
{
interactionOrigin = getFromDirection();
force = _force;
n = _n;
friction = _friction;
if (interactionOrigin == "left")
{
force = _force * -1;
kickTween = new Tween(clip,"rotationZ", Regular.easeOut, clip.rotationZ, Math.cos(n) * force,.2,true);
kickTween.addEventListener(TweenEvent.MOTION_FINISH, render);
//kickTween.onMotionFinished = Delegate.create(this,render);
}
else
{
force = _force;
kickTween = new Tween(clip,"rotationZ",Regular.easeOut,clip.rotationZ,Math.cos(n) * force,.2,true);
kickTween.addEventListener(TweenEvent.MOTION_FINISH, render);
//kickTween.onMotionFinished = Delegate.create(this,render);
}
clip.removeEventListener(Event.ENTER_FRAME, renderSwing);
//delete clip.onEnterFrame;
}
}
private function render(event:TweenEvent):void
{
kickTween.stop();
clip.addEventListener(Event.ENTER_FRAME, renderSwing);
//clip.onEnterFrame = Delegate.create(this,renderSwing);
}
public function getMomentum():String
{
trace(n);
return n.toString();
}
private function renderSwing(event:Event):void
{
//http://www.blogaboutflash.com/2007/02/using-mathcos-to-make-object-swing.html
n += 0.3;
clip.rotationZ = Math.cos(n)*force;
if (interactionOrigin=="left")
{
if (force-friction<0)
{
force+=friction;
}
else
{
n=0;
clip.removeEventListener(Event.ENTER_FRAME, renderSwing);
//delete clip.onEnterFrame;
}
}
else
{
if (force-friction>0)
{
force-=friction;
}
else
{
n=0;
clip.removeEventListener(Event.ENTER_FRAME, renderSwing);
//delete clip.onEnterFrame;
}
}
}
private function getFromDirection():String
{
if (clip.mouseX<0)
{
return "left";
}
else
{
return "right";
}
}
}
}// end package
Posted by
Ayman |
6:37 PM