将简单脚本从 actionscript 2 转换为 actionscript 3
Converting simple script from actionscript2 to actionscript3
我是 as3 的初学者,一直在关注一个简单的 flash 画板脚本,这里是 link > http://www.sitepoint.com/create-flash-sketchpad/。它是用 as2 编码的,但我想将代码转换为 as3.I 我一直在尝试将它们转换为 as3,但我似乎无法使其工作。如果有人能帮助我,我将不胜感激。
createEmptyMovieClip("Line",1);
Line.lineStyle(3,0x000000,300);
onMouseDown = function () {
Line.moveTo(_xmouse, _ymouse);
onMouseMove = function () {
Line.lineTo(_xmouse, _ymouse);
}
}
onMouseUp=function() {
onMouseMove=null;
}
这将在 AS3 中给出:
var Line:Sprite = new Sprite();
addChild(Line);
Line.graphics.lineStyle(3, 0x000000, 1);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
function mouseDownHandler(e:MouseEvent):void {
Line.graphics.moveTo(e.stageX, e.stageY);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
function mouseMoveHandler(e:MouseEvent):void {
Line.graphics.lineTo(e.stageX, e.stageY);
}
function mouseUpHandler(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
我决定创建一个完整的工作示例,以便您了解其工作原理;
package {
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
/**
* ...
* @author Martyn
*/
public class Main extends Sprite {
public var draw:Shape = new Shape();
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
trace("init");
addChild(draw);
draw.graphics.lineStyle(3, 0x000000, 300);
}
private function onMouseDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
draw.graphics.moveTo(mouseX, mouseY);
trace("onMouseDown");
}
private function onMouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
trace("onMouseUp");
}
private function onMouseMove(e:MouseEvent):void
{
draw.graphics.lineTo(mouseX, mouseY);
trace("onMouseMove");
}
}
}
希望这对您有所帮助。
我是 as3 的初学者,一直在关注一个简单的 flash 画板脚本,这里是 link > http://www.sitepoint.com/create-flash-sketchpad/。它是用 as2 编码的,但我想将代码转换为 as3.I 我一直在尝试将它们转换为 as3,但我似乎无法使其工作。如果有人能帮助我,我将不胜感激。
createEmptyMovieClip("Line",1);
Line.lineStyle(3,0x000000,300);
onMouseDown = function () {
Line.moveTo(_xmouse, _ymouse);
onMouseMove = function () {
Line.lineTo(_xmouse, _ymouse);
}
}
onMouseUp=function() {
onMouseMove=null;
}
这将在 AS3 中给出:
var Line:Sprite = new Sprite();
addChild(Line);
Line.graphics.lineStyle(3, 0x000000, 1);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
function mouseDownHandler(e:MouseEvent):void {
Line.graphics.moveTo(e.stageX, e.stageY);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
function mouseMoveHandler(e:MouseEvent):void {
Line.graphics.lineTo(e.stageX, e.stageY);
}
function mouseUpHandler(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
我决定创建一个完整的工作示例,以便您了解其工作原理;
package {
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
/**
* ...
* @author Martyn
*/
public class Main extends Sprite {
public var draw:Shape = new Shape();
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
trace("init");
addChild(draw);
draw.graphics.lineStyle(3, 0x000000, 300);
}
private function onMouseDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
draw.graphics.moveTo(mouseX, mouseY);
trace("onMouseDown");
}
private function onMouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
trace("onMouseUp");
}
private function onMouseMove(e:MouseEvent):void
{
draw.graphics.lineTo(mouseX, mouseY);
trace("onMouseMove");
}
}
}
希望这对您有所帮助。