none
shape's movement RRS feed

  • Question

  • GraphicsWindow.Show()
    aloha = Shapes.AddEllipse(10,10)
    For i = 0 To 100
    x[i] = i
    TextWindow.WriteLine(x[i])
    EndFor
    j = 0
    awal:
    Shapes.Move(aloha, x[j], x[j])
    Program.Delay(100)
    j = j + 1
    Goto awal

    Above change to this:

    GraphicsWindow.KeyDown = kd
    GraphicsWindow.Show()
    aloha = Shapes.AddEllipse(10,10)
    For i = 0 To 100
    x[i] = i
    TextWindow.WriteLine(x[i])
    EndFor
    j = 0

    Sub kd
    awal:
    Shapes.Move(aloha, x[j], x[j])
    Program.Delay(100)
    j = j + 1
    Goto awal
    EndSub

    The shape moves not smooth. Just add a little sub on that program. What is my problem folks?


    Replied


    Friday, February 7, 2014 10:50 AM

Answers

  • You can use a flag (Flip/Flop-Switch) to turn Movement on/off:

    (Use either Sub 'kd' or alternative Sub 'kdAlt')

    GraphicsWindow.KeyDown = kd  ' kdAlt
    GraphicsWindow.Show()
    aloha = Shapes.AddEllipse(10,10)

    For i = 0 To 100
      x[i] = i
      'TextWindow.WriteLine(x[i])
    EndFor

    ' DoMove = "False"

    Loopz:
    While DoMove
      For j = 0 To 100
        Shapes.Move(aloha, x[j], x[j])
        Program.Delay(10)
      EndFor
    EndWhile
    Goto Loopz

    ' ======================================

    Sub kd
    DoMove = "True"  
    EndSub

    ' ------------------------
    Sub kdAlt
      If DoMove Then
        DoMove = "False"
      Else
        DoMove = "True"
      EndIf
    EndSub


    Friday, February 7, 2014 12:23 PM
    Answerer

All replies

  • You can use a flag (Flip/Flop-Switch) to turn Movement on/off:

    (Use either Sub 'kd' or alternative Sub 'kdAlt')

    GraphicsWindow.KeyDown = kd  ' kdAlt
    GraphicsWindow.Show()
    aloha = Shapes.AddEllipse(10,10)

    For i = 0 To 100
      x[i] = i
      'TextWindow.WriteLine(x[i])
    EndFor

    ' DoMove = "False"

    Loopz:
    While DoMove
      For j = 0 To 100
        Shapes.Move(aloha, x[j], x[j])
        Program.Delay(10)
      EndFor
    EndWhile
    Goto Loopz

    ' ======================================

    Sub kd
    DoMove = "True"  
    EndSub

    ' ------------------------
    Sub kdAlt
      If DoMove Then
        DoMove = "False"
      Else
        DoMove = "True"
      EndIf
    EndSub


    Friday, February 7, 2014 12:23 PM
    Answerer
  • GraphicsWindow.Show()
    aloha = Shapes.AddEllipse(10,10)
    'V----- you dont need this
    ''For i = 0 To 100
    ''x[i] = i
    ''TextWindow.WriteLine(x[i])
    ''EndFor
    '^------''
    j = 0
    awal:
    Shapes.Move(aloha, j, j)
    Program.Delay(5)
    j = j + 1
    Goto awal


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Friday, February 7, 2014 4:03 PM
    Answerer
  • Thanks for your explanation.

    Are you suggesting to not put a loop in event's sub? and why? Is it a not-best-practice?


    Replied


    Saturday, February 8, 2014 12:25 PM
  • Good question. Handling events properly is the key to EDP and games.

    Do as little as possible in an Event Handler (your event subs). They are DIFFERENT to normal subroutines in that they are called asynchronously to the main program. They are called when an event is fired by the OS.

    So your program or main loop is running without interruption and at the same time an event can be fired, the handler called and run in a separate thread. Async. No impact on the main program.

    The trick is to tell the main program when an event has occurred. This is done by using flags.

    Simply put, doing stuff like loops in them can cause sticky animations and other problems. PREVENTION is better than no cure.

    This is worth getting a good understanding of.

    Initialise()
    
    '=========================================
    'Game Loop
    '=========================================
    While "True"
      UpdateGame()
      UpdateScreen()  
      Program.Delay(20)  'frame rate = 1000/20 = 50 fps
    EndWhile
    
    '==========================================
    'Subroutines
    '==========================================
    Sub UpdateGame
      If keyDown Then
        'updating the value of X  - Update Game
        If X < 0 Or X > 400 Then
          dX = - dX   'change direction     
        EndIf  
        X = X + dX 
      EndIf
      'or you can flag OFF here as well, depends what you're doing.
    EndSub
    
    Sub UpdateScreen
      'updating the ball on the Screen
      Shapes.Move(ball, X, X/3)
    EndSub
    
    Sub Initialise  
      ball = Shapes.AddEllipse(50, 50)
      dX = 4 'add this to X
      
      'Register Events
      GraphicsWindow.KeyDown = OnKeyDown
      GraphicsWindow.KeyUp = OnKeyUp
    EndSub
    
    '========================================
    'Event Handlers
    '========================================
    Sub OnKeyDown
      keyDown = "True"  'flag
    EndSub
    
    Sub OnKeyUp
      keyDown = "False"  
    EndSub
      

    Recommended reading:

    http://social.technet.microsoft.com/wiki/contents/articles/15060.small-basic-event-basics.aspx

    http://social.technet.microsoft.com/wiki/contents/articles/20865.small-basic-dynamic-graphics.aspx

    Monday, February 10, 2014 3:49 PM
    Moderator
  • So to harp on this....

    Main loop/ game loop / main program is running, continuously and nice and smooth. THEN...

    BANG - an event happens separately to the main program and the Event handler is called separately to the main program by this event. No interruption to the main program/loop.

    All the event handler has to do is let the main loop know that an event has occurred. This is done by using a flag. Once the main loop has processed this flag, you flag it off, flag = "False".

    The sample I gave you is a bit different in that keyDown is flagged off by OnKeyUp event.

    Note the comment in the sample: "'or you can flag OFF here as well, depends what you're doing."


    Monday, February 10, 2014 4:13 PM
    Moderator
  • Great explanation! Thank you for your hard work explaining that. Appreciate that

    Replied




    Thursday, February 13, 2014 7:57 PM