Althake


Re: XNA Game Studio Express Refreshing the graphics

Thomas Aylesworth

I'm not sure what you mean when you say that DrawableGameComponents "don't get drawn automatically". If you add your DrawableGameComponents to your Game and set their Visible property to True, then their Draw method does get called automatically. What exactly is it that you are trying to do



Re: XNA Game Studio Express Refreshing the graphics

Althake

I'm just trying to draw the sprite WHILE the game is running, NOT BEFORE IT STARTS.

I created a DrawableGameComponent called Sprite, which adds itself to the game.Components on it's instantiation.

So, suppose I want to create a new sprite in the Program class, it works this way:

Sprite sprite = new Sprite(game, @"Contents/Character", new Rectangle(134, 126, 32, 48), Color.White);
game.Run();

But I need it working this way:

game.Run();
Sprite sprite = new Sprite(game, @"Contents/Character", new Rectangle(134, 126, 32, 48), Color.White);

I need to be able to draw a sprite WHILE the game is running. Don't worry, I have a reason to do this.






Re: XNA Game Studio Express Refreshing the graphics

George Clingerman

Can't you just set the visible property of the DrawableGameComponent to False so that it isn't drawn until you set it equal to True Or am I missing something here





Re: XNA Game Studio Express Refreshing the graphics

Thomas Aylesworth

Althake wrote:
But I need it working this way:

game.Run();
Sprite sprite = new Sprite(game, @"Contents/Character", new Rectangle(134, 126, 32, 48), Color.White);

I need to be able to draw a sprite WHILE the game is running. Don't worry, I have a reason to do this.

That won't even create the sprite until the game has finished running... it certainly won't draw it while the game is running. Game.Run() doesn't return until your game has ended.

As George said, you can use the Visible property to control exactly when the component does and does not draw itself. Maybe if you explain exactly what it is you want to do, we can help you figure out how to do it.





Re: XNA Game Studio Express Refreshing the graphics

Althake

Ok, I'll try to explain what I want to do in another way, because I have been asking this for one week, and nobody seems to understand.

I'm making my very own RPG. As you know, this type of game spawns objects (like monsters, items, whatever) during the game. Lets just say I want to spawn a monster in the middle of the game (when the game has already been ran), how can I do that




Re: XNA Game Studio Express Refreshing the graphics

George Clingerman

If you want to spawn a monster in the middle of the game, then you just create a new monster object in the middle of the game.

It's the same way you would create another projectile in a game while it's running. You can create as many classes of the same type as you need to during a game and you can initialize them at any point that you want.





Re: XNA Game Studio Express Refreshing the graphics

Althake

Thats exactly what I want to do. The problem is that when I create a new monster object, this isn't drawn on the screen... It might be a problem of my computer.

Sprite sprite = new Sprite(game, @"Contents/Character", new Rectangle(134, 126, 32, 48), Color.White);

I don't know if I am doing something wrong.





Re: XNA Game Studio Express Refreshing the graphics

George Clingerman

Well, are you telling it to be in a different position then the first one you draw Is it a completely new object or are you using the same object name "sprite" The code you're showing would indeed make a new object, but there could be some other reason (like some of the ones I just mentioned) for why your new object isn't displaying. (or is displaying, but you can't see it).





Re: XNA Game Studio Express Refreshing the graphics

ProfEclipse

You don't want to create the sprite in your Program class. You would want to create it in one of your Game-derived class' methods. The Program class is just there to have an entry point that creates an instance of your Game-derived class and kicks off the application.

In your Game class, you can create objects in the Initialize method for things that you need to create before the game runs, or you can create objects on-the-fly in your Update method.





Re: XNA Game Studio Express Refreshing the graphics

Althake

Yes, but I need somehow a way to call the Update method externally... What I want is to create a sprite wherever I want to create it.





Re: XNA Game Studio Express Refreshing the graphics

ProfEclipse

Althake wrote:
Yes, but I need somehow a way to call the Update method externally... What I want is to create a sprite wherever I want to create it.

Just create it wherever you want to create it. What's the problem You don't need to call the Update method externally. It gets called constantly by the framework. Say, for example, you want to spawn a new building when the user clicks on the map. In your Update method, you check for user input, get the position of the mouse click, and create a new sprite object there. Done.

Update is where your game logic goes. Draw is where your drawing code goes. There isn't really anything outside of those methods where you have any control once the game is started.





Re: XNA Game Studio Express Refreshing the graphics

Althake

Ok, I tried to do something like that, to create a Sprite in the Update method.

I created a SpriteMask class, which has a static spriteMaskList. In its constructor it adds itself to that spriteMaskList. On the Update method of the Game I wrote something like this:

foreach (SpriteMask spriteMask in SpriteMask.spriteMaskList)
{
Sprite sprite = new Sprite(this, spriteMask.assetName, spriteMask.destinationRectangle, spriteMask.color);
sprite.LoadGraphicsContent(spriteBatch, texture);
sprite.Draw(gameTime);
}

So the Update method should create a new Sprite for each SpriteMask in the SpriteMaskList... But nothing happens.










Re: XNA Game Studio Express Refreshing the graphics

ProfEclipse

You don't draw in the Update method, you draw in the Draw method. Drawing in the Update method will probably result in an exception being thrown, since you won't be inside a BeginScene/EndScene pair. If the code you showed above didn't throw an exception, then I'd be very surprised. Is it possible for you to post your code somewhere

At any rate, you need to understand the concept of separating update from rendering in the framework. Update is where you handle game logic, create dynamic objects, handle user input, do AI, physics, etc. No drawing occurs during update. Draw is where you render any objects that need to be displayed. Update and Draw are called separately and it is possible for one to get called more often than the other. You don't control when they are called, so you don't need to worry about them. They will get called when they are supposed to. You just deal with the right things in the right place. Do your game logic, create and/or destroy dynamic objects, etc. in your Update method. Render whatever objects you have in you Draw method. The framework will take care of calling Update and Draw at the appropriate times.





Re: XNA Game Studio Express Refreshing the graphics

Thomas Aylesworth

Like Prof said, you shouldn't be calling "Draw" in your Update method. In fact, if it's a GameComponent, you shouldn't have to call its Draw method ever since it will be called automatically. But I don't think that's your only problem.

I think you said earlier that your Sprite class is a DrawableGameComponent, is that true If so, is it ever setting its Visible property to True

If it's a DrawableGameComponent, and it's been added to the current Game's list of Components, and it's Visible property is True, then it's Draw method will be called automatically every frame. You can find out whether that is happening by putting a breakpoint inside the Sprite class's Draw method. If you run and hit the breakpoint, then you know your GameComponent has been set up correctly, so the problem must be with how you are drawing the Sprite inside the Draw method (for example, maybe you are drawing it outside of the screen boundaries). If you run but don't hit the breakpoint, then you know that your GameComponent wasn't set up correctly -- it either wasn't added to the Game's list of Components, or it's Visible property is set to False.

What I would do is create a very simple test program. At first, make it so that the Sprite is created and added to your Game's Component list inside your Game's Initialize method. Then verify that the Sprite is being drawn. If it isn't, use the debugging technique I mentioned above to find out why. Assuming your Sprite class is a DrawableGameComponent, then those are the only two possible reasons why it wouldn't be drawn, so it will be easy to track down.

Once you get that test program written, modify it slightly so that instead of creating the Sprite in your Initialize method, create it in your Update method when the user presses the Spacebar (or the A button on a controller if you prefer). Don't Draw it, just create a new Sprite and add it to your Game's Component list. It should be automatically drawn just as it was in the previous version. Again, if it doesn't work, use the debugging technique above to figure out why.

If you still have problems with this simple test program, find a place to upload your code and put a link to it here so we can look at exactly what it is doing. Good luck!