0

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 ?

1
  • I agree with s.harvey, you can't just make up things and expect that it will work. Commented May 11, 2010 at 1:45

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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);

2 Comments

And you can leave out 'this'.
i'd go a step further and create a class var btn:Btn = new Btn(btnExample); btn.addEventListener(...
3

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.

Comments

0

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

3 Comments

I've also read somewhere that "with" in AS3 has performance problems.
@Shiki: it shouldn't when properties are accessed in a strictly typed manner, but otherwise property resolution becomes rather expensive of course.
@Richards: It means that at compile time the compiler has knowledge of the type of the property owner, such that the known type defines the property. Consider class A { public var a:int; }. And now var a:A = new A(); var o:* = a; In this case a.a will perform significantly better than o.a, because the compiler (in that case the JIT) can calculate the property offset from the information it has, while in the second example it has to be resolved through runtime reflection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.