分享
 
 
 

会跳舞的骨骼小人

王朝other·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

前不久,在一本讲Matlab的书上看到一个例子,用绘图函数画一个小人。

效果如下:

就像下面这样的:

既然这个小人可以画出来,那么可否让他动呢,我想这个很显然是没有问题的,只要把的身体的各个组成部分分解开就可以了。

组成这个小人的只有两种组件成分,一个是圆形,一个是线条.

代码分别如下:

class skelectonCircle

{

private var x:Number=0;

private var y:Number=0;

private var r:Number=0;

private var lineStyleNum:Number=0x3399FF;

static private var mId:Number=0;

private var mMcHolder:MovieClip;

public function skelectonCircle(inX:Number,inY:Number,inR:Number,inStyle:Number)

{

init(inX,inY,inR,inStyle);

}

public function init(inX:Number,inY:Number,inR:Number,inStyle:Number):Void

{

reset(inX,inY,inR,inStyle);

mMcHolder=_root.createEmptyMovieClip("__SKELETONC__"+mId,_root.getNextHighestDepth());

mId++;

}

public function reset(inX:Number,inY:Number,inR:Number,inStyle:Number):Void

{

x=inX;

y=inY;

r=inR;

if(inStyle!=null||inStyle!=undefined)lineStyleNum=0x3399FF;

else lineStyleNum=inStyle;

}

public function draw():Void

{

//draw a cicle:

mMcHolder.lineStyle(1,lineStyleNum,100);

//beginFill(0x6666FF);

//move to the circle’s center.

mMcHolder.moveTo(x+r,y);

var fi:Number=0;

var cX:Number=0;

var cY:Number=0;

var detaFi:Number=0.1;

while(fi<=2*Math.PI+0.2)

{

cX=x+r*Math.cos(fi);

cY=y+r*Math.sin(fi);

mMcHolder.lineTo(cX,cY);

fi+=detaFi;

}

//endFill();

}

public function clean():Void

{

mMcHolder.clear();

}

public function toString():String

{

var reString:String=new String();

reString=String("x:"+String(x)+"y:"+String(y)+"r:"+String(r)+"\n");

return reString;

}

public function finallize():Void

{

mMcHolder.removeMovieClip();

mMcHolder=null;

delete this;

}

}

class skelectonLine

{

private var x1:Number=0;

private var y1:Number=0;

private var x2:Number=0;

private var y2:Number=0;

private var lineStyleNum:Number=0x3399FF;

static private var mId:Number=0;

private var mMcHolder:MovieClip;

//static private var thisP:Object;

public function skelectonCircle(inX1:Number,inY1:Number,inX2:Number,inY2:Number,inStyle:Number)

{

init(inX1,inY1,inX2,inY2,inStyle);

}

public function init(inX1:Number,inY1:Number,inX2:Number,inY2:Number,inStyle:Number):Void

{

reset(inX1,inY1,inX2,inY2,inStyle);

mMcHolder=_root.createEmptyMovieClip("__SKELETONL__"+mId,_root.getNextHighestDepth());

mId++;

//thisP=this;

}

public function reset(inX1:Number,inY1:Number,inX2:Number,inY2:Number,inStyle:Number):Void

{

x1=inX1;

y1=inY1;

x2=inX2;

y2=inY2;

if(inStyle!=null||inStyle!=undefined)lineStyleNum=0x3399FF;

else lineStyleNum=inStyle;

}

public function draw():Void

{

//draw a line:

mMcHolder.lineStyle(1,lineStyleNum,100);

mMcHolder.moveTo(x1,y1);

mMcHolder.lineTo(x2,y2);

}

public function clean():Void

{

mMcHolder.clear();

}

public function toString():String

{

var reString:String=new String();

reString=String("x1:"+String(x1)+"y1:"+String(y1)+"x2:"+String(x2)+"y2:"+String(y2)+"\n");

return reString;

}

public function finallize():Void

{

mMcHolder.removeMovieClip();

mMcHolder=null;

delete this;

}

}

接着,我们定义一个小人类,一共有11个部分组成,具体是什么你一看便知:

class skelectonPerson

{

public var mHead:skelectonCircle;

public var mLeftShoulder:skelectonLine;

public var mRightShoulder:skelectonLine;

public var mUpBody:skelectonLine;

public var mDownBody:skelectonLine;

public var mLeftHand:skelectonLine;

public var mRightHand:skelectonLine;

public var mLeftThigh:skelectonLine;

public var mRightThigh:skelectonLine;

public var mLeftLeg:skelectonLine;

public var mRightLeg:skelectonLine;

public function skelectonPerson()

{

mHead=new skelectonCircle();

mLeftShoulder=new skelectonLine();

mRightShoulder=new skelectonLine();

mUpBody=new skelectonLine();

mDownBody=new skelectonLine();

mLeftHand=new skelectonLine();

mRightHand=new skelectonLine();

mLeftThigh=new skelectonLine();

mRightThigh=new skelectonLine();

mLeftLeg=new skelectonLine();

mRightLeg=new skelectonLine();

}

public function draw():Void

{

mHead.draw();

mLeftShoulder.draw();

mRightShoulder.draw();

mUpBody.draw();

mDownBody.draw();

mLeftHand.draw();

mRightHand.draw();

mLeftThigh.draw();

mRightThigh.draw();

mLeftLeg.draw();

mRightLeg.draw();

}

public function clean():Void

{

mHead.clean();

mLeftShoulder.clean();

mRightShoulder.clean();

mUpBody.clean();

mDownBody.clean();

mLeftHand.clean();

mRightHand.clean();

mLeftThigh.clean();

mRightThigh.clean();

mLeftLeg.clean();

mRightLeg.clean();

}

}

然后就是将这个小人的各个部分组装起来了:

var testPerson:skelectonPerson=new skelectonPerson();

testPerson.mHead.init(180,30,20);

testPerson.mLeftShoulder.init(140,80,180,80);

testPerson.mRightShoulder.init(180,80,220,80);

testPerson.mLeftHand.init(110,40,140,80);

testPerson.mRightHand.init(250,40,220,80);

testPerson.mUpBody.init(180,50,180,80);

testPerson.mDownBody.init(180,80,180,120);

testPerson.mLeftThigh.init(180,120,150,150);

testPerson.mRightThigh.init(180,120,210,150);

testPerson.mLeftLeg.init(150,150,150,200);

testPerson.mRightLeg.init(210,150,210,200);

最后,让我们以一个小人的手在挥动的动作来结束实验1:

感觉这个实验里最让我头痛的就是Flash正切角的计算方式了,搞了半天才算ok,呵呵~~: )

//调用段代码

testPerson.draw();

/* ----test 1:data init.----*/

handLen=Math.sqrt((110-140)*(110-140)+(40-80)*(40-80));

//left hand data.

x0[0]=110;

y0[0]=40;

//left hand axes center

x1[0]=140;

y1[0]=80;

//right hand data.

x0[1]=250;

y0[1]=40;

//right hand axes center

x1[1]=220;

y1[1]=80;

fi0=eAtan2(x0[0]-x1[0],y0[0]-y1[0]);

fi1=eAtan2(x0[1]-x1[1],y0[1]-y1[1]);

detaFi0=0.05;

detaFi1=-0.05;

setInterval(handMove,200);

/*---end of test 1---*/

//主运动函数:

//skelecton person test1:

function handMove()

{

//left hand move.

if(fi0>=-2*Math.PI/3||fi0<=-5*Math.PI/6)

detaFi0*=-1;

x0[0]=x1[0]+_root.handLen*Math.cos(fi0);

y0[0]=y1[0]+_root.handLen*Math.sin(fi0);

_root.testPerson.mLeftHand.reset(x0[0],y0[0],x1[0],y1[0]);

_root.testPerson.mLeftHand.clean();

_root.testPerson.mLeftHand.draw();

fi0+=detaFi0;

//right hand move

if(fi1>=-Math.PI/6||fi1<=-Math.PI/3)

detaFi1*=-1;

x0[1]=x1[1]+_root.handLen*Math.cos(fi1);

y0[1]=y1[1]+_root.handLen*Math.sin(fi1);

_root.testPerson.mRightHand.reset(x0[1],y0[1],x1[1],y1[1]);

_root.testPerson.mRightHand.clean();

_root.testPerson.mRightHand.draw();

fi1+=detaFi1;

}

testPerson.draw();

/* ----test 1:data init.----*/

handLen=Math.sqrt((110-140)*(110-140)+(40-80)*(40-80));

//left hand data.

x0[0]=110;

y0[0]=40;

//left hand axes center

x1[0]=140;

y1[0]=80;

//right hand data.

x0[1]=250;

y0[1]=40;

//right hand axes center

x1[1]=220;

y1[1]=80;

fi0=eAtan2(x0[0]-x1[0],y0[0]-y1[0]);

fi1=eAtan2(x0[1]-x1[1],y0[1]-y1[1]);

detaFi0=0.05;

detaFi1=-0.05;

setInterval(handMove,200);

/*---end of test 1---*/

//主运动函数:

//skelecton person test1:

function handMove()

{

//left hand move.

if(fi0>=-2*Math.PI/3||fi0<=-5*Math.PI/6)

detaFi0*=-1;

x0[0]=x1[0]+_root.handLen*Math.cos(fi0);

y0[0]=y1[0]+_root.handLen*Math.sin(fi0);

_root.testPerson.mLeftHand.reset(x0[0],y0[0],x1[0],y1[0]);

_root.testPerson.mLeftHand.clean();

_root.testPerson.mLeftHand.draw();

fi0+=detaFi0;

//right hand move

if(fi1>=-Math.PI/6||fi1<=-Math.PI/3)

detaFi1*=-1;

x0[1]=x1[1]+_root.handLen*Math.cos(fi1);

y0[1]=y1[1]+_root.handLen*Math.sin(fi1);

_root.testPerson.mRightHand.reset(x0[1],y0[1],x1[1],y1[1]);

_root.testPerson.mRightHand.clean();

_root.testPerson.mRightHand.draw();

fi1+=detaFi1;

}

testPerson.draw();

/* ----test 1:data init.----*/

handLen=Math.sqrt((110-140)*(110-140)+(40-80)*(40-80));

//left hand data.

x0[0]=110;

y0[0]=40;

//left hand axes center

x1[0]=140;

y1[0]=80;

//right hand data.

x0[1]=250;

y0[1]=40;

//right hand axes center

x1[1]=220;

y1[1]=80;

fi0=eAtan2(x0[0]-x1[0],y0[0]-y1[0]);

fi1=eAtan2(x0[1]-x1[1],y0[1]-y1[1]);

detaFi0=0.05;

detaFi1=-0.05;

setInterval(handMove,200);

/*---end of test 1---*/

//主运动函数:

//skelecton person test1:

function handMove()

{

//left hand move.

if(fi0>=-2*Math.PI/3||fi0<=-5*Math.PI/6)

detaFi0*=-1;

x0[0]=x1[0]+_root.handLen*Math.cos(fi0);

y0[0]=y1[0]+_root.handLen*Math.sin(fi0);

_root.testPerson.mLeftHand.reset(x0[0],y0[0],x1[0],y1[0]);

_root.testPerson.mLeftHand.clean();

_root.testPerson.mLeftHand.draw();

fi0+=detaFi0;

//right hand move

if(fi1>=-Math.PI/6||fi1<=-Math.PI/3)

detaFi1*=-1;

x0[1]=x1[1]+_root.handLen*Math.cos(fi1);

y0[1]=y1[1]+_root.handLen*Math.sin(fi1);

_root.testPerson.mRightHand.reset(x0[1],y0[1],x1[1],y1[1]);

_root.testPerson.mRightHand.clean();

_root.testPerson.mRightHand.draw();

fi1+=detaFi1;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有