Animating sprite character using struct issues.

Ok, so I attempted using a struct to animate my character, since my original hard coding produced poor performance. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//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[TOTAL_FRAMES] = 
{
	{
	//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,
	x9 = x9 + 5,
	0,
	0,
},

{
	//Character moving right
	2,
	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,
},
};

void character_animation()
{
	
	for (int i = 0; i < MAX_LOOPS; i++){
		if( ! character[i].draws)
			continue;

	//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].xMove;
	character[i].yCoord += character[i].yMove;
	}	
	//putimage(0, 0, bkimage, COPY_PUT);
	Sleep(10);
}


TOTAL_FRAMES = 4, and MAX_LOOPS = 10.

Right now I am getting an unhandled exception access violation. I am needing the character to move in this way because I will be implementing an attack animation whenever space bar is pressed in a similar manner.

I attempted a trouble shoot and I would find out when the unhandled exception happens, but I don't understand how to fix it haha.

It currently prints the character facing all four directions then breaks. I will also be needing to get keyboard input for this. I will be having it move up with w, right - d, left - a, down - s. If I put in the key commands will it keep from breaking since it will only animate one thing at a time instead of everything?
character[i] is out of bounds when i >= TOTAL_FRAMES.
Hmm... So I need MAX_LOOPS to be 4? It should therefore not exceed total_frames.

I shall attempt this.
Ok, so changing value of 'i' to not exceed Total_frames still spits out unhandled exceptions. I am wondering if I put in keyboard inputs it might help solve the issue?

Right now the code loops through and draws the character facing all directions on top of each other then gives unhandled exception.

If I put in keyboard inputs to get 'wasd' movement, then it should ONLY draw one image at a time based on which key I hit, then cause the character to move in that direction.

But I do not know where to place these keyboard hits :/
Topic archived. No new replies allowed.