Jump to content
  • Advertisement
Sign in to follow this  
JonathanCCC

basic XNA problem

This topic is 2858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all,

i'm a total beginner with XNA and C#. i'm in the middle of doing a test game.

right now I basically have 2 sprites on opposite sides of the screen, and I want one of them to fire a fireball whenever I press space.

what I have done so works, but only for 1 fireball, it shoots, dissappears at the max distance I set, but then it won't shoot again. I thought I understood how the draw system works but maybe not.

here is the code I have so far (it isn't alot, just the few methods at the bottom, the rest is mainly the XNA generated stuff.

any help would be much appriciated.

thanks


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Game1
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D player;
Texture2D enemy;
Texture2D fireball;
int playerx = 10;
int playery = 100;
int enemyx = 500;
int firex = 500;
int firey = 100;
int enemyy = 100;
int max = 10;
int maxfireballdist = 1;
bool fireballago = false;
bool visible = false;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
graphics.PreferredBackBufferWidth = 700;
graphics.PreferredBackBufferHeight = 400;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
Window.Title = "Game 1";

base.Initialize();
}


protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
player = Content.Load<Texture2D>("player");
enemy = Content.Load<Texture2D>("wizard");
fireball = Content.Load<Texture2D>("fireball");
}


protected override void UnloadContent()
{

}


protected override void Update(GameTime gameTime)
{
ProcessKeyboard();
base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
Enemy();
Player();
Fireball();
spriteBatch.End();
base.Draw(gameTime);
}
private void Player()
{
Vector2 startposition;
startposition = new Vector2(playerx, playery);
spriteBatch.Draw(player, startposition, Color.White);
}
private void ProcessKeyboard()
{
KeyboardState keyb = Keyboard.GetState();
if (keyb.IsKeyDown(Keys.Right))
playerx += 3;
if (keyb.IsKeyDown(Keys.Left))
playerx -= 3;
if (keyb.IsKeyDown(Keys.Up))
playery -= 3;
if (keyb.IsKeyDown(Keys.Down))
playery += 3;
if (keyb.IsKeyDown(Keys.Space))
{
fireballago = true;
}
}
private void Enemy()
{
Vector2 startposition;
startposition = new Vector2(enemyx, enemyy);
spriteBatch.Draw(enemy, startposition, Color.White);
}
private void Fireball()
{
if (fireballago == true)
{
visible = true;
Vector2 startposition;
startposition = new Vector2(firex, firey);
spriteBatch.Draw(fireball, startposition, Color.White);
firex -= 1;
if (firex == max)
{
visible = false;
fireballago = false;
}
}
}
}
}

Share this post


Link to post
Share on other sites
Advertisement
Hi,

Sorry if I've got this wrong but I think you need to reset firex when the fireball goes off screen. I think at the moment it is probably drawing off-screen.

Share this post


Link to post
Share on other sites
aha yeah you are right, thanks!

also while i've got a new topic open, whats the easiest way to be able to produce multiple fireballs on the screen at once, rather than having to wait for that one to be reset?

and the game I want to make will have the enemies shooting at the player every like 5 seconds or so, do I need a timer for that? does anyone know of a link that I can read to help me achieve this?

Share this post


Link to post
Share on other sites
You want to look into making your fireball a class and then keeping a list of them, in xna they have already made you an easy to use template list.

Once you have your fireball as an object you can keep creating new instances of it and adding them to the list and then in your update and draw functions just loop through the list updating and drawing the objects.

Have fun!

Share this post


Link to post
Share on other sites
Cool cool, and also with your timer you could just store a float timer and increment it by using the gameTime passed into your update function, I think you do something like gameTime.GetElapsedTime() to get the amount of time passed since last cycle.

When the timer reaches a limit then 0 the timer and add a new fireball to the list.

Share this post


Link to post
Share on other sites
i've not done a class yet, but I did add a list, but how do I make the fireballs in the list move since with foreach you aren't allowed to modify any?

new code:


private void Fireball()
{
if (fireballago == true)
{
Vector2 fireballpos;
fireballpos.X = enemyx;
fireballpos.Y = enemyy;
fireList.Add(fireballpos);
fireballago = false;
}

}
private void DrawFireballs()
{
foreach (Vector2 fireballpos in fireList)
spriteBatch.Draw(fireball,fireballpos, null, Color.White, 0, new Vector2(40, 35), 0.2f, SpriteEffects.None, 1);
}

Share this post


Link to post
Share on other sites
Quote:

foreach (Vector2 fireballpos in fireList)
spriteBatch.Draw(fireball,fireballpos, null, Color.White, 0, new Vector2(40, 35), 0.2f, SpriteEffects.None, 1);


you should be able to go like this:


foreach (Vector2 fireballpos in fireList)
{
fireballpos.x --;
spriteBatch.Draw(fireball,fireballpos, null, Color.White, 0, new Vector2(40, 35), 0.2f, SpriteEffects.None, 1);
}



I think that should work, just decrement your x like before but this time for all positions in the list.

Share this post


Link to post
Share on other sites
OK, just try it with a normal for loop then:


for(int i = 0; i < fireList.length; i++)
{
fireList.x --;
spriteBatch.Draw(fireball,fireList, null, Color.White, 0, new Vector2(40, 35), 0.2f, SpriteEffects.None, 1);
}




should work...........

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!