xna-4.0


Creating an array of game objects


G'day All,
If I have a number of objects called enemies on the screen, how can I put them into an array?
My initial thought was to do it at the time they were created. I put a declaration in the top of my Game.cs file:
public enemy[] enemiesArray = new enemy[5];
Then when I create the enemies I tried to add them to the array:
for (int i = 0; i < 2; ++i) // This will create 2 sprites
{
Vector2 position = new Vector2((i+1) * 300, (i+1) * 200);
Vector2 direction = new Vector2(10, 10);
float velocity = 10;
int ID = i;
Components.Add(new skull(this, position, direction, velocity, ID));
skullsArray[i] = skull; // This line is wrong
}
I have also tried to to it in the update() method using code like this:
foreach (GameComponent component1 in Components)
{
int index = component1.ID;
if (component1.ToString() == "enemy")
{
enemiesArray[index] = component1
}
}
But that falls down because component1 does not have an ID. I have to assume that as the program enumerates through each GameComponent it can only access a limited range of data for the component.
In the end I want to be able to refer to my enemies as enemy[1], enemy[2], etc.
Thanks,
Andrew.
I don't see why you cant use a List which works a lot like an array, but it has a variable length. Then you could do this:
List<Enemy> EnemyList;
//You have to initalize it somewhere in Initalize or LoadContent (or the constructor)
You can add enemies just like you would add components (because Components is a List<GameComponent>
EnemyList.Add(enemy);
Then you can access the enemy:
EnemyList[index].DoSomething();
EDIT: just looked at your code again and I noticed that skull doesn't exist. Did you mean
new skull(this, position, direction, velocity, ID);
Because otherwise you are trying to add a class to the array rather than an instance of the class :)
assuming you have put the line public enemy[] enemiesArray = new enemy[5];
inside your game class then your enemiesArray is just a field of your Game class not a game component, you should be able to reference it as
myGameClass.enemiesArray[1]
Assuming that your game class is in scope.
also as #annonymously says lists are easier to resize at runtime than arrays so consider using 'List(5) enemiesArray;' instead
this is not a very scaleable way of handling it however so i suggest you research how to create and register GameComponents. also consider making it generic so you can have one place to reference all your game items instead of having enemiesArray, bulletsArray, someOtherArray, etc
an easy way is to have an abstract class like
public abstract class GameThing
{
public Vector2 Position {get; set;}
//some other props...
}
and then use this as a base for your game items so Enemy is defined as
public class Enemy : GameThing
{
//some props
}
and instead of
public enemy[] enemiesArray = new enemy[5];
you would use
public GameThing[] gameItemsArray= new GameThing[5];
an add items like so
gameItemsArray[1] = new Enemy();

Related Links

Loading Silverlight Texture2D from Resources
XNA Game - Open a Popup or a Custom Game Screen before launching a game
'Microsoft.Xna.Framework.Vector2' is a 'type' but is used like a variable?
Making a trading card game XNA
How do I fix 'No suitable method found to override' error on XNA 4.0?
When to use PresentationParameters.BackBufferWidth vs .Viewport.Width
What does the pixel shader function tex1D do with a Texture2d
How to add a lap timer in XNA?
Is there a Texture1D in XNA?
Particle System: XNA Framework HiDef profile does not support alpha blending on Vector4 textures
How to unset texture data in XNA 4.0
Detecting a Key Press in XNA 4.0?
How to add a start (splash) screen in XNA 4.0?
My program is being reported as a high-level security threat by AVG?
How to add an end screen in XNA 4.0?
Texture2D is way oversized in XNA 4.0?

Categories

HOME
wso2-am
debugging
magnific-popup
urbancode
c#-4.0
tizen
onedrive
dot
xmpp
kalman-filter
gps
pheatmap
webrequest
google-apps-marketplace
remote-access
fortumo
uitypeeditor
pugjs
nhibernate-envers
viewport
predix
sylius
format-specifiers
jndi
hammerspoon
autosys
crystal-reports-2010
linkerd
ping
.net-4.0
uisplitview
catch-all
avcapturesession
windows-dev-center
xmlreader
form-data
atl
picasso
swisscomdev
vapor
particles.js
hp-ux
eclipse-scout
g-code
keydown
flashair
qwt
angular-resource
outlook-2013
recursive-datastructures
boost-preprocessor
gcsfuse
iso8601
bind9
synchronous
smart-table
gnome-shell-extensions
objective-c-swift-bridge
word-vba-mac
time-and-attendance
parallel-data-warehouse
persist
hexdump
pagerank
execute
cyclomatic-complexity
purge
android-listview
apache-commons-fileupload
sortedlist
dundas
lustre
javax.mail
block-device
codeigniter-url
marmalade
oracle-warehouse-builder
fmod
gridfs
cos
chronoforms
random-seed
orchardcms-1.7
referrer
mhtml
cascalog
ocx
adk
libc++
hamachi
yetanotherforum
inotifycollectionchanged
html-input
blitz++
dmx512
project-hosting
putchar
ubuntu-9.04
windows-live-messenger

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App