关于AS 1.0、AS 2.0和java

2005-11-27 17:36:06

关于ActionScript 1.0、ActionScript 2.0、java的区别与联系——基于一个实例的讨论 问题:创建两个位置大小颜色各不同的可拖拽的矩形。 方法一:method1.fla //ActionScript 1.0 //Flash Player 6(含)以上 //ActionScript 1.0或ActionScript 2.0均可 [code] createEmptyMovieClip ("rectangle1_mc", 1); rectangle1_mc._x = 10; rectangle1_mc._y = 10; with (rectangle1_mc) { beginFill (0xff0000, 100); moveTo (0, 0); lineTo (200, 0); lineTo (200, 200); lineTo (0, 200); endFill (); } rectangle1_mc.onPress = startDrag; rectangle1_mc.onRelease = rectangle1_mc.onReleaseOutside = stopDrag; // createEmptyMovieClip ("rectangle2_mc", 2); rectangle2_mc._x = 50; rectangle2_mc._y = 50; with (rectangle2_mc) { beginFill (0xffcc00, 100); moveTo (0, 0); lineTo (400, 0); lineTo (400, 100); lineTo (0, 100); endFill (); } rectangle2_mc.onPress = startDrag; rectangle2_mc.onRelease = rectangle2_mc.onReleaseOutside = stopDrag; [/code] 方法二:method2.fla //ActionScript 2.0 //Flash Player 6(含)以上 //必须为ActionScript 2.0 [code] var rect1:Rectangle = new Rectangle (this, 10, 10, 200, 200, 0xff0000, 1); var rect2:Rectangle = new Rectangle (this, 50, 50, 400, 100, 0xffcc00, 2); delete rect1; delete rect2; delete Rectangle; [/code] 此方法必须得有一个Rectangle.as与之同路径。Rectangle.as内容如下: [code] class Rectangle extends MovieClip { private var rectangle_mc:MovieClip; public function Rectangle (target:MovieClip, x:Number, y:Number, w:Number, h:Number, color:Number, depth:Number) { rectangle_mc = target.createEmptyMovieClip ("rectangle_mc" + depth, depth); rectangle_mc._x = x; rectangle_mc._y = y; with (rectangle_mc) { beginFill (color, 100); moveTo (0, 0); lineTo (w, 0); lineTo (w, h); lineTo (0, h); endFill (); } rectangle_mc.onPress = startDrag; rectangle_mc.onRelease = rectangle_mc.onReleaseOutside = stopDrag; } } [/code] 方法三:method3.java //如果.java也可以直接编译.swf的话 [code]public class method3{ public static void main(String[] args){ Rectangle rect1 = new Rectangle (this, 10, 10, 200, 200, 0xff0000, 1); Rectangle rect2 = new Rectangle (this, 50, 50, 400, 100, 0xffcc00, 2); //这里就不需要手动delete了,因为java有垃圾回收器,它会自动清除。 } } class Rectangle extends MovieClip { private MovieClip rectangle_mc; public Rectangle (MovieClip target, Double x, Double y, Double w, Double h, Double color, Double depth) { rectangle_mc = target.createEmptyMovieClip ("rectangle_mc" + depth, depth); rectangle_mc._x = x; rectangle_mc._y = y; with (rectangle_mc) { beginFill (color, 100); moveTo (0, 0); lineTo (w, 0); lineTo (w, h); lineTo (0, h); endFill (); } rectangle_mc.onPress = startDrag; rectangle_mc.onRelease = rectangle_mc.onReleaseOutside = stopDrag; } } 通过比较不难发现,ActionScript 1.0最灵活、最简单、最容易理解。但重复类似的操作时也会最繁琐。如果要画1000个位置大小颜色各不同的矩形的话,不累死也会被笑死! ActionScript 2.0则非常系统,单独用Rectangle.as定义了一个矩形类,使用时new即可,爽!有点小小的遗憾的是有几行delete,当然你也可以不加,让它永远霸占你的内存。 再看看java,看完了又看,看完了再看,呵呵! 啥也不说了,谁老大肯定有人知道。 [/code] [swf]http://www.blueidea.com/articleimg/2005/10/2864/method2.swf[/swf]