|  | 
 
 Two things that most every game will have are Arrays and Types. You can use them for lots of stuff...keeping track of the player's screen coordinates, hit points, shield reserves, bullets fired (and keeping track of those bullets), sprite images, etc. There's a lot of conflict over which method is the better to use for things, but most folks are coming around to using Types more often than Arrays. This is not to say that Arrays don't have their place, they do! But for speed, ease of use, and freedom from limitations (aside from memory, of course), Types will likely be the choice of most folks. This article will show the fundamentals of both Arrays and Types in order to get you started with them. It's recommended that you download the so you can see all this in action. Click Here 
 Using Arrays is quite easy in BB (as most things are). You simply determine a type of value that you wish to store, dimension (set limits) an array type and load up the values. Here is some source to demonstrate the basics of setting up an array: 
 To access the elements of an array is just as simple as loading it. You can either run through a loop and gather all the elements (or ones based on a certain criteria), or you can target a specific element. Here is an example of both that uses the information from the above example: Before going any further, there is a little tricky piece that gets most people starting out in programming. Computers don't exactly count like we do. If someone was to ask you to count to 10, you'd go 1.2.3.4.5.6.7.8.9.10, right? Well, computers would sorta do that to, but they'd start with 0 instead of 1. Kooky, eh? What this means is that when you reference your arrays, you'd better make sure that you're starting with the computer's idea of the first element and not your idea. Otherwise, you'll get some strange results. So, in the above program when I put the comment "add up the 1 and 3 element...", I was being literal. If I had said "add up the first and 3rd element of the array...", the line of code following would have looked like this: This is also why you'll see nearly all for loops start from 0. Just something you'll want to grasp now before it bites ya ;) Arrays aren't limited to integers. You can use strings and floats and such...heck, you can even have an array of Types if you want. So, flex your Array muscles and try loading up different things! 
 Types, like Arrays, allow you to hold elements. Unlike Arrays, Types allow you to hold various kinds of elements within a single Type and there is no pre-set limit. For example, let's say that you wanted to hold information on a ship. You want to know the name of the ship, what the current X and Y coordinates are, the shield points left, the armor points left, and the type of weapon the ship currently has. Using a Type this is simple! Here's what you'd do. 
 Now that Type, called "Ship", will allow all that stuff to be stored for each ship! From here you would need to create a New element of Type "Ship". You can add as many of these elements as your computer's memory will allow. As an example, here is how we could add one...let's use the Player's ship: What we just did was populate the elements in the "PlayerShip" element of Type "Ship". The beauty of this is that we can now add enemy ships, or ship information from multiplayer connections, etc. They can all use this same Type! For enemy ships, though, I'd recomment naming the element differently. I would call it "EnemyShips" or something to that effect. This helps ensure that you won't be confused when going through your code at a future date. The next cool thing is that we can update each element in the Type by going through a special kind of For loop that BB has for Types. It allows the Each command. To demonstrate how it looks, here is an example that updates all the enemy ship locations: That's it. It goes through each "EnemyShips" element and makes changes to the current X,Y for that element. I'm using the variable iDirectionX just as an example. This would likely be something that included speed and such so the ships could all move at different rates...don't want it to look like everything is in perfect order! If you blow up an enemy ship, you're going to want to remove it from the EnemyShips element. To do that all you have to do is use the BB Delete command. The following code looks for any EnemyShips element that has the "iAlive" flag set to 0, and delete's it accordingly: ConclusionStudy the files in the zip file for this tutorial and you'll see that I have put up some rocks on the screen. There are Array and Type versions of these programs so you can compare the code, speed, etc. Currently the rocks move in only one direction (SE) and they don't spin or anything. I would recommend that you use the image rotation information in previous tutorials to rotate the rocks and then add a field to the "Rocks" Type called "iFrame". Each time you update the X,Y coordinates, update the frame too. You'll probably also want to mess with the timer functions on this so you can avoid the rocks spinning too quickly...I'll touch on the timer a bit in the next tutorial, but see if you can figure it out ahead of time ;) You'll note that I'm starting to leave more exploring up to you rather than spelling out each detail. This is intentional. I'm doing this so you'll study the code and get used to seeing things in action. Also, I'm hopeful that you'll take that code and do some more stuff with it. Anything that is really difficult, I'll try to get into detail on...but most of these items are building blocks from other things we've touched on already so they shouldn't give a brain freeze :) Anyways, play around and experiment...most importantly, have fun! Until next time...cya! 
 For a printable copy of this article, please click HERE. 
 This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.   |