none
Order of operation RRS feed

  • Question

  • Can someone tell me why this code does not work as I expected.

    Expected: Execute in order of appearance - Draw Lines, Make sound.

    Actual: Make Sound, Draw Lines.

    Sub FireLaser
      GraphicsWindow.PenColor = "Red"
      GraphicsWindow.PenWidth = "2"
      GraphicsWindow.DrawLine(10,630,250,450)
      GraphicsWindow.DrawLine(500,630,250,450)
     
      Sound.PlayAndWait(Program.Directory + "\Ship Laser.mp3")
      
      
      'GraphicsWindow.PenColor = "Black"
      'GraphicsWindow.PenWidth = "3"
      'GraphicsWindow.DrawLine(10,630,250,450)
      'GraphicsWindow.DrawLine(500,630,250,450)
     
    EndSub

    Wednesday, February 21, 2018 3:35 PM

Answers

  • My guess is you are calling the sub from an event.  Events are called asynchronously and the UI is only updated after the event sub returns to the main UI thread.  Try setting flags in event sub to be handled in a game loop.
    • Marked as answer by jswlhw Thursday, February 22, 2018 5:34 PM
    • Unmarked as answer by jswlhw Thursday, February 22, 2018 5:35 PM
    • Marked as answer by jswlhw Thursday, February 22, 2018 5:37 PM
    Thursday, February 22, 2018 2:57 PM
    Moderator

All replies

  • Hi,

    You can try this code:

    GraphicsWindow.PenColor = "Red"
    GraphicsWindow.PenWidth = "2"
    Shapes.AddLine(10,630,250,450)
    Shapes.AddLine(500,630,250,450)
    Sound.PlayAndWait(Program.Directory + "\Ship Laser.mp3")

    Thanks,


    Roshan - 7th Grade Student (Age 12) - Learning Small Basic, Visual Basic and C#

    Thursday, February 22, 2018 2:10 AM
  • Thanks for the suggestion. I had tried that and tried it again after your suggestion.

    The results are the same. Sound then line.

    Thursday, February 22, 2018 3:18 AM
  • Hello jswlhw,

    How about Sound.Play() instead of Sound.PlayAndWait()?


    Nonki Takahashi

    Thursday, February 22, 2018 1:06 PM
    Moderator
  • My guess is you are calling the sub from an event.  Events are called asynchronously and the UI is only updated after the event sub returns to the main UI thread.  Try setting flags in event sub to be handled in a game loop.
    • Marked as answer by jswlhw Thursday, February 22, 2018 5:34 PM
    • Unmarked as answer by jswlhw Thursday, February 22, 2018 5:35 PM
    • Marked as answer by jswlhw Thursday, February 22, 2018 5:37 PM
    Thursday, February 22, 2018 2:57 PM
    Moderator
  • Thanks to all who replied.

    litdev - your suggestion works.

    Below is the modified code

    Main()
    Sub Main  
      ' Run forever  
      While(1 = 1)
        Program.Delay(80) 'added jsw to slow effect down 
        Update()
    ' +++++++ Added per litdev   
        If LaserSound = "Yes" Then    
          MakeLaserSound()                   '
        EndIf
        LaserSound = "No"
     '++++++++++++++++++++  
      EndWhile
    EndSub

    Sub FireLaser
      GraphicsWindow.PenColor = "Red"
      GraphicsWindow.PenWidth = "2"
      GraphicsWindow.DrawLine(10,630,250,450)
      GraphicsWindow.DrawLine(500,630,250,450)
      'Sound.PlayAndWait(Program.Directory + "\Ship Laser.mp3")
      'Sound.Stop(Program.Directory + "\Ship Laser.mp3")
      'Sound.Play(Program.Directory + "\Ship Laser.mp3")
      LaserSound = "Yes"   '+++++++++++++Added per litdev
     
      EndSub
    '++++++ Added
    Sub MakeLaserSound
      Sound.Stop(Program.Directory + "\Ship Laser.mp3")
      Sound.Play(Program.Directory + "\Ship Laser.mp3")
      'Sound.PlayAndWait(Program.Directory + "\Ship Laser.mp3")
      EraseLaser()
    EndSub

    Sub EraseLaser
    GraphicsWindow.PenColor = "Black"
    GraphicsWindow.PenWidth = "3"
    GraphicsWindow.DrawLine(10,630,250,450)
    GraphicsWindow.DrawLine(500,630,250,450)
    EndSub
    ++++++

    Thursday, February 22, 2018 5:37 PM