Welcome to Phrogram Sign in | Join | Help


Sprite Tip: Avoid Global Space

  •  10-17-2006, 2:49 PM

    Sprite Tip: Avoid Global Space

    Ever had a sprite that displayed the wrong picture?

    Many of the old KPL examples use global methods such as "LoadSprite". As programmers evolve to Phrogram, a typical piece of code for loading a Sprite may look like this:

    Define mySprite As Sprite = LoadSprite("mypic.gif")
    (My code turned out to contain these)

    Beware! in effect you are registering the sprite in some internal table under a name you do not control. This means you will get the following behaviour:

    Define flowerSprite As Sprite = LoadSprite("flower.gif")
    Define gunSprite As Sprite = LoadSprite("gun.gif")
    flowerSprite.stamp()  //THIS WILL DISPLAY A GUN!

    Two solutions exist:
    (1) Bad solution - Use "LoadSprite" with a unique name. It works, but means that you must manage these names - an unnecessary drag in Phrogram's OO world
    (2) Good Solution: Avoid global functions and more importantly - the global namespace. Use member methods:
    Define flowerSprite As Sprite
    flowerSprite.Load("flower.gif") // Now nothing will put a gun on my screen.

    Happy Spriting.
    Filed under:
View Complete Thread