A little code snippet for beginners like me

Creating Games? Developing something for fun?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jon
Posts: 52
Joined: Sat Apr 14, 2007 11:44 am
Location: Hull UK

A little code snippet for beginners like me

Post by jon » Sat Aug 25, 2012 1:56 am

Hi hope this helps new people (like me)
I used this code for showing scores images and hiding them with visual effects as and when users clicked on certain buttons

Local tplayer1score //declare a local variable (you don't have to declare local but it helps to keep track, i think ? :) )

Code: Select all

if the icon of the target  is 1287 then 
//this is a button icon, the target is the location of what was just clicked on

      set the Loc of image "yourimage.png" to the Loc of the target
 //makes the (hidden) image go to the location I just clicked on
 
      show image "yourimage.png" 
//shows the image at that location

      show image "pow.jpg" 
//and another image, at a location where ever you have placed it

      lock screen
 // for visual effect, Dont want the world to see my crap coding efforts

      play ac "godlike.wav"
//I know, just stop saying it :)

      hide image "yourimage.png" 
      hide image "pow.jpg"

      unlock screen with visual effect dissolve very fast
// now you see the results of my crap coding efforts, but without the in between effects

//Below, the user clicked on a hi score button and scored 10000 points
//fld "test" may or may not have a number in it.

      put fld "test" + 10000 into tplayer1score
//Add numbers to fld "test" by firstly putting fld "test" +10000 into a variable" named (in this case) "tplayer1score"

      put tplayer1score into fld "test"
 //then put tplayer1score into fld "test", putting overwrites anything that is in fld "test"
// (you can use fld OR field)
   end if ]
Kind regards, Jon
I dare not shake the dew drop from a rose lest I disturb the stars.

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: A little code snippet for beginners like me

Post by shaosean » Sat Aug 25, 2012 2:33 am

The only thing I would recommend doing, especially for code that is being shared, is to use constants for items that can have different data.. In your script above, you have two show images and two hide images with two different file names.. If you were to assign a constant the person re-using your code only needs to change the data at the constant declaration and it will automatically update the rest of the script, without them having to go through and manually change them and perhaps miss one (which I am sure I am not the only person to do that ;-) )

Code: Select all

constant kYourImage = "yourimage.png"
constant kPowImage = "pow.jpg"
constant kGodlikeAudio = "godlike.wav"
constant kTestField = "test"

if the icon of the target  is 1287 then 
//this is a button icon, the target is the location of what was just clicked on

      set the Loc of image kYourImage to the Loc of the target
//makes the (hidden) image go to the location I just clicked on

      show image kYourImage
//shows the image at that location

      show image kPowImage
//and another image, at a location where ever you have placed it

      lock screen
// for visual effect, Dont want the world to see my crap coding efforts

      play ac kGodlineAudio
//I know, just stop saying it :)

      hide image kYourImage
      hide image kPowImage

      unlock screen with visual effect dissolve very fast
// now you see the results of my crap coding efforts, but without the in between effects

//Below, the user clicked on a hi score button and scored 10000 points
//fld "test" may or may not have a number in it.

      put fld kTestField + 10000 into tplayer1score
//Add numbers to fld "test" by firstly putting fld "test" +10000 into a variable" named (in this case) "tplayer1score"

      put tplayer1score into fld kTestField
//then put tplayer1score into fld "test", putting overwrites anything that is in fld "test"
// (you can use fld OR field)
end if

jon
Posts: 52
Joined: Sat Apr 14, 2007 11:44 am
Location: Hull UK

Re: A little code snippet for beginners like me

Post by jon » Sat Aug 25, 2012 10:03 am

Hi shaosean

Thank you for your ( as usual ) excellent advice, I learn something every time I post on here. :)
I dare not shake the dew drop from a rose lest I disturb the stars.

Post Reply

Return to “Games”