Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Any way to hide this 'mess' of initialisation code?

    Beginners
    4
    6
    438
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • SteveZX81
      SteveZX81 F last edited by

      Hi folks,
      Whenever I start a new project you can guarantee the first 50-100 lines are going to be an ugly mess of loadimage-createsprite-setsprite commands and the like as I load my images and initialise the sprites etc. but man it looks messy and pig ugly (see image)

      I tried to create a function at the very end of my project and dump all this stuff there but of course that does not work.

      So, I'm wondering. Do you have any tips on how to make it look less messy?

      mess.png

      1 Reply Last reply Reply Quote 1
      • pianofire
        pianofire Fuze Team last edited by pianofire

        You can tidy it up a bit using functions e.g.

        player = createPlayer()
        pal = createPlayerAim( "playeraiml" )
        pam = createPlayerAim( "playeraimm" )
        par = createPlayerAim( "playeraimr" )
        
        // Create player sprite
        function createPlayer( )
          result = createSprite( )
          image = loadImage( "player" )
          setSpriteImage( result, image)
          setSpriteLocation( result , 500, 500)
          setSpriteScale( result, 3, 3 )
          setSpriteAnimation( result, 0, 2, 25 )
          setSpriteColour( result, 1, 1 ,0, 1 )
        return result
        
        // Create player aim sprite
        function createPlayerAim( imageName )
          result = createSprite( )
          image = loadImage( imageName )
          setSpriteImage( result, image )
          setSpriteLocation( result, -250, 0)
          setSpriteScale( result, 3, 3 )
          setSpriteAnimation( result, 0, 0, 0 )
          setSpriteColour( result, 1, 1 ,0, 1 )
        return result
        
        1 Reply Last reply Reply Quote 2
        • pianofire
          pianofire Fuze Team last edited by pianofire

          You could even refactor this again (but maybe not so readable) - untested

          player = createPlayerSprite( "player", { 500, 500 }, { 3, 3 }, { 1, 1, 0, 1 }, { 0, 2, 25 } )
          pal = createPlayerSprite( "playeraiml", { -250, 0 }, { 3, 3 }, { 1, 1, 0, 1 }, { 0, 0, 0 } )
          pam = createPlayerSprite( "playeraimm", { -250, 0 }, { 3, 3 }, { 1, 1, 0, 1 }, { 0, 0, 0 } )
          par = createPlayerSprite( "playeraimr", { -250, 0 }, { 3, 3 }, { 1, 1, 0, 1 }, { 0, 0, 0 } )
          
          // Create player sprite
          function createPlayerSprite( imageName, location, scale, colour, animation )
            result = createSprite( )
            image = loadImage( imageName )
            setSpriteImage( result, image)
            setSpriteLocation( result , location)
            setSpriteScale( result, scale )
            setSpriteAnimation( result, animation.x, animation.y, animation.z )
            setSpriteColour( result, colour )
          return result
          
          1 Reply Last reply Reply Quote 3
          • SteveZX81
            SteveZX81 F last edited by

            Thanks a lot!

            1 Reply Last reply Reply Quote 0
            • Jonboy
              Jonboy Fuze Team last edited by Martin

              I tend to organise my projects along these lines:

              //Main global variable
              player = [.id = 0, .pal = 0, .pam = 0, .par = 0]
              
              setup()
              
              loop
                  checkControls()
                  player()
                  enemies()
                  display()
                  debug()
                  update()
              repeat
              
              // then just dump all the actual setup down here..
              
              function setup()
                  image = loadimage("player")
                  paimm = loadimage("playeraim")
                  paiml = loadimage("playeraiml")
                  paimr = loadimage("playeraimr")
                  player.id = createsprite()
                  player.pam = createsprite()
                  player.pal = createsprite()
                  player.par = createsprite()
                  setSpriteBlahBlah....
                  setSpriteBlahBlah....
              return void
              
              function debug()
              // prints useful variables
              return void
              

              Although saying that - my method above with Colin's more sensible user createSprite function would be even better!

              But, I am not sure if my approach is seen as good or bad - it just works well for the way i like to work.

              1 Reply Last reply Reply Quote 3
              • sahara-min
                sahara-min last edited by

                Here is a trick you can use if you want to place your global variables at the end of the code:

                function main()
                  // Program starts here
                return void
                
                // Other functions...
                // Variables...
                
                main()
                

                The main function serves as the starting point for your program. It is defined at the top but invoked at the very end of the code, so all variables will have been created when it runs.

                1 Reply Last reply Reply Quote 3
                • First post
                  Last post