Still doing that static thing with images. Also my previous blog on a static image was missing a line of code, so I have a class of students trying to figure out what I did wrong. As to the rest of you, thanks for not telling me that I made a mistake.
Today, let’s see if I can do a really simple discussion of how to use Rectangles with Textures. It is a way to program, some designer do it differently.
First, we are going to use the fish images I found somewhere (and will put in a zip file, so if there isn’t a zip file let me know):
Here is the fish. It is also included in the zip file. As well as second one for you to experiment with.
Add the fish to your project, your project will look slightly different, right click on the Project that says (Content) to the right, then select Add and then Existing Item. This will add the image to the content project that is required in every XNA project. You can another fish or image or even use a copy of the same one, just make sure to rename it to Fish2.
Browse to an image you have collected from the web and select it. Change the filename to fish1, this way we don’t have to change the code. Normally you would change the asset name, but in this case to make it easy this should work. Make sure to add the second image or fish named fish2.
Repeat this for another image and name this image fish2
The Class Definition is:
Now we need to initialize the object variables that we have declared:
In this case we are loading the Texture2D with the asset “Fish1”, which we loaded into the (Content) part of the project:
Now we need to draw the image, we use the rectangle, which has the location included in the r_Fish1 when we initialize the rectangle.
This is fun!
Here is what you will see if you use two fishes, one of the images that I used had a background and the other didn’t. One of the fish (the smaller one) is inside of a rectangle, which is one of the ways to scale the image up or down. Rectangles also give you a way to test for collisions.