Help with 'tertiary operator'

is there a way to use the ‘tertiary operator’ to shorten this code?
[AS]if (this._x<0) {
this._x = Width;
}
if (this._x>Width) {
this._x = 0;
}
if (this._y<0) {
this._y = Heigth;
}
if (this._y>Heigth) {
this._y = 0;
}[/AS]
i tried
AS?this._x = Width;
(this._x>Width)?this._x = 0;
(this._y<0)?this._y = Heigth;
(this._y>Heigth)?this._y = 0;[/AS]
but an error comes up…

can anyone help?

never mind, i figured it out, you need a : null at the end of the statement for the else. so it would be:

AS ? this._x = Width : null;
(this._x>Width) ? this._x = 0 : null;
(this._y<0) ? this._y = Heigth : null;
(this._y>Heigth) ? this._y = 0 : null;[/AS]
or to shorten it:

AS ? this._x = Width : (this._x>Width) ? this._x = 0 : (this._y<0) ? this._y = Heigth : (this._y>Heigth) ? this._y = 0 : null;[/AS]

actually, the last script is not a really good idea… :-\

you should use this one instead. :wink:

this._x<0 ? this._x=Width : this._x>Width ? this._x=0 : null;
this._y<0 ? this._y=Heigth : this._y>Heigth ? this._y=0 : null;

I think you’re kinda missing the poin of the operator :wink:


this._x = (this._x < 0) ? Width : (this._x > Width) ? 0 : this._x;
this._y = (this._y < 0) ? Height : (this._y > Heigth) ? 0 : this._y;

:egg:

:stuck_out_tongue:

nice! i didn’t thought about that. =)

I think I spelled one of the words wrong :!:

:wink: