One of my friend has a site www.magicwebmaster.com, I wondered how the effect of string attached to mouse was done. I started googling around and found the template (www.templatemonster.com/flash-templates/10335.html).
Now I have to really learn how this is done, I thought to myself and started googling more. Finally I found what I was looking for in this site (www.lemlinh.com/flash-source-elastic-string). Happiness didn't last long... I was disappointed after download because the code was in AS1.
Finally after a day I was able to convert it into AS3 and to my requirement but later I found there was one final addition of point was needed to make this perfect.
here is the code after everything.
import flash.display.*;
import flash.events.MouseEvent;
import flash.events.Event;
var haschild:Boolean = false;
var gotonodes:Array = new Array();
var currentnodes:Array = new Array();
gotonodes=Interpolate(stage.mouseX,stage.mouseY,275,0,25);
currentnodes=gotonodes;
stage.addEventListener(Event.ENTER_FRAME, onmove1);
stage.addEventListener(MouseEvent.MOUSE_MOVE , onmove);
function onmove(e:MouseEvent){
gotonodes=Interpolate(stage.mouseX,stage.mouseY,275,0,25);
}
function onmove1(e:Event){
for(var node = 0; node<gotonodes.length-1;node++){
currentnodes[node].xco=currentnodes[node].xco+(gotonodes[node].xco-currentnodes[node].xco)/(node*node/30+1);
currentnodes[node].yco=currentnodes[node].yco+(gotonodes[node].yco-currentnodes[node].yco)/(node*node/30+1);
}
DrawNodes(currentnodes);
}
function FindAngle(x1, x2, y1, y2):Number {
return Math.atan2(y2-y1, x2-x1);
};
function Distance(x1,x2,y1,y2):Number{
return Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
}
function Interpolate(x1,y1,x2,y2,n):Array{
var dist= Distance(x1,x2,y1,y2);
var ang = FindAngle(x1,x2,y1,y2);
var points = [];
for(var l = 0;l <=dist;l+=dist/n){
var x3 =x1+l*Math.cos(ang);
var y3 = y1+l*Math.sin(ang);
points.push({xco:x3,yco:y3});
}
points.push({xco:x1,yco:y1});
return points;
}
function DrawNodes(array):void{
if(haschild)
{
this.removeChildAt(0);
haschild=false;
}
var shape:Shape = new Shape();
shape.graphics.lineStyle(1,0xFFFFFF,40);
shape.graphics.moveTo(array[0].xco,array[0].yco);
for(var i = 0; i < array.length-1;i++){
shape.graphics.lineTo(array[i].xco,array[i].yco);
}
shape.graphics.beginFill(0xFBFFA4,1);
shape.graphics.drawCircle(array[0].xco,array[0].yco,5);
shape.graphics.endFill();
this.addChild(shape);
haschild=true;
}
No comments:
Post a Comment