Any way to hide this 'mess' of initialisation code?
-
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?
-
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
-
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
-
Thanks a lot!
-
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.
-
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.