View Full Version : hanging by a thread...
achisholm
02-06-2007, 04:57 PM
Hi, i'm wanting to create the effect where an object is hanging by a piece of string. I would really like it to gently sway in the breeze and perhaps move more dramatically when rolled over.
Any help on creating this effect?
thanks,
Alistair
Well, this is not exactly what you are describing, but it might help. It is the beginnings of a dynamic yoYo that I am playing with.
It is all code, so just paste it into a oneFrame file and run it. Make sure you give it at least 30 fps though.
var swayRight:Boolean=true;
var swayLeft:Boolean=false;
var startPoint:Number=(Stage.width/2)+40;
var endPoint:Number=(Stage.width/2)-40;
var midPoint:Number=Stage.width/2;
var swingSpeed:Number = 2;
//
function createYoYo(){
var cir:MovieClip = this.createEmptyMovieClip("circMC", this.getNextHighestDepth());
cir.lineStyle(30, 0xFF00FF, 100);
cir.lineTo(.5,.5);
cir._y=Stage.height/2;
cir._x=Stage.width/2;
}
//
function swayYoYo(){
//horizontal swing
if (this.circMC._x>=startPoint){swayLeft=true;swayRight=false};
if (this.circMC._x<=endPoint){swayLeft=false;swayRight=true};
if (swayLeft==true){this.circMC._x-=swingSpeed;}
else if (swayRight==true){this.circMC._x+=swingSpeed;};
//vertical motion
if ((this.circMC._x<=midPoint)&&(swayLeft==true)){
this.circMC._y-=.5
}
else if ((this.circMC._x<=midPoint)&&(swayLeft==false)){
this.circMC._y+=.5
}
if ((this.circMC._x>=midPoint)&&(swayLeft==true)){
this.circMC._y+=.5
}
else if ((this.circMC._x>=midPoint)&&(swayLeft==false)){
this.circMC._y-=.5
}
//Draw String
var line:MovieClip = this.createEmptyMovieClip("lineMC", 1);
line.lineStyle(1, 0xFF0000, 100);
line.moveTo(Stage.width/2, 0);
line.lineTo(this.circMC._x,this.circMC._y-15);
}
createYoYo();
onEnterFrame=function(){
swayYoYo();
if (swayNum>10){swayNum-=.5}
if (swingSpeed>2){swingSpeed-=.5}
}
var swayNum:Number = 1
this.circMC.onRollOver = function(){
swayNum++
swingSpeed+=swayNum
}
achisholm
02-07-2007, 10:51 AM
thanks a lot, that is excellent!
I definately think i could use this to help me create what i'm after with a bit of tweaking here and there.
The idea is that there will be little stars hanging from the sky.
I've replaced the circle you had as the yoyo and inserted a movieclip (a star).
I have a couple of questions:
I'm wanting to tone down the swaying effect making it a bit more subtle. I'm sure i could figure this out myself actually, i'l have a play about with it.
I want about 6 of these things hanging down. What's the best way to have multiple stars?
Also I see it's using the 'onEnterFrame' command. Is running 6 of these simultanuously going to be alright on the cpu (there wll be other stuff going on as well)
One more thing. At the start I want to have the star slowly come down from the sky as if it's being lowered into the picture. Would this be easy to do?
Thanks again for sharing your yoyo code, it was just the thing i was looking for, well done.
The swaying effect was controlled by 3 things; the swingSpeed, and the margins. The further apart the margins, the further apart it will swing, the speed is obvious... Those are all declared at the very beginning of the script just for this purpose. (start and endpoint are your margins.)
Right now I have them set to the center of the stage +/- 40 pixels in each direction.
var startPoint:Number = (Stage.width/2)+40;
var endPoint:Number = (Stage.width/2)-40;
var midPoint:Number = (Stage.width/2);
var swingSpeed:Number = 2;
As far as multiple stars...thats a whole other animal. I don't think it would be any problem on the processor to run this code on multiple stars, but it wasn't designed that way as is.
To do it for multiple stars you would have to either:
A.) copy this entire script change the coordinates and variable names for everything as needed, and paste both of them into the same onEnterFrame.
b.) Recode it so it isn't looking for a specific star, but an array of them.(or just multiple ones)
As far as lowering it slowly down...that would be no big deal, you would just have to _y++ it onEnterFrame till it got where you wanted it. The only issue there might be that some variables might need to get played with to be more dynamic to the y start point.
I should mention though, this script is still a work in progress and has all kinds of bugs. It slowly creeps upwards as it gets going fast, and I haven't gotten it to swing in the correct direction based on how you hit it yet. I am still working on it though...
vBulletin® v3.7.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.