views:

157

answers:

4

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 ?

+3  A: 

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.

Paul Bevis
+4  A: 

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);
Shiki
And you can leave out 'this'.
bitc
i'd go a step further and create a class var btn:Btn = new Btn(btnExample);btn.addEventListener(...
maxmc
A: 

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

back2dos
I've also read somewhere that "with" in AS3 has performance problems.
Shiki
@Shiki: it shouldn't when properties are accessed in a strictly typed manner, but otherwise property resolution becomes rather expensive of course.
back2dos
+2  A: 

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.

Theo