将代码从 Actionscript 2 转换为 Actionscript 3
我有从朋友那里得到的一小段代码,但我无法将其转换为工作的 AS3.0。无论我进行什么更改,我都会不断收到编译器错误。 这是原始代码,我非常感谢您能看一下它。
laser_nodes = 2;
for (var x=1; x<=laser_nodes; x++) {
node = _root.attachMovie("laser", "laser_"+x, x, {_x:Math.random()*460+20, _y:Math.random()*310+20});
node.onPress = function() {
startDrag(this);
};
node.onRelease = function() {
stopDrag();
};
}
_root.createEmptyMovieClip("ray", _root.getNextHighestDepth());
ray.onEnterFrame = function() {
this.clear();
this.lineStyle(3, 0xff0000);
this.moveTo(_root.laser_1._x, _root.laser_1._y);
for (x=2; x<=laser_nodes; x++) {
this.lineTo(_root["laser_"+x]._x, _root["laser_"+x]._y);
}
this.lineTo(_root.laser_1._x, _root.laser_1._y);
};
I have this small piece of code I got from a friend, but i can't manage to translate it into working AS3.0. I keep getting compiler errors no matter what i change.
This is the original piece of code and I would really appreciate you tailing a look at a it.
laser_nodes = 2;
for (var x=1; x<=laser_nodes; x++) {
node = _root.attachMovie("laser", "laser_"+x, x, {_x:Math.random()*460+20, _y:Math.random()*310+20});
node.onPress = function() {
startDrag(this);
};
node.onRelease = function() {
stopDrag();
};
}
_root.createEmptyMovieClip("ray", _root.getNextHighestDepth());
ray.onEnterFrame = function() {
this.clear();
this.lineStyle(3, 0xff0000);
this.moveTo(_root.laser_1._x, _root.laser_1._y);
for (x=2; x<=laser_nodes; x++) {
this.lineTo(_root["laser_"+x]._x, _root["laser_"+x]._y);
}
this.lineTo(_root.laser_1._x, _root.laser_1._y);
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有很多问题。有些是句法上的,有些则需要新的方法。
例如:
_root
在 AS3 中不存在。在 AS3 中,它变为:MovieClip(root)
attachMovie
在 AS3 中不可用,您必须将其替换为像var node 这样的构造函数调用=新激光(); ...
onPress
和onRelease
回调在 AS3 中不受支持。您需要研究将addEventListener
与MouseEvent
类一起使用。与onEnterFrame
相同 (Event.ENTER_FRAME
)createEmptyMovieClip()
变为new MovieClip();
AS3 中的图形绘制命令现在嵌套在
graphics
对象中看来你需要深入研究一下 AS3。这不是一段需要转换的非常直接的代码。
There are a lot of issues here. Some are syntactical, where others require new methods.
for instance:
_root
does not exist in AS3. In AS3 it becomes:MovieClip(root)
attachMovie
is not available in AS3, you'll have to replace it with a constructor call likevar node = new laser(); ...
onPress
andonRelease
callbacks are not supported in AS3. you'll need to look into using theaddEventListener
w/ theMouseEvent
class. Same withonEnterFrame
(Event.ENTER_FRAME
)createEmptyMovieClip()
becomesnew MovieClip();
the graphic drawing commands in AS3 are now nested in the
graphics
object of Sprites.Seems like you'll need to dig into AS3 a little bit for this. It's not a very straight forward bit of code to convert.