xna-4.0


XNA: Identifying identical sprites created with for loop


G'day all,
In short, I'm using a for loop to create a bunch of identical sprites that I want to bounce around the screen. The problem is how do I write a collision detection process for the sprites. I have used the process of placing rectangles around sprites and using the .intersects method for rectangles but in that case I created each sprite separately and could identify each one uniquely. Now I have a bunch of sprites but no apparent way to pick one from another.
In detail, if I create an object called Bouncer.cs and give it the movement instructions in it's update() method then create a bunch of sprites using this in Game.cs:
for (int i = 1; i < 5; ++i)
{
Vector2 position = new Vector2(i * 50, i * 50);
Vector2 direction = new Vector2(i * 10, i * 10);
Vector2 velocity = new Vector2(10);
Components.Add(new Bouncer(this, position, direction, velocity, i));
}
base.Initialize();
I can draw a rectangle around each one using:
foreach (Bouncer component1 in Components)
{
Bouncer thing = (Bouncer)component1;
Rectangle thingRectangle;
thingRectangle = new Rectangle((int)thing.position.X, (int)thing.position.Y, thing.sprite.Width, thing.sprite.Height);
But now, how do I check for a collision? I can hardly use:
if (thingRectangle.Intersects(thingRectangle))
I should point out I'm a teacher by trade and play with coding to keep my brain from turning to mush. Recently I have been working with Python and with Python I could just put all the sprites into a list:
sprites[];
Then I could simply refer to each as sprite[1] or sprite[2] or whatever its index in the list is. Does XNA have something like this?
Please let me know if any more code needs to be posted.
Thanks,
Andrew.
One solution, which I use in my game engine, is to have a Logic code run inside the objects for every game Update, ie. every frame. It seems you already do this, according to the variable names, which indicate you run some physics code in the objects to update their positions.
You might also want to create the collision rectangle inside the Bouncer's constructor so it's more accessible and you make good use of object oriented programming, maybe even make it an accessor, so you can make it update every time you call it instead of manually updating the bounding/collision box. For example:
public Rectangle #BoundingBox {
get { return new Rectangle(_Position.X, _Position.Y, width, height); }
}
Whichever way works, but the collision checks can be run inside the Bouncer object. You can either make the reference list of the Bouncer objects static or pass it to the objects itself. The code for collisions is very simply:
foreach(Bouncer bouncer in Components) //Components can be a static List or you can pass it on in the constructor of the Bouncer object
{
if (bouncer.BoundingBox.Intersects(this.BoundingBox))
{
//they collided
}
}

Related Links

Displaying seconds in XNA with Keypress?
XNA game how to make a snake tail
Creating an array of game objects
Lap timer in XNA 4.0?
XNA: Identifying identical sprites created with for loop
Increase Character Spacing in XNA
XNA choosing a subgroup of Game.Components
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?

Categories

HOME
ibm-bluemix
wso2-am
testng
minimum-spanning-tree
view
syntax
octobercms
youtube-dl
filtering
swagger-ui
adfs
elasticsearch-hadoop
cloudkit
vault
modelica
resize
moonmail
iron-router
quartz-scheduler
worldwind
openrefine
highlight.js
angular-ui
text-rendering
lucene.net
karma-jasmine
icloud-api
p-value
facet
intel-pin
windows-server-2000
primitive
react-chartjs
xmlreader
android-kernel
nssegmentedcontrol
fusionpbx
dosbox
adobe-premiere
impersonation
angularjs-factory
azure-ml
lumberjack
trim
eigenvalue
gpx
qcombobox
angular2-meteor
qwt
auto-update
network-flow
hibernate-tools
revapi
togetherjs
sqlclient
lift-json
memory-alignment
flow-control
account-kit
removechild
phishing
ctest
etsy
storekit
persist
react-native-listview
sdf
php-internals
ios4
goose
kendonumerictextbox
cartesian-product
ora-00900
django-unittest
tableau-online
sonarqube5.1.2
camanjs
nsbutton
flask-cors
has-many-through
eol
navigationservice
valuechangelistener
cloud-connect
srs
openlaszlo
page-layout
c18
farseer
soundtouch
ruby-datamapper
bulkloader
qt-faststart
pushbackinputstream
spring-validator
enterprisedb
jquery-knob
gil
coderush
pysimplesoap
netbeans-6.9
jquery-ui-layout
xdomainrequest
dentrix
thunderbird-lightning
nerddinner
w3wp

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