9 Replies - 560 Views - Last Post: 08 April 2013 - 08:57 PM Rate Topic: -----

#1 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Shooting projectile from character animation trouble.

Posted 01 April 2013 - 08:17 PM

switch (move){
		
	case 1://left

		readimagefile("PIX\\Character\\left_r.gif",x9,y9,x9+50,y9+50);Sleep(15);
		x9 = x9 + 5;
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\Character\\left_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
		x9 = x9 + 5;
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\Character\\left_l.gif",x9,y9,x9+50,y9+50);Sleep(15);
		x9 = x9 + 5;
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\Character\\left_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
		x9 = x9 + 5;
		putimage(0, 0, background, COPY_PUT);
		
		if(kbhit()){
			getch();
			if(getch() == ' '){
		readimagefile("PIX\\thunderball\\thunderball.gif", x9+10, y9+40, x9+40, y9+70);Sleep(80);
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\thunderball\\thunderball2.gif", x9+10, y9+40, x9+40, y9+70);Sleep(80);
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\thunderball\\thunderball3.gif", x9+10, y9+40, x9+40, y9+70);Sleep(80);
		putimage(0, 0, background, COPY_PUT);
		x9--;}
		if(x9 == 30){
			readimagefile("PIX\\thunderball\\thunderball_contact.gif", mx, my, mx+30, my+30);Sleep(80);
			putimage(0, 0, background, COPY_PUT);
			readimagefile("PIX\\thunderball\\thunderball_contact2.gif", mx, my, mx+30, my+30);Sleep(80);
			putimage(0, 0, background, COPY_PUT);
			readimagefile("PIX\\thunderball\\thunderball_contact3.gif", mx, my, mx+30, my+30);Sleep(80);
			putimage(0, 0, background, COPY_PUT);
		}}
		break;

	case 2://up

		readimagefile("PIX\\Character\\up_r.gif",x9,y9,x9+50,y9+50);Sleep(15);
		y9 = y9 - 5;
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\Character\\up_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
		y9 = y9 - 5;
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\Character\\up_l.gif",x9,y9,x9+50,y9+50);Sleep(15);
		y9 = y9 - 5;
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\Character\\up_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
		y9 = y9 - 5;
		putimage(0, 0, background, COPY_PUT);
		
		if(kbhit()){
			getch();
			if(getch() == ' '){
		readimagefile("PIX\\thunderball\\thunderball.gif", x9+10, y9+40, x9+40, y9+70);Sleep(80);
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\thunderball\\thunderball2.gif", x9+10, y9+40, x9+40, y9+70);Sleep(80);
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\thunderball\\thunderball3.gif", x9+10, y9+40, x9+40, y9+70);Sleep(80);
		putimage(0, 0, background, COPY_PUT);
		y9--;}
		if(y9 == 30){
			readimagefile("PIX\\thunderball\\thunderball_contact.gif", mx, my, mx+30, my+30);Sleep(80);
			putimage(0, 0, background, COPY_PUT);
			readimagefile("PIX\\thunderball\\thunderball_contact2.gif", mx, my, mx+30, my+30);Sleep(80);
			putimage(0, 0, background, COPY_PUT);
			readimagefile("PIX\\thunderball\\thunderball_contact3.gif", mx, my, mx+30, my+30);Sleep(80);
			putimage(0, 0, background, COPY_PUT);
		}}
		break;

	case 3://right

		readimagefile("PIX\\Character\\right_r.gif",x9,y9,x9+50,y9+50);Sleep(15);
		x9 = x9 - 5;
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\Character\\right_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
		x9 = x9 - 5;
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\Character\\right_l.gif",x9,y9,x9+50,y9+50);Sleep(15);
		x9 = x9 - 5;
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\Character\\right_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
		x9 = x9 - 5;
		putimage(0, 0, background, COPY_PUT);
		
		if(kbhit()){
			getch();
			if(getch() == ' '){
				while(x9 < 770){
		readimagefile("PIX\\thunderball\\thunderball.gif", x9+10, y9+40, x9+40, y9+70);Sleep(80);
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\thunderball\\thunderball2.gif", x9+10, y9+40, x9+40, y9+70);Sleep(80);
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\thunderball\\thunderball3.gif", x9+10, y9+40, x9+40, y9+70);Sleep(80);
		putimage(0, 0, background, COPY_PUT);
		x9++;}}
		if(x9 == 770){
			readimagefile("PIX\\thunderball\\thunderball_contact.gif", mx, my, mx+30, my+30);Sleep(80);
			putimage(0, 0, background, COPY_PUT);
			readimagefile("PIX\\thunderball\\thunderball_contact2.gif", mx, my, mx+30, my+30);Sleep(80);
			putimage(0, 0, background, COPY_PUT);
			readimagefile("PIX\\thunderball\\thunderball_contact3.gif", mx, my, mx+30, my+30);Sleep(80);
			putimage(0, 0, background, COPY_PUT);
		}}
		break;


	case 4://down
		 
		readimagefile("PIX\\Character\\down_l.gif",x9,y9,x9+50,y9+50);Sleep(15);
		y9 = y9 + 5;
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\Character\\down_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
		y9 = y9 + 5;
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\Character\\down_r.gif",x9,y9,x9+50,y9+50);Sleep(15);
		y9 = y9 + 5;
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\Character\\down_still.gif",x9,y9,x9+50,y9+50);Sleep(15);
		y9 = y9 + 5;
		putimage(0, 0, background, COPY_PUT);
		
		if(kbhit()){
			getch();
			if(getch() == ' '){
				while(y9 < 770){
		readimagefile("PIX\\thunderball\\thunderball.gif", x9+10, y9+40, x9+40, y9+70);Sleep(80);
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\thunderball\\thunderball2.gif", x9+10, y9+40, x9+40, y9+70);Sleep(80);
		putimage(0, 0, background, COPY_PUT);
		readimagefile("PIX\\thunderball\\thunderball3.gif", x9+10, y9+40, x9+40, y9+70);Sleep(80);
		putimage(0, 0, background, COPY_PUT);
		y9 = y9 + 10;}}
		if(y9 == 770){
			readimagefile("PIX\\thunderball\\thunderball_contact.gif", x9, y9, x9+30, y9+30);Sleep(80);
			putimage(0, 0, background, COPY_PUT);
			readimagefile("PIX\\thunderball\\thunderball_contact2.gif", x9, y9, x9+30, y9+30);Sleep(80);
			putimage(0, 0, background, COPY_PUT);
			readimagefile("PIX\\thunderball\\thunderball_contact3.gif", x9, y9, x9+30, y9+30);Sleep(80);
			putimage(0, 0, background, COPY_PUT);
		}}
		break;

	default:
		readimagefile("PIX\\Character\\down_still.gif", x9, y9, x9+50, y9+50);
		putimage(0, 0, background, COPY_PUT);


This is my character move function as well as basic attack function. My big issue right now is that the character follows the thunderball as it moves in whichever direction (character doesn't animate, but he cannot move until thunderball collides with wall, then character is at wall because he was dragged by thunderball).

I can see why this occurs (using x9 and y9 for character and thunderball). But I don't know how to implement this with proper coordinates to always be right in front of the character no matter which direction he is facing. I tried having it be a seperate function and it just didn't work at all lol.

(C language on Visual Studio 2012)

Is This A Good Question/Topic? 0
  • +

Replies To: Shooting projectile from character animation trouble.

#2 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Shooting projectile from character animation trouble.

Posted 02 April 2013 - 12:55 PM

Ok, I fixed the animation issue. My character now stays where he was when he shot the thunderball. However, Now I am wondering how I might get the function to work so I can shoot the thunderball and still move my character. I am now shooting, but my character cannot move until the thunderball animation ends.
Was This Post Helpful? 0
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1643
  • View blog
  • Posts: 3,120
  • Joined: 30-May 10

Re: Shooting projectile from character animation trouble.

Posted 03 April 2013 - 11:32 AM

You need to think in these terms -> http://en.wikipedia....80%93controller

That is, you want a clear and clean separation of the control aspects (up, down, left, right, fire, hit, gameover etc etc) from the act of drawing the scene on screen.

Here is a prototype idea to think about.
#define MAX_FRAMES  4
#define MAX_THINGS  10

struct animate {
  int     drawable;
  int     numAnimationFrames;
  char   *frames[MAX_FRAMES];
  int     glyphWidth;
  int     glyphHeight;
  int     xPos;
  int     yPos;
  int     deltaX;
  int     deltaY;
  int     currentFrame;
};

struct animate things[MAX_THINGS] = {
  // You, going left
  {
    1,
    4,
    {
      "PIX\\Character\\left_r.gif",
      "PIX\\Character\\left_still.gif",
      "PIX\\Character\\left_l.gif",
      "PIX\\Character\\left_still.gif"
    },
    50,
    50,
    0,  // pos
    0,
    0,  // speed
    0,
    0,  // #frame
  },
  // Thunderball, going left
  {
    0,
    3,
    {
      "PIX\\thunderball\\thunderball.gif",
      "PIX\\thunderball\\thunderball2.gif",
      "PIX\\thunderball\\thunderball3.gif",
    },
    40,
    70,
    0,  // pos
    0,
    0,  // speed
    0,
    0,  // #frame
  },
  // Thunderball, hitting
  {
    0,
    3,
    {
      "PIX\\thunderball\\thunderball_contact.gif",
      "PIX\\thunderball\\thunderball_contact2.gif",
      "PIX\\thunderball\\thunderball_contact3.gif",
    }
    30,
    30,
    0,  // pos
    0,
    0,  // speed
    0,
    0,  // #frame
  },
};

void doOneAnimationFrame ( void ) {
  for ( i = 0 ; i < MAX_THINGS ; i++ ) {
    if ( ! things[i].drawable ) continue;   // not this time

    // something to draw
    readimagefile( things[i].frames[things[i].currentFrame],
                   things[i].xPos,
                   things[i].yPos,
                   things[i].xPos + things[i].glyphWidth,
                   things[i].yPos + things[i].glyphHeight );

    // move things along for next time.
    things[i].currentFrame++;
    if ( things[i].currentFrame == things[i].numAnimationFrames ) {
      things[i].currentFrame = 0;

      // possibly disable this animation, 
      // until the control loop decides to do something
      things[i].drawable = 0;
    }
    
    things[i].xPos += things[i].deltaX;
    things[i].yPos += things[i].deltaY;
  }
  putimage(0, 0, background, COPY_PUT);
  Sleep(10);  // GCD of all your sleep times
}

void game ( void ) {
  int gameOver = 0;
  do {
    testKeys();
    gameOver = isPlayerDead();
    doOneAnimationFrame();
  } while ( !gameOver );
}



Where testKeys() is a function to poll the keyboard (without blocking), and update various entries in the things array (mostly, this will be setting the appropriate directions of objects to be either .drawable = true (or not).
Was This Post Helpful? 0
  • +
  • -

#4 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Shooting projectile from character animation trouble.

Posted 07 April 2013 - 10:35 AM

Ok, that code you posted in my topic about shooting projectile from character. I have been mulling it over trying to understand how to use it... (I am new to structs), and I finally understood how it operates and have made my own rough copy of a character moving struct based on what you gave me.

But I am wondering if your doOneAnimationFrame() function works out for any direction the character might move, or is this where I need to get keyboard input and based on which key was pressed, move in that direction. Like in:

1
character[i].xCoord += character[i].xMove;
2
character[i].yCoord += character[i].yMove;


Do I want 4 of these? All based on which key was pressed?

Also, can you explain the for() loop at the beginning of doOneAnimation()? What is the significance of using 10 for MAX_THINGS?

Thanks for any help

This post has been edited by Bawnawgwa: 07 April 2013 - 10:37 AM

Was This Post Helpful? 0
  • +
  • -

#5 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Shooting projectile from character animation trouble.

Posted 07 April 2013 - 08:25 PM

Or does anyone know where I would want to place keyboard inputs to initialize a specific direction based on salem_c's code above? (mine is similar)
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3548
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: Shooting projectile from character animation trouble.

Posted 07 April 2013 - 08:34 PM

Yes. In the the testKeys() function.
Was This Post Helpful? 0
  • +
  • -

#7 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Shooting projectile from character animation trouble.

Posted 07 April 2013 - 11:20 PM

//struct template for animating character movement
struct animate {
	int draws;
	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[MAX_LOOPS] = 
{
	{
	//Character moving left
	2,
	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,
	x9 = x9 + 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,
	x9 = x9 + 5,
	0,
	0,
},

{
	//Character moving up
	3,
	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,
	y9 = y9 - 5,
	0,
},

{
	//Character moving down
	4,
	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,
	y9 = y9 + 5, //Character movement
	0,
},
//thunderball structs!
{
	5, //thunderball going right
	3,
	{
		"PIX\\thunderball\\thunderball.gif"
		"PIX\\thunderball\\thunderball2.gif"
		"PIX\\thunderball\\thunderball3.gif"
	},
	30,
	30,
	x9+40,
	y9+10,
	x9 = x9 + 10,
	0,
	0,
},
{
	6, //thunderball going left
	3,
	{
		"PIX\\thunderball\\thunderball.gif"
		"PIX\\thunderball\\thunderball2.gif"
		"PIX\\thunderball\\thunderball3.gif"
	},
	30,
	30,
	x9-10,
	y9+10,
	x9 = x9 - 10,
	0,
	0,
},
{
	7, //thunderball going up
	3,
	{
		"PIX\\thunderball\\thunderball.gif"
		"PIX\\thunderball\\thunderball2.gif"
		"PIX\\thunderball\\thunderball3.gif"
	},
	30,
	30,
	x9+10,
	y9-10,
	0,
	y9 = y9 - 10,
	0,
},
{
	8, //thunderball going down
	3,
	{
		"PIX\\thunderball\\thunderball.gif"
		"PIX\\thunderball\\thunderball2.gif"
		"PIX\\thunderball\\thunderball3.gif"
	},
	30,
	30,
	x9+10,
	y9+40,
	0,
	y9 = y9 + 10,
	0,
},
};

void character_animation()
{
	int area = imagesize(0, 0, 799, 799);
	bkimage = malloc(area);
	getimage(0, 0, 799, 799, bkimage);
	
	check_char();
	
	for (int i = 0; i < 4; i++){
		//if( ! character[i].draws)
			//continue;
	testKeys();
		while( !kbhit())
			Sleep(100);
		
		if(character[i].draws == 1){
			
	//Puts image of character on screen based on inputs from struct
	readimagefile(character[i].frames[character[i].currentFrame],
		character[i].xCoord,
		character[i].yCoord,
		character[i].xCoord + character[i].charWidth,
		character[i].yCoord + character[i].charHeight );
	
	character[i].currentFrame++; //increments the "frame" by one, thus only animating one image at a time
	if(character[i].currentFrame == character[i].animationFrames){
		character[i].currentFrame = 0;} //When frame 4 is reached, it resorts back to first frame and restarts
		//character[i].draws = 0;}
	
	//These next two lines of code dictate the movement of the character
	character[i].xCoord = character[i].xCoord + 5;
		}

		if(character[i].draws == 2){
			
	//Puts image of character on screen based on inputs from struct
	readimagefile(character[i].frames[character[i].currentFrame],
		character[i].xCoord,
		character[i].yCoord,
		character[i].xCoord + character[i].charWidth,
		character[i].yCoord + character[i].charHeight );
	
	character[i].currentFrame++; //increments the "frame" by one, thus only animating one image at a time
	if(character[i].currentFrame == character[i].animationFrames){
		character[i].currentFrame = 0;} //When frame 4 is reached, it resorts back to first frame and restarts
		//character[i].draws = 0;}
	
	//These next two lines of code dictate the movement of the character
	character[i].xCoord = character[i].xCoord - 5;
		}

		if(character[i].draws == 3){
			
	//Puts image of character on screen based on inputs from struct
	readimagefile(character[i].frames[character[i].currentFrame],
		character[i].xCoord,
		character[i].yCoord,
		character[i].xCoord + character[i].charWidth,
		character[i].yCoord + character[i].charHeight );
	
	character[i].currentFrame++; //increments the "frame" by one, thus only animating one image at a time
	if(character[i].currentFrame == character[i].animationFrames){
		character[i].currentFrame = 0;} //When frame 4 is reached, it resorts back to first frame and restarts
		//character[i].draws = 0;}
	
	//These next two lines of code dictate the movement of the character
	character[i].yCoord = character[i].yCoord - 5;
		}

		if(character[i].draws == 4){
			
	//Puts image of character on screen based on inputs from struct
	readimagefile(character[i].frames[character[i].currentFrame],
		character[i].xCoord,
		character[i].yCoord,
		character[i].xCoord + character[i].charWidth,
		character[i].yCoord + character[i].charHeight );
	
	character[i].currentFrame++; //increments the "frame" by one, thus only animating one image at a time
	if(character[i].currentFrame == character[i].animationFrames){
		character[i].currentFrame = 0;} //When frame 4 is reached, it resorts back to first frame and restarts
		//character[i].draws = 0;}
	
	//These next two lines of code dictate the movement of the character
	character[i].yCoord = character[i].yCoord + 5;
		}

	if(character[i].draws == 5){
			
	//Puts image of character on screen based on inputs from struct
	readimagefile(character[i].frames[character[i].currentFrame],
		character[i].xCoord,
		character[i].yCoord,
		character[i].xCoord + character[i].charWidth,
		character[i].yCoord + character[i].charHeight );
	
	character[i].currentFrame++; //increments the "frame" by one, thus only animating one image at a time
	if(character[i].currentFrame == character[i].animationFrames){
		character[i].currentFrame = 0;} //When frame 4 is reached, it resorts back to first frame and restarts
		//character[i].draws = 0;}
	
	//These next two lines of code dictate the movement of the character
	character[i].xCoord = character[i].xCoord + 10;
		}
	}
	putimage(0, 0, bkimage, COPY_PUT);
	Sleep(10);
}
void testKeys(){
	for(int i = 0; i < MAX_LOOPS; i++){
	if(kbhit()){
		int c = getch();
		if(c == 'd'){
			character[i].draws = 1;}
		if(c == 'a'){
			character[i].draws = 2;}
		if(c == 's'){
			character[i].draws = 4;}
		if(c == 'w'){
			character[i].draws = 3;}
		if(c == '1'){
			character[i].draws - 5;}
	}
	}
}


Uff dah... that is long. Anyway this is what I have come up with so far.

At the moment it is moving my character, but it is creating 3 instances of my character haha... when he moves in whatever direction he just spins in circles while moving in the right direction (at least movement is working :P).

And thunderball is not showing up at all. Do I need a whole seperate struct animate thunderball[MAX_FRAMES] = thing going on or get it to work under the character one? (yes I know I only put an instance for one thunderball, but I just wanted to test out 1 aaaaaaaand it didn't work as expected).
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3548
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: Shooting projectile from character animation trouble.

Posted 08 April 2013 - 05:19 AM

You are back again to doing your input checks in the middle of your animation. (See lines 171-173.) The point of Salem's post on this thread, and my posts on the other thread is to separate your input check logic from your animation logic.

The following blogpost is in Java, but it does a pretty good explanation of what you need to do and matches your 2D problem: http://www.cokeandco...info/tut2d.html
Was This Post Helpful? 0
  • +
  • -

#9 Bawnawgwa  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-January 13

Re: Shooting projectile from character animation trouble.

Posted 08 April 2013 - 11:43 AM

ok I moved testKeys() into the main rungame function before character_animation(), and moved the coordinate increments to the testKeys() to (to make a certain key increment a certain point, thus moving character).

But I am still getting the 4 images all facing a different way drawn at once thing hahaha. I will need to study your link further Skydiver. Thank you for that :P
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3548
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: Shooting projectile from character animation trouble.

Posted 08 April 2013 - 08:57 PM

The draws field is meant to help the frame drawer figure out whether a particular animate structure is to be rendered or not. You are smashing those field values in your testKeys() function, and miscuing in your character_animation() function because you are comparing against fixed values instead of comparing against a game state variable that keeps track of which direction your character should be facing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1