问题标题:
AS3.0在函数外怎样同时得到a1,a2,b1,b2参数package{importflash.display.MovieClip;importflash.display.Sprite;importflash.geom.Point;importflash.events.MouseEvent;importflash.events.DataEvent;importflash.utils.Timer;publiccl
问题描述:

AS3.0在函数外怎样同时得到a1,a2,b1,b2参数

package{



importflash.display.MovieClip;

importflash.display.Sprite;

importflash.geom.Point;

importflash.events.MouseEvent;

importflash.events.DataEvent;

importflash.utils.Timer;





publicclassTrowballextendsMovieClip{

privatevarp1:Point;

privatevarp2:Point;

privatevarv1:Number;

privatevarv2:Number;



publicfunctionTrowball(){



varball=newBALL();

addChild(ball);

ball.x=stage.stageHeight/2;

ball.y=stage.stageWidth/2;





functiondown(evt:MouseEvent):void{

evt.target.startDrag();

p1=newPoint(mouseX,mouseY);

//p1.x=mouseX;

//p1.y=mouseY;



vardate:Date=newDate();

vara1:Number=date.seconds;

varb1:Number=date.milliseconds;





trace();

trace("移动前坐标"+p1);

trace("鼠标移动秒数"+a1);

trace("鼠标移动毫秒数"+b1);

}



functionup(evt:MouseEvent):void{

evt.target.stopDrag();

p2=newPoint(mouseX,mouseY);

vardate:Date=newDate();

vara2:Number=date.seconds;;

varb2:Number=date.milliseconds;



trace("移动后坐标"+p2);

trace("鼠标移动秒数"+a2);

trace("鼠标移动毫秒数"+b2);





}







ball.addEventListener(MouseEvent.MOUSE_DOWN,down);

ball.addEventListener(MouseEvent.MOUSE_UP,up);





}

}



}

陆春江回答:
  首先你要明白,函数内的变量是局部变量,只能在函数内调用,函数外是不能调用的,要想让函数内的变量让函数外调用,必须引用其值,你可以声明一个类全局变量,将函数内变量的值传递给这个全局变量就可以了.具体是这样做的:...
刘斌武回答:
  感谢回答我的问题但是我这样输入后在函数外trace里面的函数值输出的都是Number的默认值NAN还是不行啊
陆春江回答:
  package{importflash.display.MovieClip;importflash.display.Sprite;importflash.geom.Point;importflash.events.MouseEvent;importflash.events.DataEvent;importflash.utils.Timer;publicclassTrowballextendsMovieClip{privatevarp1:Point;privatevarp2:Point;privatevarv1:Number;privatevarv2:Number;privatevarga1:Number;privatevargb1:Number;privatevarga2:Number;privatevargb2:Number;publicfunctionTrowball(){//你看你这晨,把所有的类成员都写在了构造函数里了。这是不行的。构造函数是初始化的地方。不应该这样写。varball=newBALL;addChild(ball);ball.x=stage.stageHeight/2;ball.y=stage.stageWidth/2;ball.addEventListener(MouseEvent.MOUSE_DOWN,down);ball.addEventListener(MouseEvent.MOUSE_UP,up);}//在这里加个大花括号,将最後的花括号去掉就可以了。而且函数前面最好加上private。privatefunctiondown(evt:MouseEvent):void{evt.target.startDrag();p1=newPoint(mouseX,mouseY);//p1.x=mouseX;//p1.y=mouseY;vardate:Date=newDate;vara1:Number=date.seconds;ga1=a1;varb1:Number=date.milliseconds;gb1=b1;trace();trace(("移动前坐标"+p1));trace(("鼠标移动秒数"+a1));trace(("鼠标移动毫秒数"+b1));}privatefunctionup(evt:MouseEvent):void{evt.target.stopDrag();p2=newPoint(mouseX,mouseY);vardate:Date=newDate;vara2:Number=date.seconds;varb2:Number=date.milliseconds;ga2=a2;gb2=b2;trace(("移动后坐标"+p2));trace(("鼠标移动秒数"+a2));trace(("鼠标移动毫秒数"+b2));//现在我们来计算一下你所求的值trace("x方向移动了"+(p2.x-p1.x)+"象素。","y方向移动了"+(p2.y-p1.y)+"象素;");trace("鼠标移动的时间(秒):"+(ga2-ga1));trace("鼠标移动的毫秒数:"+(gb2-gb1))}}}这是修改後的类,你的类有问题。其实你是想实现按下鼠标拖动对象,松开鼠标停止拖动,然後获取坐标及时间。
查看更多
其它推荐
热门其它推荐