You are viewing the archives of the original Pixelfumes blog. This blog is no longer maintained. All of the content available on this site is available on the new Pixelfumes blog in addition to all of our new posts! Visit: http://blog.pixelfumes.com.

« Home | Apple is Punishing me for Buying an iPod Touch Whe... » | Happy 2008! Free PixelStyles Pro for Eight of You... » | Flickr API and Flashloaded's New gridNavigation Co... » | The Mac vs. PC War is Still Raging - CNET Ad » | New Adobe AIR Logo » | Pixelfumes Snow Class Used in a TV Commercial » | AFComponents Using the Pixelfumes Reflect Framewor... » | AIR Version of the Stacks Class » | AS3 OSX Leopard-like "Stacks" Class with Source » | AS3 Problem Targeting MovieClips Containing Nested... » 

Wednesday, January 23, 2008 

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!

Looks great. Very cool to play with. The reflection is off though.

Looks great. Very fun to play with. The reflection is off a bit though.

Looks sweet, but yeah - the reflections is having a hard time keepin' up :)

Yea, thats strange... I just tossed my Reflection class on there for effect.

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...

Very interesting, I think this would be nicer with a bouncing ball, thanks , Keep Kool, Peace

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?

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);

hi
i have another question. Could swinger has button function? I mean when click it will load external swf file. Is it possible?
Thanks

You could modify the class to handle the click event by adding a click event listener to it, sure.

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.

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.

Great effect.
How can i do the rolling number effect?

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

Post a Comment