Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (2 posts)

how to re-use a Texture2 image pointer/reference

Last post 1/11/2017 6:52 PM by ElvishJumpSuit. 1 replies.
  • 1/9/2017 3:54 AM

    how to re-use a Texture2 image pointer/reference

    I am working on a windows phone application.

    My images are referenced in the Content folder and in my C# code. I have about 100 images that I reference, but not more than on of the 100 images is displayed at the same time. In other words I only display one of the 100 images at any one time.

     Is it possible to just have one reference to an image and then somehow repoint the reference to a different image? Here is an example of my code.  See Example 1. Example 2 is a pseuduocode example of what I would like to do in order to use just one reference to point
    to different images.

    Example 1 :
    Texture2D playImage_1 ;
    Texture2D playImage_2 ;
    .
    .
    Texture2D playImage_100 ;

    playImage_1 = Content.Load<Texture2D>("playImage_1"); // Reference to image playImage_1
    playImage_2 = Content.Load<Texture2D>("playImage_2");  // Reference to image playImage_2
    playImage_3 = Content.Load<Texture2D>("playImage_3");  // Reference to image playImage_3
    .
    .
    playImage_99 = Content.Load<Texture2D>("playImage_99"); // Reference to image playImage_3
    playImage_100 = Content.Load<Texture2D>("playImage_100"); // Reference to image playImage_3

    Example 2:
    if(Condition == 1)
    {
    playImage_1 = Content.Load<Texture2D>("playImage_1"); 
    }
    else if(Condition == 2)
    {
    playImage_1 = Content.Load<Texture2D>("playImage_2"); 
    }
    .
    .
    .
    else if(Condition == 100)
    {
    playImage_1 = Content.Load<Texture2D>("playImage_100"); 
    }


     
  • 1/11/2017 6:52 PM In reply to

    Re: how to re-use a Texture2 image pointer/reference

    Texture2D playImage  
     
    public void SetImage(int imageNumber)  
    {  
      playImage = Content.Load<Texture2D>("playImage_" + imageNumber.ToString());  

    This is the simplest way to change your image without that massive switch statement.
    Make sure all your required images are present (it will likely cause an exception on a missing file)

    Try it with a random number being generated from a button press.

    In your draw code you could display the filename on top of your picture as debug info and then turn it off when you see it working OK.

    EJS
Page 1 of 1 (2 posts) Previous Discussion Next Discussion