Simple loop problem
Author |
Message |
x-ecutioner
|
Posted: Mon Nov 24, 2008 4:44 pm Post subject: Simple loop problem |
|
|
hi im trying to get my square to move from the left side to the ride side of the screen and back. the issue is, it gets to the right side and gets jammed there. i know its a simple problem but i just cant figure it out
code: |
View.Set ("graphics:500,500")
View.Set ("position:middle,middle")
setscreen ("offscreenonly")
var inc : int
inc := 50
loop
if inc + 10 >= 450 then
inc := inc - 10
else
inc := inc + 10
end if
drawfillbox (inc, 250, inc + 100, 450, 250)
View.Update
delay (30)
drawfillbox (inc, 250, inc + 100, 450, 0)
View.Update
end loop
|
any ideas?
thanks for any help possible |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Mon Nov 24, 2008 4:54 pm Post subject: RE:Simple loop problem |
|
|
Think about it.
The box is at 50. It is as 50. It adds moves right until it becomes greater than 450. It is then moved left by ten, at which point it is less than 450, so it moves right again. Hope I helped! |
|
|
|
|
|
x-ecutioner
|
Posted: Mon Nov 24, 2008 5:10 pm Post subject: RE:Simple loop problem |
|
|
it definetely helped
but the question remains
how do i make it go bcak to the direction it came from
im not looking for actual code
but perhaps a suggestion on what should probably be changed :S
argh
its such a simple problem
i gotta sleep more lol |
|
|
|
|
|
x-ecutioner
|
Posted: Mon Nov 24, 2008 5:12 pm Post subject: RE:Simple loop problem |
|
|
actually dont worry about thank you very much for your help:)
i decided to use for loops instead. |
|
|
|
|
|
copthesaint
|
Posted: Mon Nov 24, 2008 5:30 pm Post subject: Re: Simple loop problem |
|
|
Well If you look at the code
code: | if inc + 10 >= 450 then
inc := inc - 10
else
inc := inc + 10
end if |
You see that once inc will equal 450 it will continueously go back from 440 to 450.
Also if you want to do this right add a cls after the last View.Update. and delet the the white box you keep creating.
Furthermore, if you want a smoother running program then shorten the delay to 3 and decrease
inc:= inc-10
to
inc-= 1
as you see also I changed the := to -= It does the same thing to the effect you were wanting but less typing. |
|
|
|
|
|
Insectoid
|
Posted: Mon Nov 24, 2008 5:40 pm Post subject: RE:Simple loop problem |
|
|
instead of using for loops, use a variable to hold the movement.
code: |
var move := 1
if (box hits right wall) then
move *= -1
elsif (box hits left wall) then
move *= -1
inc += move
|
this can be shortened using the 'or' operator, I just showed the expanded version so you would know what was going on. So the box has 1 added to it, then it hits a wall. The move is flipped to negative 1. adding negative 1 is the same as subtracting positive 1, so it moves the other way. |
|
|
|
|
|
|
|