-3
\$\begingroup\$

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));
    }
}  
\$\endgroup\$
2
\$\begingroup\$

You are setting fireballCount to 0 in updateGameState, so I think the problem is that what is being animated and drawn is fireballDest[0], which is the first of the new fireballs and was just initialised with a position of (0, 0), which, presumably, is the upper right corner.

\$\endgroup\$
  • \$\begingroup\$ Thanks. But why? Does the computer cannot update in time? \$\endgroup\$ – optimisez Jul 3 '13 at 11:55
  • \$\begingroup\$ Thanks for accepting. No, the computer's doing what you tell it. But what are you trying to achieve? Why do you have a fireball-limit of 10, if you immediately replenish them anyway? \$\endgroup\$ – HumanCatfood Jul 3 '13 at 13:22
  • \$\begingroup\$ I want to shoot unlimited fireball and if a fireball hits enemy it will disappear immediately. I wonder if I just set all the 10 fireballs back to the player position to follow the player for replenishing after used up the 10 fireballs, will it hit the enemies during this process? \$\endgroup\$ – optimisez Jul 3 '13 at 14:55
  • \$\begingroup\$ If you want the fireballs to follow the player, maybe you should try looking at steering behaviours: gamedev.stackexchange.com/questions/44400/… \$\endgroup\$ – HumanCatfood Jul 4 '13 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.