locked
Anyway to put a Sub in a Sub? RRS feed

  • Question

  • Ok, so I am making a game that enters by calling a sub that runs the main game. Now I am trying to get some controls going so that I can move a shape. This requires me to make a subroutine inside the original subroutine. This is what I want but it won't work, Any help is appreciated :)

    Sub maingame
        GraphicsWindow.Clear()
        GraphicsWindow.BackgroundColor = "lightgreen"
        player = Shapes.AddImage(sprite1)
        playery = Shapes.GetTop(player)
        playerx = Shapes.GetLeft(player)
        movex = playerx +1
        movey = playery +1
        GraphicsWindow.KeyDown = OnKeyDown
        sub OnKeyDown
        If GraphicsWindow.LastKey = "w" Then
        Shapes.Move(player, playerx, movey)
            EndIf
        EndSub
      EndSub

    Tuesday, October 22, 2013 8:29 PM

Answers

  • Hi GeekyGabe

    It's impossible to nest a Sub within a Sub. This because Sub has to be followed by EndSub before Sub can be used again.

    What you can do is Call a Sub from within a Sub.

    In your sample you want to run the Sub OnKeyDown within the Sub MainGame. No problem.

    Just create the Sub OnKeyDown outside the Sub MainGame, 2 separate subroutines, then Call OnKeyDown from inside the Sub MainGame.

    Maingame()
    
    Sub maingame
      GraphicsWindow.Clear()
      GraphicsWindow.BackgroundColor = "lightgreen"
      'player = Shapes.AddImage(sprite1)
      'playery = Shapes.GetTop(player)
      'playerx = Shapes.GetLeft(player)
      
      player = Shapes.AddEllipse(20, 20)
      
      GraphicsWindow.KeyDown = OnKeyDown  'call event handler OnKeyDown
    EndSub
    
    Sub OnKeyDown
      If GraphicsWindow.LastKey = "W" Then  'move right
        movex = movex + 1
      ElseIf GraphicsWindow.LastKey = "Q" Then  'move left    
        movex = movex - 1
      ElseIf GraphicsWindow.LastKey = "Down" Then  'move down (arrow key)
        movey = movey + 1    
      EndIf
      Shapes.Move(player, movex, movey)
    EndSub

    Also the key name for "w" is an upper case "W"

    You can program a simple tool for yourself that will give you all the keynames and characterCodes for them using GraphicsWindow.LastKey.

    • Marked as answer by GeekyGabe Tuesday, October 22, 2013 9:53 PM
    Tuesday, October 22, 2013 9:26 PM

All replies

  • Hi GeekyGabe

    It's impossible to nest a Sub within a Sub. This because Sub has to be followed by EndSub before Sub can be used again.

    What you can do is Call a Sub from within a Sub.

    In your sample you want to run the Sub OnKeyDown within the Sub MainGame. No problem.

    Just create the Sub OnKeyDown outside the Sub MainGame, 2 separate subroutines, then Call OnKeyDown from inside the Sub MainGame.

    Maingame()
    
    Sub maingame
      GraphicsWindow.Clear()
      GraphicsWindow.BackgroundColor = "lightgreen"
      'player = Shapes.AddImage(sprite1)
      'playery = Shapes.GetTop(player)
      'playerx = Shapes.GetLeft(player)
      
      player = Shapes.AddEllipse(20, 20)
      
      GraphicsWindow.KeyDown = OnKeyDown  'call event handler OnKeyDown
    EndSub
    
    Sub OnKeyDown
      If GraphicsWindow.LastKey = "W" Then  'move right
        movex = movex + 1
      ElseIf GraphicsWindow.LastKey = "Q" Then  'move left    
        movex = movex - 1
      ElseIf GraphicsWindow.LastKey = "Down" Then  'move down (arrow key)
        movey = movey + 1    
      EndIf
      Shapes.Move(player, movex, movey)
    EndSub

    Also the key name for "w" is an upper case "W"

    You can program a simple tool for yourself that will give you all the keynames and characterCodes for them using GraphicsWindow.LastKey.

    • Marked as answer by GeekyGabe Tuesday, October 22, 2013 9:53 PM
    Tuesday, October 22, 2013 9:26 PM
  • OMG how did I not think of that?!?!!? Thanks, Jibba Jabba!
    Tuesday, October 22, 2013 9:34 PM
  • Also, do you know the best way to get my sprite to turn side ways when walking in that direction? I'm using currently using WASD for the controls.
    Tuesday, October 22, 2013 9:53 PM
  • Well, there's Shapes.Rotate(). But you're gonna end up w/ an upside down sprite! =(

    There's no mirror feature in default SB!

    So you'd better have at least 2 mirrored sprite files!

    Also, take a look at this little game -> WXR400-16


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Tuesday, October 22, 2013 10:17 PM
    Answerer
  • As well as Shapes.Rotate you might be able to create a desired effect by combing Shape.Zoom(X, Y) where X = X + dX then adding a new Shape that walks in the other direction and Removing the old Shape that's not in use.



    • Edited by Jibba j Wednesday, October 23, 2013 12:22 AM addd
    Wednesday, October 23, 2013 12:20 AM
  • The turn your trying to make is like a 3d effect and Shapes just rotates on a 2d plane. So if a Zoom effect on the X-axis doesn't give what you want you could create in a drawing program maybe several sprites that do the turn then add and remove then to create the effect.
    Wednesday, October 23, 2013 1:01 AM
  • Goto. I love that game. I don't know how to kill the enemy though.

    I might that the 1st sprite game I clone.

    Wednesday, October 23, 2013 1:15 AM
  • You just shoot it! Eating pills gives ya ammu!  :D

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, October 23, 2013 1:26 AM
    Answerer