-
|
|
Problems with GameComponents
|
I'm doing a 2D Game, where I have a ship that shoots to meteors, but I have only one meteor, and I tried this
for(int i = 0; i<10; i++)
{
Components.Add(meteors);
}
but gave me a error: Cannot add the same game component to a game component collection multiple times.
Is there another way to do that, help me. Thanks.
|
|
-
|
|
Re: Problems with GameComponents
|
You need to instantiate 10 meteor objects.
for(int i = 0; i<10; i++)
{
Components.Add(new Meteor());
}
Game hobbyist hell-bent on coding a diabolical Matrix
|
|
-
|
|
Re: Problems with GameComponents
|
But I wanna show 10 meteors in the same time.
|
|
-
|
|
Re: Problems with GameComponents
|
Hi Thiago Nagaoka,
To show 10 meteors at the same time, you will need 10 instances of it, like what Martin Craig has suggested in his code.
Suggestion: you should probably have a MeteorManager class (which is a game component) that manages Meteor instances (which are not game components). The MeteorManager will create Meteor instances, update them (change position, rotation, size etc) and delete them (can do this by just hiding the Meteor instance, and then turning the visibility back on when a "new" Meteor is requested).
Hope I have understood what you need correctly and hope that this post helps!
Skeel Games and Technical Portfolio - http://cg.skeelogy.com/
|
|
-
|
|
Re: Problems with GameComponents
|
Thiago Nagaoka:
But I wanna show 10 meteors in the same time.
Naturally. If you think about it, what your code is doing, is adding the same meteor object to the components list 10 times. So even if the components list allowed this, it still would be no good, because it would just draw the same meteor on top of itself 10 times, so it would still look like 1 meteor.
You need to have 10 meteor objects so that you can specify a different position for each of them, so they aren't drawn in the same position (on top of each other).
Game hobbyist hell-bent on coding a diabolical Matrix
|
|
-
|
|
Re: Problems with GameComponents
|
Thiago Nagaoka:
But I wanna show 10 meteors in the same time.
The Components list handles this for you. Even if you add 100 components to the list, they will all be drawn at the same time (which is not actually the same time, but the same frame, which makes it look like the same time).
Game hobbyist hell-bent on coding a diabolical Matrix
|
|
-
|
|
Re: Problems with GameComponents
|
So, I have to put differents positions into Components.Add?
|
|
-
|
|
Re: Problems with GameComponents
|
Thiago Nagaoka:
So, I have to put differents positions into Components.Add? Only.
|
|
-
|
|
Re: Problems with GameComponents
|
Yes, you will need to set the positions of each meteor. Take a look at some of the tutorials, in the Education catalog (link at top of forum bar), lots of them show how to do this.
Game hobbyist hell-bent on coding a diabolical Matrix
|
|
-
|
|
Re: Problems with GameComponents
|
I use class Random for position, I don't know if that's it I'm not showing 10 meteors.
|
|
-
|
|
Re: Problems with GameComponents
|
Thiago Nagaoka:
I use class Random for position, I don't know if that's it I'm not showing 10 meteors.
that should work, show me the code.
Game hobbyist hell-bent on coding a diabolical Matrix
|
|
-
|
|
Re: Problems with GameComponents
|
My class Meteors has a constructor:
public Meteors(Game game, Texture2D theTexture, Vector2 thePosition)
{
texture = theTexture;
position = thePosition;
}
and my Meteors' method Update has:
position += velocity //velocity = new Vector2(1,2)
And my class Game1 this variable:
Texture2D meteor;
and in my Game1's method LoadContent:
meteor = Content.Load<Texture2D>("meteor");
for(int i = 0; i<10; i++)
{
Components.Add(new Meteors(this, meteor, new Vector2(random.Next(1,2), random.Next(1,7))));
}
|
|
-
-
- (12)
-
premium membership
-
Posts
95
|
Re: Problems with GameComponents
|
Thiago Nagaoka:
for(int i = 0; i<10; i++)
{
Components.Add(new Meteors(this, meteor, new Vector2(random.Next(1,2), random.Next(1,7))));
}
Your problem is that random.Next(1,2) and random.Next(1,7) is a very small range to vary your meteors over - you probably are displaying 10 but they are all almost on top of each other. The screen is (probably) 1280 by 720 pixels in size, and you are placing ll the meteors within 6 pixels of each other.
Try this instead:
new Vector2(random.Next(0,1280), random.Next(0,720))
|
|
-
|
|
Re: Problems with GameComponents
|
All 10 meteors are top of each other. The problem continues.
|
|
-
-
- (10744)
-
premium membership
MVP
-
Posts
6.664
|
Re: Problems with GameComponents
|
Did you read the reply above yours? That will fix your problem.
Jim Perry - Microsoft XNA MVP If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job. Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki. Please mark posts as Answers or Good Feedback when appropriate.
|
|
-
|
|
Re: Problems with GameComponents
|
Thanks Jim, but I already fix my problem, that was logic's problem. I'm sorry if I took a lot of time of you guys. Thanks a lot.
|
|
|