fireballs follow character?

ok, im not sure what im doing wrong...

but when I stop press space bar(character is facing right) to fire and then decide to move my my character left the fire ball moves backwards with the character

and vice a versa I can move my character left, right, left right repedatly and the fireballs stays with the character until I stop moving and they finish the x distance and disappear

here are two methods im using

protected void AddFireball()

{

if (_fireballsElapsed == 0)

{

bool _addFireball = true;

foreach (KeyValuePair<string, Sprite.Sprite> aFireball in _fireballs)

{

if (aFireball.Value.Visible == false)

{

aFireball.Value.StartPosition = new Vector2(_tif.Position.X + 50, _tif.Position.Y);

aFireball.Value.Position = new Vector2(_tif.Position.X + 50, _tif.Position.Y);

aFireball.Value.Visible = true;

_fireballsElapsed = (float).5;

_addFireball = false;

break;

}

}

if (_addFireball == true && _tif.Direction == Sprite.Sprite.eDirection.Right)

{

string aKey = "Fireball" + (_fireballs.Count + 1).ToString();

_fireballs.Add(aKey, new Sprite.Sprite(new Rectangle((int)_tif.Position.X + 10, (int)_tif.Position.Y, 25, 25),

new Rectangle(125, 250, 125, 125)));

_fireballs[aKey].Visible = true;

_fireballsElapsed = (float)0.5f;

}

if (_addFireball == true && _tif.Direction == Sprite.Sprite.eDirection.Left)

{

string aKey = "Fireball" + (_fireballs.Count + 1).ToString();

_fireballs.Add(aKey, new Sprite.Sprite(new Rectangle((int)_tif.Position.X - 10, (int)_tif.Position.Y, 25, 25),

new Rectangle(0, 250, 125, 125)));

_fireballs[aKey].Visible = true;

_fireballsElapsed = (float)0.5f;

}

}

}

and....

protected void UpdateFireballs(float elapsed)

{

_fireballsElapsed -= elapsed;

if (_fireballsElapsed < 0)

{

_fireballsElapsed = 0;

}

bool aFireballVisible = false;

foreach (KeyValuePair<string, Sprite.Sprite> aFireball in _fireballs)

{

if (aFireball.Value.Visible == true && _tif.Direction == Sprite.Sprite.eDirection.Right)

{

if (aFireball.Value.Position.X > aFireball.Value.StartPosition.X + 300)

{

aFireball.Value.Visible = false;

}

else

{

aFireball.Value.Position = new Vector2(aFireball.Value.Position.X + 10, aFireball.Value.Position.Y);

aFireballVisible = true;

}

}

if (aFireball.Value.Visible == true && _tif.Direction == Sprite.Sprite.eDirection.Left)

{

if (aFireball.Value.Position.X > aFireball.Value.StartPosition.X + 300)

{

aFireball.Value.Visible = false;

}

else

{

aFireball.Value.Position = new Vector2(aFireball.Value.Position.X - 10, aFireball.Value.Position.Y);

aFireballVisible = true;

}

}

}

if (aFireballVisible == false)

{

_fireballsElapsed = 0;

}

}

this is just modified code from http://www.xnaresources.com/links.asp action=getlink&categoryID=2&linkID=25

also once I get this solved would it be easier to use farseer physics to do jumping and falling

also would I use the GetPressedKeys property so my character can jump and shoot at the same time

Thanks for all the help again...



Answer this question

fireballs follow character?

  • Amos Soma

    You could do the same thing with a Vector2, the logic is still the same. Sometimes I like to keep them in Vector3 so I can use the built in structure BoundingBox.


  • danni123

    Hmmm....that code looks strangely familiar yet very alien. I can't put my finger on it, but I feel like I should know the answer to this question...I'll think about it tonight and see what I can come up with tomorrow. (if I could just figure out where I've seen code like this before I'm sure I'd be able to just "get it" )


  • Patrick Tremblay

    I can't quite grasp what you want your bullet to do. I am guessing _tif is your character object   Are these fireballs that circles around the ship/character   Or are they more like bullets that flys away from the starting poisition

    If it's more like bullets...  You probably shouldn't updating the fireball's position base on _tif's direction in UpdateFireballs.  In the second block where you are doing "aFireball.Value.Position.X > aFireball.Value.StartPosition.X + 300", wouldn't it be - 300 instead   If you want them to fly away from starting point, but not need to track ship/character's movement, it might be easier to keep a current position, and a direction/velocity vector.  Every UpdateFireballs call, recalc the current position by adding the vector to current position.

    And instead of creating a new Vector2 object every time a bullet going out of bound, it would probably be better just to update the value of the vector object.

    EDIT: and you can restructure your if statements to reduce the number of comparison/boolean operations.  Makes it little easier to read too.

    Have fun!



  • JohnBurton

    I'm not sure I understand the logic of the code myself. But assuming that you are tyring to create a bullet like fireball as described by waruwaru, then I would suggest modifying your fireball structure to possess the following:

    public class FireBall {
    private Vector3 _velocity;
    private Vector3 _position;

    public Vector3 Velocity { get { return _velocity; } set { _velocity = value; } }

    public FireBall( Vector3 position, Vector3 velocity )
    {
    _position = position;
    _velocity = velocity;
    }

    ...

    Update(GameTime gameTime)
    {
    _position += _velocity * (float)gameTime.ElapsedGameTime.Millisecond / 1000.0f;
    }
    }

    This way, when you create your fire ball, you simply

    List<FireBall> _fireballs = new List<FireBall>();
    _fireballs.Add(new FireBall( player.position, new Vector3( dir.x, dir.y, 0));

    Then in the Game.Update(GameTime gameTime) method, simply update all your fireballs like so:

    Update(GameTime gameTime)
    {
    foreach (FireBall fireball in _fireballs)
    {
    fireball.Update(gameTime);
    }
    ...
    }

    This way, the fireballs move independently of what the player is doing. They will "think" for themselves so to speak.

    This is just one method of achieving a bullet that you fire and forget, but it demonstrates the thinking behind the setup.


  • Bubba76

    B1narySh0t wrote:

    I'm not sure I understand the logic of the code myself. But assuming that you are tyring to create a bullet like fireball as described by waruwaru, then I would suggest modifying your fireball structure to possess the following:

    public class FireBall {
    private Vector3 _velocity;
    private Vector3 _position;

    public Vector3 Velocity { get { return _velocity; } set { _velocity = value; } }

    public FireBall( Vector3 position, Vector3 velocity )
    {
    _position = position;
    _velocity = velocity;
    }

    ...

    Update(GameTime gameTime)
    {
    _position += _velocity * (float)gameTime.ElapsedGameTime.Millisecond / 1000.0f;
    }
    }

    This way, when you create your fire ball, you simply

    List<FireBall> _fireballs = new List<FireBall>();
    _fireballs.Add(new FireBall( player.position, new Vector3( dir.x, dir.y, 0));

    Then in the Game.Update(GameTime gameTime) method, simply update all your fireballs like so:

    Update(GameTime gameTime)
    {
    foreach (FireBall fireball in _fireballs)
    {
    fireball.Update(gameTime);
    }
    ...
    }

    This way, the fireballs move independently of what the player is doing. They will "think" for themselves so to speak.

    This is just one method of achieving a bullet that you fire and forget, but it demonstrates the thinking behind the setup.

    Thats exactly what I'm trying to achive im doing a mario style game... I'll work on the bouncing fireballs later just trying to figure out the logic behind a bullet that moves on its own.

    Question though, why are you using a vector3 and not vector2


  • fireballs follow character?