Welcome to Phrogram Sign in | Join | Help


Sprite cloning and LoadSprite

  •  10-14-2006, 7:18 AM

    Sprite cloning and LoadSprite

    I was thinking about creation of many identical sprites. For example, in the "Facing2D" example many identical targets are created:
      For i = 1 To NumberOfTargets
       Define target As Sprite = LoadSprite( "Target" + I, "SmallRedBall.png" )
       target.Visible = True
       PositionTarget( target )
       list.Add( target )
      Next
    This seemed to me inelegant - why should I load the same sprite several times from the disk?
    So I replaced this with:
                    Define template As Sprite = LoadSprite( "SmallRedBall.png" )
      For i = 1 To NumberOfTargets
       Define target As Sprite = template.Clone()
       target.Visible = True
       PositionTarget( target )
       list.Add( target )
      Next
    Well, it didn't work. instead I got: "Object reference not set to an instance of an object". When stepping in with a debugger I notices all my local references where nilled.
    wierd.
    (1) Any ideas why this does not work?
    (2) Is cloning more efficient then loading?
    (3) Also, why did the original set a name for the targets?
    (4) Why do we need a "LoadSprite" which defines a sprite name in some global space?
    Filed under:
View Complete Thread