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

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?
How to set xna's TextureFilter to Point
Why doesnt my xna pixel shader turn this texture blue?
How to calibrate kinect skeleton data to video feed?

Categories

HOME
gitlab
cloud
heroku
json.net
view
electron
octobercms
youtube-dl
rsyslog
adb
cvs
spring-jdbc
amazon-ecs
fingerprint
add
cloudkit
quickbooks
ef-migrations
shared-hosting
jprofiler
custom-wordpress-pages
swiftlint
jquery-ajaxq
orleans
mps
dcevm
language-agnostic
apache-commons-io
selectedindexchanged
user-interaction
libssl
c++-amp
nat
oracle-fusion-middleware
siesta-swift
catch-all
amazon-kinesis-kpl
starteam
sqlite2
unobtrusive-validation
fedex
reportingservices-2005
core-plot
elmah
outlook-api
acoustics
flickr-api
dandelion
carrot
kbuild
sqlclient
nsarray
redundancy
google-cdn
passport-google-oauth
rotativa
sts-springsourcetoolsuite
intrusion-detection
multi-level
ctest
multiple-regression
holder.js
markojs
moveit
captivenetwork
nessus
php-internals
independentsoft
two-factor-authentication
xpath-1.0
jfugue
ruby-2.2
dukescript
tableau-online
sysinternals
e10s
device-orientation
nsmutabledictionary
website-monitoring
jscript.net
cctv
php-5.4
uitouch
navigationservice
system.net.webexception
poller
dealloc
bundles
resty-gwt
gridfs
spring-io
padarn
referrer
mt
manchester-syntax
dbproviderfactories
osql
paster
bespin
uimenucontroller
aquaticprime
putchar
scripting-languages
msdev
mdac

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App