AS3:如何简化Action Script 3代码?

发布于 2024-09-01 04:36:45 字数 407 浏览 3 评论 0原文

这是一个当我想创建具有鼠标悬停效果的按钮时必须使用的示例:

    this.buttonExample.buttonMode = true;
    this.buttonExample.useHandCursor = true;
    this.buttonExample.addEventListener(MouseEvent.CLICK,myaction);

我是 AS3 新手 - 有什么方法可以像这样简化此代码:

    this.buttonExample.buttonMode = true;.useHandCursor = true;.addEventListener(MouseEvent.CLICK,myaction);

为什么它不起作用?

Here's a example that I've to use when I want to create a button with mouse-over effect:

    this.buttonExample.buttonMode = true;
    this.buttonExample.useHandCursor = true;
    this.buttonExample.addEventListener(MouseEvent.CLICK,myaction);

I'm new to AS3 - is there any way, to simplify this code like this:

    this.buttonExample.buttonMode = true;.useHandCursor = true;.addEventListener(MouseEvent.CLICK,myaction);

why does it not works ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

明媚殇 2024-09-08 04:36:45

它已经很简单了。首先,

this.buttonExample.buttonMode = true;
this.buttonExample.useHandCursor = true;
this.buttonExample.addEventListener(MouseEvent.CLICK,myaction)

更具可读性

this.buttonExample.buttonMode = true;.useHandCursor = true;.addEventListener(MouseEvent.CLICK,myaction);

它比始终追求可读性胜过其他任何事情 。其次,

this.buttonExample.buttonMode = true; 

不返回对象,因此您无法与任何东西交互。

Its already as simple as it gets. Firstly

this.buttonExample.buttonMode = true;
this.buttonExample.useHandCursor = true;
this.buttonExample.addEventListener(MouseEvent.CLICK,myaction)

is much more readable than

this.buttonExample.buttonMode = true;.useHandCursor = true;.addEventListener(MouseEvent.CLICK,myaction);

Always go for readbility over anything else. And secondly,

this.buttonExample.buttonMode = true; 

does not return an object so you can't interact with anything.

如果您经常使用该模式,您可以创建一个辅助函数:

public function setAsButton(button:Sprite, clickHandler:Function):void {
  button.buttonMode = button.userHandCursor = true;
  button.addEventListener(MouseEvent.CLICK, clickHandler);
}

然后在某处调用它:

setAsButton(this.buttonExample, myaction);

If you're using that pattern a lot, you can make a helper function:

public function setAsButton(button:Sprite, clickHandler:Function):void {
  button.buttonMode = button.userHandCursor = true;
  button.addEventListener(MouseEvent.CLICK, clickHandler);
}

Then call it somewhere:

setAsButton(this.buttonExample, myaction);
ˉ厌 2024-09-08 04:36:45

如果您觉得一遍又一遍地输入 this.buttonExample 过于重复,只需将该对象分配给一个变量并在其余语句中使用该变量:

var b : Button = this.buttonExample;
b.buttonMode = true;
b.useHandCursor = true;
b.addEventListener(...);

正如其他人提到的,还有 < code>with 语句,但不鼓励使用它,因为它使代码更难阅读,并可能导致奇怪的结果:

with (this.buttonExample) {
  buttonMode = true;
  useHandCursor = true;
  addEventListener(...);
}

当然,您可以将这些建议与其他技巧结合起来,例如链接赋值:

var b : Button = this.buttonExample;
b.buttonMode = b.useHandCursor = true;
b.addEventListener(...);

Be 非常如果分配的值是不可变(例如truefalse、数字和字符串),则仅以这种方式链接分配,但不是数组或大多数其他对象),因为相同对象将被分配给左侧的所有变量。如果该值是不可变的,这并不重要,但如果它是可变的,您最终可能会得到奇怪的结果,如本例所示:

 a = b = [ ];
 a.push(1);
 b.push(2);
 trace(a); // outputs 1, 2
 trace(b); // also outputs 1, 2

此结果的原因是 ab< /code> 都引用相同数组,并且由于数组是可变的,因此无论您如何访问该对象,它仍然会发生更改。 ab 不会仅仅因为它们是不同的变量而引用不同的数组。

您可能认为可以执行以下操作,但它行不通

// this will NOT work
var b : Button = this.buttonExample;
(b.buttonMode = b.useHandCursor = true).addEventListener(...);

之所以可以说 b.buttonMode = b.useHandCursor = true,但不能添加 .addEventListener(...) 是因为赋值表达式的值(例如b.buttonMode = true)是分配给左侧的值(例如true)。如果您添加 .addEventListener(...) ,您实际上是在说 true.addEventListener(...) ,这显然不是您想要的。换句话说,

b.buttonMode = b.useHandCursor = false;

相当于

b.useHandCursor = false;
b.buttonMode = b.useHandCursor;

希望也能够使上述警告变得简单。

If you feel that typing this.buttonExample over and over again is too repetitive, simply assign that object to a variable and use that variable in the rest of the statements:

var b : Button = this.buttonExample;
b.buttonMode = true;
b.useHandCursor = true;
b.addEventListener(...);

As other's have mentioned, there's also the with statement, but it's use is discouraged since it makes the code harder to read, and may lead to weird results:

with (this.buttonExample) {
  buttonMode = true;
  useHandCursor = true;
  addEventListener(...);
}

You can, of course, combine these suggestions with other tricks, like chaining assignments:

var b : Button = this.buttonExample;
b.buttonMode = b.useHandCursor = true;
b.addEventListener(...);

Be very careful to only chain assignments in this way if the assigned value is immutable (e.g. true, false, numbers and strings, but not arrays or most other objects), because the same object will be assigned to all variables on the left side. If the value is immutable this doesn't matter, but if it's mutable you can end up with weird results, like this in this example:

 a = b = [ ];
 a.push(1);
 b.push(2);
 trace(a); // outputs 1, 2
 trace(b); // also outputs 1, 2

The reason for this result is that a and b both reference the same array, and since arrays are mutable it doesn't matter how you access the object, it will still be changed. a and b don't reference different arrays just because they are different variables.

You may think that you could do something like the following, but it will not work.

// this will NOT work
var b : Button = this.buttonExample;
(b.buttonMode = b.useHandCursor = true).addEventListener(...);

The reason why it works to say b.buttonMode = b.useHandCursor = true, but not to add .addEventListener(...) is that the value of an assignment expression (e.g. b.buttonMode = true) is the value assigned to the left hand side (e.g. true). If you add .addEventListener(...) to that you are essentially saying true.addEventListener(...), which clearly is not what you want. In other words

b.buttonMode = b.useHandCursor = false;

is equivalent to

b.useHandCursor = false;
b.buttonMode = b.useHandCursor;

Which should hopefully also make the caveats mentioned above plain.

饮湿 2024-09-08 04:36:45

您可以使用 with声明。但是我不鼓励你这样做,因为这会导致很多歧义和不清楚。

另外,你可以有多个作业:

this.buttonExample.buttonMode = this.buttonExample.useHandCursor = true;

这有时很有用,但为了可读性,你不应该过度使用它。

问候
后退2dos

you can use the with statement. however I'd not encourage you to do so, since it leads to a lot of ambiguity and unclearness.

also, you can have multiple assignments:

this.buttonExample.buttonMode = this.buttonExample.useHandCursor = true;

this sometimes is useful, but for the sake of readability, you shouldn't overuse it.

greetz
back2dos

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文