View Full Version : simple but annoying task
sonic_2k_uk
12-24-2002, 10:48 PM
ok ive got whats probably a very simple question and problem. Im trying to create a little flash program where if you click a button it rotates an object an additional 90 degrees. Stupid i know! But i have got that part sorted but then comes the next bit - to create a dynamic text box that displays the angel of rotation of the object. This also works fine up to 180 degress but when it should show 240 it shows 90 again. For anyone that can spare the time to help here is my coding:
on (press) {
_root.red._rotation = _root.red._rotation +90;
}
on (press) {
que = _root.red._rotation;
}
And here is the swf file. (http://www.unique.f9.co.uk/task.swf)
thanks a lot for anyone that helps!
Andy Burton
jcgodart
12-25-2002, 09:33 AM
Hi,
Angles are cyclic, so 240 deg == -90 deg.
Try to add the minus character to the embeded chars of the dynamic text, and it'll shows "-90".
You can also group the 2 lines:
on(press) {
_root.red._rotation += 90;
que = _root.red._rotation;
}
Hope this helps,
Jean-Christophe
sonic_2k_uk
12-25-2002, 09:51 AM
yeah cheers. that gets it to show -90 instead of 90 whensits rotates 240. guess thats the best i can get! lol. thanks a lot!
jcgodart
12-25-2002, 05:45 PM
Great
I noticed we should both go back to school ;)
3*90 deg = 270 deg (not 240)
sonic_2k_uk
12-25-2002, 06:48 PM
easy mistake! but thanks teach. ill learn the error of my ways. netherless, the code still doesnt work 100% as i want it.
cheez
12-25-2002, 07:54 PM
Hehe, I'm king of the hill in the nooooob section today! That does not bode well ;)
Try:
////////////////////////////////////////////////
on(press) {
_root.red._rotation += 90;
x++; if (x==4) {
x=0; }
que = 90 * x;
}
///////////////////////////////////////////////
I don't know, could work! Set x as zero somewhere...
GL,
Cheez
Originally posted by sonic_2k_uk
easy mistake! but thanks teach. ill learn the error of my ways. netherless, the code still doesnt work 100% as i want it.
sonic_2k_uk
12-25-2002, 08:49 PM
by gum i think hes got it! lol, yes it works. how did you work out that one? cheers cheez! my sleepless nights are over.
Just for the record, the modulo operator might be useful here. Instead of thisx++;
if (x==4) {
x=0;
}you could usex++%4;And since both variables are eaqual, why not put everything in 1 line?on(press) {
x++%4;
que=_root.red._rotation = 90*x;
}pom :)
jcgodart
12-26-2002, 05:54 AM
hi,
I don't think x++%4 will do what we need, since the result of the modulo isn't assigned. The post increment (x++) is affecting the variable, but not modulo (%). So it should be written x=(x+1)%4.
That said, we can still use the inc and modulo, but x itself won't be clamped to [0 - 3]
on(press) {
que=_root.red._rotation = 90*(x++ % 4);
}
cheez
12-26-2002, 06:14 AM
Hehe, c'est plus facile quand J' ecrit le resolution! ;)
FWIW, I actually think that the real art and solution to this problem was to separate the geometry from the readout. I guess that's why my two separate events. Because 360 is divisible evenly by 90, that allows us to use whole number counting...
And of course, I have no bloody idea what you guys are talking about! ;) Not until just now when I read it in my book that I see that modulo (%) refers to the remainder when myNumber is divided by x, for instance, (myNumber/x). Hehe, keep us noobs in mind or I'll go back to google I swear! NOT !
Cheers,
Cheez
Originally posted by jcgodart
hi,
I don't think x++%4 will do what we need, since the result of the modulo isn't assigned. The post increment (x++) is affecting the variable, but not modulo (%). So it should be written x=(x+1)%4.
That said, we can still use the inc and modulo, but x itself won't be clamped to [0 - 3]
on(press) {
que=_root.red._rotation = 90*(x++ % 4);
}
jcgodart
12-26-2002, 07:03 AM
In this particular case, we can also use the bitwise and (&) which is usually faster to compute than a remainder (%). It's the kind of thing a compiler optimizer may do for us, but I don't thing AS is optimized that way.
Replacing the remainder (%) by a bitwise and (&) will only work for the positive power of 2 serie : 1,2,4,8,16... by substracting 1 : x%2 = x&1, x%4 = x&3, x%8 = x&7...
It's less easy to understand, so if you have to change your code later, you might not remember what you did.
on(press) {
que=_root.red._rotation = 90*(x++ & 3);
}
Sorry about that, jc, you're absolutely right :) And thanks for the tip. Can you explain why it works like that? I'm not sure I understand (well, I'm sure I don't understand...).
jcgodart
12-26-2002, 12:44 PM
Well computers use a binary system, but we can transpose that to the decimal system we, human being, are using.
remainder of 123 div 10 = 3 <=> keep the lowest digit
remainder of 123 div 100 = 23 <=> keep the 2 lowest digits
...
We can do the same with a computer, but in its binary system
1111011 (123) & 00000001 (1) = 00000001 (1) = 123 % 2
1111011 (123) & 00000011 (3) = 00000011 (3) = 123 % 4
1111011 (123) & 00000111 (7) = 00000011 (3) = 123 % 8
...
The reason it's usually much faster is that a remainder is usually making a division, wich is very very slow.
Another much useful optimization is to replace whenever possible divisions by multiplications. It can be up to 5 to 10 times faster !
x / 2 = x * 0.5
x / 4 = x * 0.25
x / 5 = x * 0.2
Even with variables it can make a difference
x /= n; y /= n; // 2 divisions
Can be written
var inv_n = 1/n; x *= inv_n; y*=inv_n; // 1 division, 2 multiplications : faster
sonic_2k_uk
12-27-2002, 12:32 AM
lol. we'll just let you guys go on in binary whilst we just sit back and try and work out what all the (++%&x) as was 5 posts back...
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.