locked
Multiple Timer.Interval values RRS feed

  • Question

  • In Small Basic, is it possible to have multiple Timer.Interval values so that different events can be raised at different points.
    Tuesday, June 7, 2011 11:13 AM

Answers

  • First, this bit of code near the top, does a loop 100 times and each loop has a delay of 3 sec, so this takes 5min with nothing else happening before the While (True") loop that check for button input.  Perhaps get rid of this code.

    For i = 0 To 1100 Step 11
      Shapes.Move(targetShape, ShapeX, ShapeY)
      ShapeX = ShapeX + 100
      Program.Delay(3000)
    EndFor

    Secondly, the test for a hit doesn't work because ShapeX and ShapeY are not updated with the shape position, since you are moving it with the animate inside the timer to fixed coordinates and not updating its position as ShapeX and ShapeY.  Note that when tick = 12, then Math.Remainder(tick,6) = 0 and Math.Remainder(tick,12) = 0, so perhaps you need slightly different logic.

    In fact doing the animate movement inside the event timer subroutine will cause problems so you should use flags like for the buttonPress, also the position of ShapeX and ShapeY cannot be updated for intermediate positions during the Shapes.Animate so I would consider using Shapes.Move, updating ShapeX and ShapeY appropriately and probably not use a Timer at all unless it is used to update ShapeX and ShapeY directly and do the move in the main While loop.

    PS Do a search of the SmallBasic forum for 'data extension'.

    Perhaps a bit like this:

    x = 100
    y = 510

    GraphicsWindow.BackgroundColor = "Cyan"
    GraphicsWindow.Height = 690
    GraphicsWindow.Width = 1350
    GraphicsWindow.Top = Desktop.Height - Desktop.Height
    GraphicsWindow.Left = Desktop.Width - Desktop.Width
    GraphicsWindow.BrushColor = "Blue"
    Turtle.Show()
    Turtle.PenUp()

    AddShape()

    DegreesTextBox = Controls.AddTextBox(100, 610)
    GraphicsWindow.DrawText(100, 595, "Enter Degrees:")
    DistanceTextbox = Controls.AddTextBox(455, 610)
    GraphicsWindow.DrawText(455, 595, "Enter Distance:")
    LeftButton = Controls.AddButton("Left", 300, 609)
    Rightbutton = Controls.AddButton("Right", 370, 609)
    HitButton = Controls.AddButton("Hit", 650, 609)
    Controls.ButtonClicked = ButtonPress
    Controls.TextTyped = TextBoxSub

    Timer.Interval = 100
    Timer.Tick = onTicking
    tick = 0

    buttonPressed = 0
    Turtle.Speed = 9

    While ("True")
      Shapes.Move(targetShape, ShapeX, ShapeY)
     
      If (buttonPressed = 1) Then
        doTurtleMove()
        Program.Delay(1000)
        buttonPressed = 0
      EndIf
     
      WinScenario()
     
      Program.Delay(100)
    EndWhile

    Sub AddShape
      ShapeX = 100
      ShapeY = 100
      TargetShape = Shapes.AddEllipse(80, 80)
    EndSub

    Sub doTurtleMove
      operator = Controls.GetButtonCaption(Controls.LastClickedButton)
      If operator = "Left" Then
        Turtle.Turn(LeftDegrees)
      ElseIf operator = "Right" Then
        Turtle.Turn(Degrees)
      ElseIf operator = "Hit" Then
        Turtle.Move(Distance)
      EndIf
    EndSub

    Sub WinScenario
      x2= Turtle.X
      y1 = Turtle.Y
      x1= ShapeX + 40
      y2 = ShapeY + 40
      D = Math.SquareRoot((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
      If (D < 40) Then
        GraphicsWindow.ShowMessage("You Win", "Winner")
        Program.Delay(200)
        Program.End()
      EndIf
    EndSub    

    Sub TextBoxSub
      Degrees = Controls.GetTextBoxText(DegreesTextBox)
      LeftDegrees = 0 - Degrees
      Distance = Controls.GetTextBoxText(DistanceTextbox)
    EndSub

    Sub ButtonPress
      buttonPressed = 1
    EndSub

    Sub onTicking
      If (buttonPressed = 0) Then 'Only move target when turtle isn't moving
        ShapeX = ShapeX+1
        ShapeY = 100
      EndIf
    EndSub




    • Marked as answer by Horslip Tuesday, June 7, 2011 8:57 PM
    Tuesday, June 7, 2011 7:52 PM

All replies

  • You can either use the SecondTimer method available with the Oskariok Data extension or program your timer to do different things on different Ticks, for example dots appearing at 300, 700 and 1300 ms intervals:

    gw = 800
    gh = 600
    GraphicsWindow.Width = gw
    GraphicsWindow.Height = gh
    tick = 0
    Timer.Interval = 100
    Timer.Tick = OnTick

    Sub OnTick
      tick = tick+1
      If (Math.Remainder(tick,3) = 0) Then
        GraphicsWindow.BrushColor = "Red"
        GraphicsWindow.FillEllipse(Math.GetRandomNumber(gw),Math.GetRandomNumber(gh),20,20)
        EndIf
      If (Math.Remainder(tick,7) = 0) Then
        GraphicsWindow.BrushColor = "Blue"
        GraphicsWindow.FillEllipse(Math.GetRandomNumber(gw),Math.GetRandomNumber(gh),20,20)
        EndIf
      If (Math.Remainder(tick,13) = 0) Then
        GraphicsWindow.BrushColor = "Green"
        GraphicsWindow.FillEllipse(Math.GetRandomNumber(gw),Math.GetRandomNumber(gh),20,20)
        EndIf
    EndSub

    Tuesday, June 7, 2011 5:40 PM
  • What am I doing wrong?

    Start:

     

    x = 100

    y = 510

     

    GraphicsWindow.BackgroundColor = "Cyan"

    GraphicsWindow.Height = 690

    GraphicsWindow.Width = 1350

    GraphicsWindow.Top = Desktop.Height - Desktop.Height

    GraphicsWindow.Left = Desktop.Width - Desktop.Width

    GraphicsWindow.BrushColor = "Blue"

    Turtle.Show()

    Turtle.PenUp()

     

    AddShape()

     

    DegreesTextBox = Controls.AddTextBox(100, 610)

    GraphicsWindow.DrawText(100, 595, "Enter Degrees:")

    DistanceTextbox = Controls.AddTextBox(455, 610)

    GraphicsWindow.DrawText(455, 595, "Enter Distance:")

    LeftButton = Controls.AddButton("Left", 300, 609)

    Rightbutton = Controls.AddButton("Right", 370, 609)

    HitButton = Controls.AddButton("Hit", 650, 609)

    Controls.ButtonClicked = ButtonPress

    Controls.TextTyped = TextBoxSub

    Timer.Tick = onTicking

    Timer.Interval = 2000

    tick = 0

    Turtle.Speed = 9

    ShapeX = 100

    ShapeY =100

     

    For i = 0 To 1100 Step 11

      Shapes.Move(targetShape, ShapeX, ShapeY)

      ShapeX = ShapeX + 100

      Program.Delay(3000)

    EndFor

     

    While ("True")

      If (buttonPressed = 1) Then

        doTurtleMove()

        Program.Delay(1000)

        buttonPressed = 0

      EndIf

      WinScenario()

      Program.Delay(100)

    EndWhile

     

    Sub AddShape

      ShapeX = 100

      ShapeY = 100

      TargetShape = Shapes.AddEllipse(80, 80)

      Shapes.Move(TargetShape, 100, 100)

    EndSub

     

    Sub doTurtleMove

      operator = Controls.GetButtonCaption(Controls.LastClickedButton)

      If operator = "Left" Then

        Turtle.Turn(LeftDegrees)

        Program.Delay(2000)

      ElseIf operator = "Right" Then

        Turtle.Turn(Degrees)

      ElseIf operator = "Hit" Then

        Turtle.Move(Distance)

      EndIf

    EndSub

     

    Sub WinScenario

      x2= Turtle.X

      y1 = Turtle.Y

      x1= ShapeX + 40

      y2 = ShapeY + 40

      D = Math.SquareRoot((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))

      If (D < 40) Then

        GraphicsWindow.ShowMessage("You Win", "Winner")

        Program.Delay(200)

        Program.End()

      EndIf

    EndSub     

     

    Sub TextBoxSub

      Degrees = Controls.GetTextBoxText(DegreesTextBox)

      LeftDegrees = 0 - Degrees

      Distance = Controls.GetTextBoxText(DistanceTextbox)

    EndSub

     

    Sub ButtonPress

      buttonPressed = 1

    EndSub

     

    Sub onTicking

      tick = tick + 1

      If (Math.Remainder(tick,6) = 0) Then

        Shapes.Animate(targetShape, 300, 100, 10000)

      EndIf

      If (Math.Remainder(tick,12) = 0) Then

        Shapes.Animate(targetShape, 500, 100, 10000)

      EndIf

      If (Math.Remainder(tick,18) = 0) Then

        Shapes.Animate(targetShape, 700, 100, 10000)

      EndIf

      If (Math.Remainder(tick,24) = 0) Then

        Shapes.Animate(targetShape, 900, 100, 10000)

      EndIf

      If (Math.Remainder(tick,30) = 0) Then

        Shapes.Animate(targetShape, 1100, 100, 10000)

      EndIf

      If (Math.Remainder(tick,36) = 0) Then

        GraphicsWindow.ShowMessage("Too Bad, You Lost! Ha! Ha!", "LOSER!")

        Program.End()

      EndIf

    EndSub

     ps. Where can I get ostariok data extension

    Tuesday, June 7, 2011 7:22 PM
  • First, this bit of code near the top, does a loop 100 times and each loop has a delay of 3 sec, so this takes 5min with nothing else happening before the While (True") loop that check for button input.  Perhaps get rid of this code.

    For i = 0 To 1100 Step 11
      Shapes.Move(targetShape, ShapeX, ShapeY)
      ShapeX = ShapeX + 100
      Program.Delay(3000)
    EndFor

    Secondly, the test for a hit doesn't work because ShapeX and ShapeY are not updated with the shape position, since you are moving it with the animate inside the timer to fixed coordinates and not updating its position as ShapeX and ShapeY.  Note that when tick = 12, then Math.Remainder(tick,6) = 0 and Math.Remainder(tick,12) = 0, so perhaps you need slightly different logic.

    In fact doing the animate movement inside the event timer subroutine will cause problems so you should use flags like for the buttonPress, also the position of ShapeX and ShapeY cannot be updated for intermediate positions during the Shapes.Animate so I would consider using Shapes.Move, updating ShapeX and ShapeY appropriately and probably not use a Timer at all unless it is used to update ShapeX and ShapeY directly and do the move in the main While loop.

    PS Do a search of the SmallBasic forum for 'data extension'.

    Perhaps a bit like this:

    x = 100
    y = 510

    GraphicsWindow.BackgroundColor = "Cyan"
    GraphicsWindow.Height = 690
    GraphicsWindow.Width = 1350
    GraphicsWindow.Top = Desktop.Height - Desktop.Height
    GraphicsWindow.Left = Desktop.Width - Desktop.Width
    GraphicsWindow.BrushColor = "Blue"
    Turtle.Show()
    Turtle.PenUp()

    AddShape()

    DegreesTextBox = Controls.AddTextBox(100, 610)
    GraphicsWindow.DrawText(100, 595, "Enter Degrees:")
    DistanceTextbox = Controls.AddTextBox(455, 610)
    GraphicsWindow.DrawText(455, 595, "Enter Distance:")
    LeftButton = Controls.AddButton("Left", 300, 609)
    Rightbutton = Controls.AddButton("Right", 370, 609)
    HitButton = Controls.AddButton("Hit", 650, 609)
    Controls.ButtonClicked = ButtonPress
    Controls.TextTyped = TextBoxSub

    Timer.Interval = 100
    Timer.Tick = onTicking
    tick = 0

    buttonPressed = 0
    Turtle.Speed = 9

    While ("True")
      Shapes.Move(targetShape, ShapeX, ShapeY)
     
      If (buttonPressed = 1) Then
        doTurtleMove()
        Program.Delay(1000)
        buttonPressed = 0
      EndIf
     
      WinScenario()
     
      Program.Delay(100)
    EndWhile

    Sub AddShape
      ShapeX = 100
      ShapeY = 100
      TargetShape = Shapes.AddEllipse(80, 80)
    EndSub

    Sub doTurtleMove
      operator = Controls.GetButtonCaption(Controls.LastClickedButton)
      If operator = "Left" Then
        Turtle.Turn(LeftDegrees)
      ElseIf operator = "Right" Then
        Turtle.Turn(Degrees)
      ElseIf operator = "Hit" Then
        Turtle.Move(Distance)
      EndIf
    EndSub

    Sub WinScenario
      x2= Turtle.X
      y1 = Turtle.Y
      x1= ShapeX + 40
      y2 = ShapeY + 40
      D = Math.SquareRoot((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
      If (D < 40) Then
        GraphicsWindow.ShowMessage("You Win", "Winner")
        Program.Delay(200)
        Program.End()
      EndIf
    EndSub    

    Sub TextBoxSub
      Degrees = Controls.GetTextBoxText(DegreesTextBox)
      LeftDegrees = 0 - Degrees
      Distance = Controls.GetTextBoxText(DistanceTextbox)
    EndSub

    Sub ButtonPress
      buttonPressed = 1
    EndSub

    Sub onTicking
      If (buttonPressed = 0) Then 'Only move target when turtle isn't moving
        ShapeX = ShapeX+1
        ShapeY = 100
      EndIf
    EndSub




    • Marked as answer by Horslip Tuesday, June 7, 2011 8:57 PM
    Tuesday, June 7, 2011 7:52 PM
  • Thanks yet again.


    Tuesday, June 7, 2011 8:57 PM