Undefined fireball movement behavior

Posted by optimisez on Game Development See other posts from Game Development or by optimisez
Published on 2013-07-03T09:41:07Z Indexed on 2013/07/03 11:19 UTC
Read the original article Hit count: 322

Filed under:
|
|
|
|

bug

Demonstration video

I try to do after the player shoot 10 times of fireball, then delete all the fireball objects and recreate a 10 new set of fireball objects. I did it but there is a weird bug happens that sometimes the fireball will come out from top and move to the right after shooting a few times. All the 10 fireballs should follow the player all the time and all the fireball should come out from player even after a new set of fireballs is recreated. Any ideas to fix it?

Variables

typedef struct gameObject{
float X;
float Y;
int length;
int height;
bool action;
};

// Fireball
#define FIREBALL_NUM 10
LPDIRECT3DTEXTURE9 fireball = NULL;
RECT fireballRect;
gameObject *fireballDest = new gameObject[FIREBALL_NUM];
int iFireBallAnimation;
int fireballCount = 0;

Set up Fireball

void setUpFireBall()
{
    // Initialize destination rectangle, rectangle height and length
    for (int i = 0; i < FIREBALL_NUM; i++)
    {
        fireballDest[i].X = 0;
        fireballDest[i].Y = 0;

        fireballDest[i].length = fireballRect.right - fireballRect.left;
        fireballDest[i].height = fireballRect.bottom - fireballRect.top;
    }

    iFireBallAnimation = fireballRect.right - fireballRect.left;

    // Initialize boolean 
    for (int i = 0; i < FIREBALL_NUM; i++)
    {   
        fireballDest[i].action = false;
    }
}

Initialize fireball

void initFireball()
{
    hr = D3DXCreateTextureFromFileEx(d3dDevice, "fireball.png", 512, 512,
    D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,
    D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 0),
    NULL, NULL, &fireball);

    // Initialize source rectangle
    fireballRect.left = 0;
    fireballRect.top = 256;
    fireballRect.right = 64;
    fireballRect.bottom = 320;

    setUpFireBall();
}

Update fireball

void update()
{
    updateAnimation();
    updateAI();
    updatePhysics();
    updateGameState();
}


void updatePhysics()
{
    motion();
    collison();
}

void motion()
{
    playerMove();
    playerJump();
    playerGravity();
    shootFireball();
    fireballFollowPlayer();
}

void shootFireball()
{
    if (keyArr['Z'])
        fireballDest[fireballCount].action = true;

    if (fireballDest[fireballCount].action)
    {
        fireballDest[fireballCount].X += 10;
        if (fireballDest[fireballCount].X > 800)
            fireballCount++;
    }
}

void fireballFollowPlayer()
{
    for (int i = 0; i < FIREBALL_NUM; i++)
    {
        if (fireballDest[i].action == false)
        {
            fireballDest[i].X = playerDest.X - 30;
            fireballDest[i].Y = playerDest.Y - 14;
        }
    }
}

void updateGameState()
{
    // When no more fireball left, rearm fireball
    if (fireballCount == FIREBALL_NUM)
    {
        delete[] fireballDest;
        fireballDest = new gameObject[10];
        fireballCount = 0;
        setUpFireBall();
    }
}

Render fireball

void renderFireball()
{
    for (int i = 0; i < FIREBALL_NUM; i++)
    {
        if (fireballDest[i].action)
            sprite->Draw(fireball, &fireballRect, NULL, &D3DXVECTOR3(fireballDest[i].X, fireballDest[i].Y, 0), D3DCOLOR_XRGB(255,255, 255));
    }
}  

© Game Development or respective owner

Related posts about c++

Related posts about 2d