You can pretty easily wrap it in your own little helper function
Math.abs() is using a lot of memory?
Ternary operators are really cool.
var result:String = value ? "result if value is true" : "result if value is false";
You can also chain 'em!
trace("myInt is", myInt != 0 ? myInt % 2 == 0 ? "even." : "odd." : "zero.");
A lunatic coding wizard who has no regard for the minds of others. I think the general consensus is to avoid it, and if you HAVE to do it, turn the inner ternaries into functions.
I’m alright with them if they have parentheses around each expression, but I try to avoid them myself.
I think that the ternary operators just need some practice. They are not that difficult.
Actually if you think it like and if
and else
(and of ourse else if
) it’s not that hard to understand (and becouse is a one line operator allows you to use it as, for example, a function argument. .
Here the same example with if
and else
. As you can see they use a lot more space (both of them can be coded in one single line).
var result:String;
if(value)
{
result = "result if value is true";
}
else
{
result = "result if value is false";
}
var result:String;
if(myInt != 0)
{
if(myInt % 2 == 0)
{
result = "even.";
}
else
{
result = "odd.";
}
}
else
{
result = "zero.";
}
trace("myInt is ", result);
In that kind of case it’s almost always obvious what it’s doing. The kinds of ternary operations to avoid are things like this:
H = (C == 0 ? null : V == r ? (g - b) / C : V == g ? (b - r) / C + 2 : (r - g) / C + 4);
Yes, that is actual code from a project in an actual production environment.
You can write in another way for the most complex ones (it’s more visual and easier to understand):
H = (
c == 0
? null
: V == r
? (g - b) / C
: (r - g) / C + 4
);
It’s also true that there is almost no difference between using the ternary operator or the if-else sentence. In this kind of big things i would recomend to use if-else sentence.
The ternary operator was made for that small and obvious cases (it’s faster to write the code and easy to understand).
Another gotcha about them is that some languages (most famously PHP) do the associativity in a different order for ternaries. So if you move amongst languages a lot, it can cause some weird bugs.
Thanks for all the replys, i found that rectangle intersects is the best solution as its much faster, speaking about faster methods, which is the best way to draw a line as I know DRAW function is only really for debugging but I cannot get MovieClip to work, it doesn’t render for some reason, any idea why?
If you’re just using FlashPunk and not drawing many lines, the utilities Draw class will be much faster than AS3’s native line drawing (MovieClip).
But I will most likely be drawing quite a few lines, and i want to add filters to them to make them look like lasers. Any suggestion?
A more efficient method of creating a laser
I’d use an Image instead. Make a small slice of your ‘laser’; say, 4x8 pixels, then scale it as appropriate (lineLength / 8) and rotate it. This’ll require some maths but will be the most efficient way to draw lines as it’s just drawing sprites.
Probably draw a sprite, apply X filter, extract its bitmapdata, draw image. I keep getting beaten to it.
(Note that @Ultima2876’s solution is much more efficient in terms of performance, but you’ll have to apply the “effect” on the pre-done image)
Thanks both for the reply from both of you
Probably draw a sprite, apply X filter, extract its bitmapdata, draw image
Is there a example on the internet on how to extract its bitmap data, draw image. Because I have only been drawing images rather than extracting the bitmap data.(is there a advantage of this).
See if this helps:
var sp:Sprite = new Sprite();
sp.graphics.lineStyle(3,0xffffff);
sp.graphics.moveTo(0,0); //Draws line from (0,0)
sp.graphics.lineTo(100,100); //To (100,100)
var glow:GlowFilter = new GlowFilter();
sp.filters = [glow];
var bmd:BitmapData = new BitmapData(sp.width,sp.height); //Probably the bit
bmd.draw(sp); //you're interested in
var img:Image = new Image(bmd);
I’m not sure this works, but it should.
(and no, there is no advantage to this, but only this way you can apply AS3 filters.)
I’d reeeally like to stress this. My solution is not efficient at all, and has probably made @Ultima2876 cringe.
Thanks, I will test both methods when I can access my pc (as i’m on my phone) Then stress test them, but I couldn’t remember that you can stretch images / sprite because I tried on like my first day on flash punk, so I will retry I as soon as possible.