Make secret blocks transparent when discovered [Solved]


(Zachary Lewis) #21

It’s an in-line conditional. It works much like if (…) else. The code before the ? is the condition (is stepCounter less than 0.5?), the code after is what to do if the condition is true, and the code after the : is what to do if the condition is false.

The last part is more difficult to answer. As a guideline, if you aren’t sure, add the super call.

The only time you shouldn’t include the super call is if you explicitly don’t want to perform the parent functionality. For example, assume you had a Dog class that had a speak() function which played a sound. If you made a RabidDog class extending Dog, it should froth when it speaks, but it should also make a sound, so you only write code to froth, then call super.speak() to play the sound as written in Dog. If you made a DeadDog class extending Dog, it shouldn’t make any sound at all. You’d override speak() to do nothing, making sure to not call super.speak().

And, @jacobalbano beat me to some shit. I guess that’s what I get for using my phone!

Edit: It’s creepy that we both wrote pretty much the same thing. Samsies!


(Jacob Albano) #22

Are you me???


(Lozza JP) #23

I feel like I have so much code I want to refactor now :smile:

Unless it isn’t that big of a deal, is there any performance difference?

Also should a super call be at the end or start of the overridden function?

And great explanations, thanks guys heaps.


(Zachary Lewis) #24

There’s not enough of a performance difference to worry about it.

As to where the call goes, it depends on how your code is written, and super() runs wherever it was called. Take these classes, Foo, BarFoo and FooBar.

public class Foo extends Object {
  protected var count:int = 0;

  public function traceCount():void {
    trace("count =>", count);
  }
}
public class BarFoo extends Foo {
  override public function traceCount():void {
    count++;
    super.traceCount();
  }
}
public class FooBar extends Foo {
  override public function traceCount():void {
    super.traceCount();
    count++;
  }
}

BarFoo.traceCount() Output

count => 1

FooBar.traceCount() Output

count => 0

That said, unless you need the parent functionality before your child’s function, just put it at the end.


(Lozza JP) #25

Perfect explanation thanks.

And protected vars go down to subclasses right? And private remain just for the parent class?

Can you then override protected vars?


(Zachary Lewis) #26

http://greenethumb.com/article/27/public-private-protected-internal-access-modifiers-in-as3/