Hello, in my game I am wanting to move my character and shoot a projectile simultaneously, but I need the projectile to emit from the character. Thus I am using the same variable for starting positions of character and projectile.

When I shoot projectile, it goes from player to wall, then character freezes and no longer able to move him.

Is there a way I can use same variables to keep projectile starting at my character, but seperate them so one doesn't interfere with the other?

Many ways. Here's one. Make the projectile a different object completely. Upon creation, it copies its location from the player. After that, it's completely independent.

Edited 3 Years Ago by Moschops

struct animate {
    int drawable;
    int animationFrames;
    char *frames[TOTAL_FRAMES];
    int charWidth;
    int charHeight;
    int xCoord;
    int yCoord;
    int xMove;
    int yMove;
    int currentFrame;
};

//Actual struct for character movement
struct animate character[] = 
{
    {
    //Character moving left
    1,
    3,
    {
        "PIX\\Character\\left_r.gif",
        "PIX\\Character\\left_still.gif",
        "PIX\\Character\\left_l.gif",
        "PIX\\Character\\left_still.gif",
    },
    50,
    50,
    x9,
    y9,
    5,
    0,
    0,
},

{
    //Character moving right
    1,
    3,
    {
        "PIX\\Character\\right_r.gif",
        "PIX\\Character\\right_still.gif",
        "PIX\\Character\\right_l.gif",
        "PIX\\Character\\right_still.gif",
    },
    50,
    50,
    x9,
    y9,
    -5,
    0,
    0,
},

{
    //Character moving up
    1,
    3,
    {
        "PIX\\Character\\up_r.gif",
        "PIX\\Character\\up_still.gif",
        "PIX\\Character\\up_l.gif",
        "PIX\\Character\\up_still.gif",
    },
    50,
    50,
    x9,
    y9,
    0,
    -5,
    0,
},

{
    //Character moving down
    1,
    3, //number of frames for array to consider
    {
        "PIX\\Character\\down_r.gif",
        "PIX\\Character\\down_still.gif",
        "PIX\\Character\\down_l.gif",
        "PIX\\Character\\down_still.gif",
    },
    50, //width of character
    50, //Height of character
    x9, //starting position
    y9,
    0,
    5, //Character movement
    0,
},
};
int thunderx9;
int thundery9;
struct animate thunderballAttack[] =
{
    {
        1,
        2,
        {
            "PIX\\thunderball\\thunderball.gif",
            "PIX\\thunderball\\thunderball2.gif",
            "PIX\\thunderball\\thunderball3.gif",
        },
        30,
        30,
        0,
        0,
        10,
        0,
        0,
    {

This is how I am currently defining the animations, then in my thunderball animation function I define thunderballAttack[0].xCoord = x9, and .yCoord = y9, then it enters a while loop to animate when the right button is pressed.

Is this correctly copying coordinates then separating itself? Or would it be best to just include thunderball animations into charactr[] struct?

You can't just make your thunderball animation a while loop. When you have that loop going, no other code outside of that loop will execute, and this is likely the reason your character freezes on screen every time you shoot a bullet.

If you want these things to happen simultaneously, you need one, gigantic loop that will encompass both the code for your character, and the code for the projectile. Basically, it will end up looking something like this:

while(1) //Or whatever condition you want the loop to have.
{
    //Code to make your character move little bit...
    //Code to make your projectile move a little bit, if it exists...
    //Code to draw your stuff onto the screen...
    //Code to have a little delay, so your program doesn't run insanely fast.
}

Essentially what you will have written at this point is your standard game loop.

Edited 3 Years Ago by Tumlee

Hmm... alright. I did some work on it in between my post and yours and believe I am much closer to what you mentioned. I will do more work on it tomorrow and see what I can come up with.

Also, +1 for a giromon picture :P

This article has been dead for over six months. Start a new discussion instead.