将简单的Flash AS2代码转换为AS3

时间:2011-03-24 20:46:14

标签: flash actionscript-3 actionscript-2

我是Flash初学者并遵循教程:http://www.webwasp.co.uk/tutorials/018/tutorial.php ...学习如何制作“现场绘画/画画”效果。我没有意识到,如果我在AS2中做了一些东西,我就无法将它(并且让它工作)嵌入到我的根AS3文件中,在那里我还有其他一些东西在继续。我已经尝试过如何将AS2代码更改为AS3的提示,但它不起作用。我知道这是简单的代码,那里的一些天才可以解决它,但我不知所措。请帮忙!

这是AS2代码:

_root.createEmptyMovieClip("myLine", 0);

_root.onMouseDown = function() {
   myLine.moveTo(_xmouse, _ymouse);
   ranWidth = Math.round((Math.random() * 10)+2);
   myLine.lineStyle(ranWidth, 0xff0000, 100);
   _root.onMouseMove = function() {
      myLine.lineTo(_xmouse, _ymouse);
   }
}

_root.onMouseUp = function() {
   _root.onMouseMove = noLine;
}

1 个答案:

答案 0 :(得分:6)

这是AS3中完全相同的事情

import flash.display.Sprite;
import flash.events.MouseEvent;

var ranWidth:Number;

//creation of a new clip (Sprite is the base class of MovieClip, 
//it's the same without the unneeded timeline)
var myLine:Sprite = new Sprite();
addChild(myLine);

//in AS3 the main container is stage, not _root
//you see here the main difference between beginner AS3 and beginner AS2: 
//event listeners
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); 


function onMouseDown(event:MouseEvent):void
{
    myLine.graphics.moveTo(mouseX, mouseY);
    ranWidth = Math.round((Math.random() * 10)+2);
    myLine.graphics.lineStyle(ranWidth, 0xff0000, 100);
    stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}

//nesting function in other functions is not a good practice
function onMouseMove(event:MouseEvent):void
{
    myLine.graphics.lineTo(mouseX, mouseY);
}

function onMouseUp(event:MouseEvent):void
{
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}