Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Neverending loop
#1
So here's one that has me scratching my head, that maybe you guys can take a look at with a fresh set of eyes and sort out:

Code: (Select All)
Screen _NewImage(1280, 720, 32)
$Color:32
f = _LoadFont("courbd.ttf", 128, "monospace")
_Font f
Color Red, Transparent
_PrintString (284, 200), "Steve is" '284 - 644
_PrintString (284, 328), "Awesome!"
Sleep
_Font 8

Explode 284, 200, 644, 456, 16, 16


Print "FINISHED!!"






Sub Explode (x1, y1, x2, y2, pw, ph)
    tempScreen = _NewImage(_Width, _Height, 32)
    _PutImage , 0, tempScreen
    w = x2 - x1 + 1: h = y2 - y1 + 1
    ax = 2 * w \ pw + 1: ay = 2 * h \ ph + 1
    cx = x1 + w \ 2: cy = y1 + h \ 2

    Type box
        x As Single
        y As Single
        handle As Long
        rotation As Single
        changex As Single
        changey As Single
    End Type

    Dim Array(0 To ax, 0 To ay) As box
    For x = 0 To ax
        For y = 0 To ay
            Array(x, y).handle = _NewImage(pw, ph, 32)
            Array(x, y).x = x1 + pw * x
            Array(x, y).y = y1 + ph * y
            _PutImage , 0, Array(x, y).handle, (x1 + pw * x, y1 + ph * y)-Step(pw, ph)
            Array(x, y).changex = -(cx - Array(x, y).x) / 10
            Array(x, y).changey = -(cy - Array(x, y).y) / 10
        Next
    Next

    Do
        Cls , 0
        finished = -1
        For x = 0 To ax
            For y = 0 To ay
                Array(x, y).x = Array(x, y).x + Array(x, y).changex
                Array(x, y).y = Array(x, y).y + Array(x, y).changey
                If Array(x, y).x >= 0 And Array(x, y).y >= 0 And _
                   Array(x, y).x <= _Width And Array(x, y).y <= _Height Then finished = 0
                _PutImage (Array(x, y).x, Array(x, y).y)-Step(pw, ph), Array(x, y).handle, 0, (0, 0)-(pw, ph)
            Next
        Next
        _Display
        _Limit 60

    Loop Until finished
    _AutoDisplay
End Sub


This is supposed to be just a simple little routine which explodes a portion of the screen off the screen.  It works as intended, except for the simple fact that it doesn't know when to stop working, resulting in an endless loop!

Our main logic here comes from this little snippet of code:

    Do
        Cls , 0
        finished = -1
        For x = 0 To ax
            For y = 0 To ay
                Array(x, y).x = Array(x, y).x + Array(x, y).changex
                Array(x, y).y = Array(x, y).y + Array(x, y).changey
                If Array(x, y).x >= 0 And Array(x, y).y >= 0 And _
                   Array(x, y).x <= _Width And Array(x, y).y <= _Height Then finished = 0
                _PutImage (Array(x, y).x, Array(x, y).y)-Step(pw, ph), Array(x, y).handle, 0, (0, 0)-(pw, ph)
            Next
        Next
        _Display
        _Limit 60

    Loop Until finished


Our DO loop.
   We clear the screen
   Set a flag for being finished
   The FOR loops
     change the X/Y coordinates
     IF we still have an X/Y coordinate on the screen, then we're not finished
     Draw the image in its new positon
   NEXT
   Display
LOOP until finished

*********************************************

So the question becomes, "Why isn't this simple logic working?"  We set the flag by default every time, and only if we draw on screen do we clear that flag...  Why is this running as a non-terminating loop?  Enquiring, tired old eyes are going to bed, and hoping that maybe someone here will figure out what the heck is going wrong with such a simple process.
Reply


Messages In This Thread
Neverending loop - by SMcNeill - 07-30-2022, 03:18 AM
RE: Neverending loop - by Pete - 07-30-2022, 03:42 AM
RE: Neverending loop - by Pete - 07-30-2022, 04:08 AM
RE: Neverending loop - by bplus - 07-30-2022, 05:02 AM
RE: Neverending loop - by TempodiBasic - 08-02-2022, 01:08 PM
RE: Neverending loop - by bplus - 07-30-2022, 05:11 AM
RE: Neverending loop - by Pete - 07-30-2022, 05:26 AM
RE: Neverending loop - by SMcNeill - 07-30-2022, 03:13 PM
RE: Neverending loop - by bplus - 07-30-2022, 03:43 PM
RE: Neverending loop - by Pete - 07-30-2022, 03:45 PM
RE: Neverending loop - by Kernelpanic - 07-30-2022, 09:54 PM
RE: Neverending loop - by bplus - 08-02-2022, 03:42 PM
RE: Neverending loop - by Kernelpanic - 08-02-2022, 05:03 PM
RE: Neverending loop - by admin - 08-02-2022, 06:59 PM
RE: Neverending loop - by Kernelpanic - 08-02-2022, 07:51 PM
RE: Neverending loop - by TempodiBasic - 08-04-2022, 12:43 AM



Users browsing this thread: 8 Guest(s)