从ActionScript 2到ActionScript 3.0

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit.

我知道过渡可能很难。 我们已经习惯了ActionScript 2的易用性,但是老实说,后来它变得成问题了,特别是如果设计人员参与了该项目,并发现可以轻松添加合适的代码。

So, this article takes the basic operations we all did in AS2 and brings them to AS3.

因此,本文采用了我们在AS2中所做的所有基本操作,并将它们带入了AS3。

So what happened to my on(release) or something.onRelease statements?

那么我的on(release)或something.onRelease语句发生了什么?

The on(EVENT) clause was an AS1 relic, but AS2 kept it for backwards functionality. The biggest problem with this is that it made the code appear all over the project and finding it could prove to be a big challenge. The Object.ONEVENT clause proved to be better but people could still define the function afterwards and if it was big enough it could be difficult to read the code.

on(EVENT)子句是AS1的遗物,但是AS2保留它是为了向后功能。 这样做的最大问题是,它使代码遍及整个项目,并且发现这可能是一个很大的挑战。 事实证明Object.ONEVENT子句更好,但是人们仍然可以在以后定义该函数,如果它足够大,则可能很难读取代码。

//AS1
on(release) {
    trace(this); //would return _level0.myButton (or _level0.instanceXX if it didn't have a name)
}
//AS2
myButton.onRelease = function() {
    trace(this); //would return something like _level0.myButton
}

For AS3, we need listener objects, the listener objects are "listening" to the object they are assigned to, waiting for them to execute an event. If the listener is expecting a certain type of event, it will fire the function assigned to it.

对于AS3,我们需要侦听器对象,这些侦听器对象正在“侦听”分配给它们的对象,等待它们执行事件。 如果侦听器期望某种事件类型,它将触发分配给它的函数。

//AS3
myButton.buttonMode = true; //makes the cursor a hand, otherwise it shows the default arrow
myButton.addEventListener(MouseEvent.MOUSE_DOWN, buttonPressed);

function buttonPressed(event:MouseEvent):void {
    trace(event.currentTarget); //returns "[Object MovieClip]"
    trace(event.currentTarget.name); //returns "myButton"
}

Undefined Variables?

未定义的变量?

AS2 was extremely forgiving. You could not define any variable and the code would still work. This was useful but was also a nightmare when a project grew too much. It's not the case anymore for AS3, where you must define the variable and it's type (although, there's still the wildcard "*" for any type).

AS2非常宽容。 您无法定义任何变量,并且代码仍然可以运行。 这很有用,但当项目增长过多时,也是一场噩梦。 AS3不再是这种情况,您必须定义变量及其类型(尽管对于任何类型,通配符“ *”仍然存在)。

//AS2
total="";
for(i=0;i<=5;i++) {
	total+=i;
}
newvar = total;
trace(total); //returns 012345
trace(newvar); //returns 012345

We never defined the type of anything, we assumed that total was a string, but what if we wanted to actually add and not just concatenate? In AS3 we can't do that anymore.

我们从未定义任何东西的类型,我们假设total是一个字符串,但是如果我们想实际添加而不只是连接怎么办? 在AS3中,我们不能再这样做了。

//AS3
var total:String = "";
var i:Number = 0;
var newvar:String = "";
for(i=0;i<=5;i++) {
	total+=i;
}
newvar = total;
trace(total); //returns 012345
trace(newvar); //returns 012345

We have to define the type of the variable before being able to continue.

我们必须先定义变量的类型,然后才能继续。

And what about getURL?

那getURL呢?

A lot of people have asked me about that one. It was a simple yet useful function we used to have.

很多人问我这个问题。 这是我们过去拥有的简单但有用的功能。

//AS2
myButton.onRelease = function() {
   getURL("http://www.experts-exchange.com");
}

So, what happened with this in AS3? Well it got a pretty major overhaul.

那么,在AS3中发生了什么? 好吧,它进行了重大的大修。

//AS3
myButton.addEventListener(MouseEvent.MOUSE_DOWN, buttonPressed);

function buttonPressed(event:MouseEvent):void {
	var urlRequest:URLRequest = new URLRequest("http://www.experts-exchange.com");
	navigateToURL(urlRequest);
}

This is just a small tidbit of AS3 to give you a starting point. There are countless other changes like the removal of attachMovie, loadMovie, createEmptyMovieClip, and many others that I'll write about in the next article.

这只是AS3的一个小窍门,可以为您提供一个起点。 还有无数其他更改,例如删除了attachMovie,loadMovie,createEmptyMovieClip,以及我将在下一篇文章中介绍的许多其他更改。

-V

-V

翻译自: https://www.experts-exchange.com/articles/702/From-ActionScript-2-To-ActionScript-3-0.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值