+ 4

Animation 1px problem #Solved

So... Ball 20x20. Jumps off from walls each time it touch it,but... It jumps from Y walls <0 coordinate and >380 (works good) But when I'm trying to set same for X - it stuck. Aaaand...I can't get why :D Works only with <0 && >379...but it supposed to be 380 ? Code: https://code.sololearn.com/W3oz311rOExq

19th May 2017, 11:46 AM
Rose Sevenyears
Rose  Sevenyears - avatar
11 Respostas
+ 5
...Look like you prefer to use the long code what I mean is if (x < 0 || x>380){ speedx *= -1; } if (y<0 || y>380) { speedy *= -1; } *just replace else if with "if"*
19th May 2017, 12:13 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 7
if ((x<0 && speedx==-1)||(x>380 && speedx==1)) {speedx *= -1;} else if ((y<0 && speedy==-1)||(y>380 && speedy==1)) {speedy *= -1;}
19th May 2017, 11:28 AM
Burey
Burey - avatar
+ 7
ya because of red colorđŸ˜”đŸ˜”
19th May 2017, 4:31 PM
Prashanth Kumar
Prashanth Kumar - avatar
+ 5
change to the conditions above to also take the current direction into consideration
19th May 2017, 11:28 AM
Burey
Burey - avatar
+ 5
replace "else if" with just "if" That should be worked After that replace x=0 with x=100//For working beautifully
19th May 2017, 11:29 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 5
Btw btw @Burey is so cool xD
19th May 2017, 1:18 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 5
nice code did u saw my code of bouncin ball with marquee
19th May 2017, 4:21 PM
Prashanth Kumar
Prashanth Kumar - avatar
0
@Burey Yep,that works :) Changed to if ((x <=0 && speedx == -1) || (x >= 380 && speedx == 1)) { speedx *= -1; } else if ((y <= 0 && speedy == -1) || (y >= 380 && speedy == 1)) { speedy *= -1; } Not sure if it goes out of limits now ,but whatever :D _____________________ @Very Hard(java & c++ & c# & py & rust & (AllHard)) Thank you,i'll replace it with random positions later:P
19th May 2017, 11:43 AM
Rose Sevenyears
Rose  Sevenyears - avatar
- 1
@Very Hard(java & c++ & c# & py & rust & (AllHard)) Not realy ,I tryed to guess the reason of problem first of all... And guess now I see it. My first code can't check 2 conditions at once So after Y bounce - it went in same step to X and instead of bounce kept moving... So it was like 382 - 381 - 382 ...........untill other Y. I choised Burey's code cause I thought yours wrong,thought you want me to put X "if" inside Y "if" and for sure I can say it will not work even without tests , that's the reason why I ignored it ,just misunderstood :) I'm pretty slow at finding real reason :D
19th May 2017, 12:41 PM
Rose Sevenyears
Rose  Sevenyears - avatar
- 1
@Prashanth Kumar {Éčəpօɔ} Nope,post it here if you want to,i'm here from PC ,so I can't check your codes without link :)
19th May 2017, 4:26 PM
Rose Sevenyears
Rose  Sevenyears - avatar
- 1
@Prashanth Kumar {Éčəpօɔ} Fun one,but pretty painfull for eyes :D
19th May 2017, 4:30 PM
Rose Sevenyears
Rose  Sevenyears - avatar