Ugh... really stupid error, please someone help

Started by
12 comments, last by Muzlack 22 years, 1 month ago
This is very stupid.. Without ever changing the data, an array's values change. Let me show you what I mean:
  
void MakeAnimation(SPRITEPTR sprite, int animindex, int numframes, int anim[]) {
	fprintf(fp2,"%d\n",sprite->anims[0][0]);
	fprintf(fp2,"%d\n",sprite->anims[0][1]);
	sprite->anims[animindex]=anim;
	sprite->curanimframes=numframes;
	sprite->indexinanim=0;
}
  
Ok, the one above me puts 0\n1\n0\n1\n (not really the \n, but you understand) this is the correct data. However,
    
void UpdateSprite(SPRITEPTR sprite) {
	fprintf(fp2,"%d\n",sprite->anims[0][0]);
	fprintf(fp2,"%d\n",sprite->anims[0][1]);
	if(sprite->counter==sprite->updatecounter) {
		sprite->indexinanim++;
		if(sprite->indexinanim>sprite->curanimframes) sprite->indexinanim=0;
		sprite->curframe=sprite->anims[sprite->curanim][sprite->indexinanim];
		sprite->counter=0;
	}
	sprite->counter++;
}
    
Produces: -858993460 -858993460 0 -858993460 0 2 0 2 0 2 0 1 -858993460 -858993460 0 -858993460 I'm not sure where this starts repeating, but I don't understand. You don't have to pay any attention to anything except what it fprintf s. This doesn't make any sense! I did a search for sprite->anim[ and only these places come up. Anyone have any clue how this can change inbetween this time? Ugh.. this is really irritating.. *EDIT* I'm sorry! haha! I forgot to mention something that makes this even more peculiar: Only the enemies do this, the normal spaceship works fine! How can this be? Also to add, before I added in animations for the enemies, this also worked fine, but I never called UpdateSprite or MakeAnimation, so who knows.. Edited by - Muzlack on February 18, 2002 9:55:51 PM
--Muzlack
Advertisement
use %i instead of %d. You are printing a double where you should be printing an int.
NewDeal: %d prints an int, not a double. The code''s correct there.

Muzlack: Can we see some code that calls these functions? Are SPRITE objects created, but not initialized when they hit the UpdateSprite function?

--Ben Carter
Romans 8:38-39
--Ben CarterRomans 8:38-39
Oops
Sorry... I was trying to avoid this.. The reason is because I am well into the development of the game, but yes, I can show you the code, but it''ll be a bit hidden I guess you could say.. but just do a search and you''ll see it. I''ve taken away alot of the code so I don''t have practically my whole game in here, most of it is just summary
  SPRITE ship;SPRITE back;SPRITE bullet;SPRITE startup;SPRITE hp;int WINAPI WinMain(HINSTANCE hinstance,				   HINSTANCE hPrevInstance,                     LPSTR     lpCmdLine,                     int       nCmdShow){	srand((unsigned)time(NULL)); //JUST FYI, I TOOK OUT THE WINDOWS CODE HERE	initdd(800,600,32); //initialize dd with 800x600x32 for the display mode	initdi(hinstance); //initialize di with keyboard	//create the sprites	CreateSprite(&back,800,600,"level1.bmp");	CreateSprite(&ship, 100,100, "space.bmp");	//CFFromCell means CreateFrameFromCell(the sprite        // then the frame #, then the x, y in the image	CFFromCell(&ship,0,0,0); //frame 0 at 0,0	CFFromCell(&ship,1,1,0); //frame 1 at 1,0	CFFromCell(&ship,2,2,0); //frame 2 at 2,0	CFFromCell(&ship,3,3,0); //frame 3 at 3,0	CFFromCell(&ship,4,0,1); //frame 4 at 1,0	CFFromCell(&ship,5,1,1); //frame 5 at 1,1	CFFromCell(&ship,6,2,1); //frame 6 at 2,1	CFFromCell(&ship,7,3,1); //frame 7 at 3,1	assignrect(back.frames[0], 0,0, 800,600);//get the back image into 0	int shoot[]={6,7}; //integer animations	int fleft[]={4,5}; //frames for fly left	int fright[]={2,3}; //frames for fly right	int fly[]= {0,1}; //frames for fly straight	ship.y=500; //ship''s pos	ship.x=200; //ship''s pos	ship.updatecounter=2; //how often does the ship update?	MakeAnimation(&ship,0,1,fly); //create the straight anim	MakeAnimation(&ship,1,1,fright); //create the right anim	MakeAnimation(&ship,2,1,fleft); //Create the left anim	MakeAnimation(&ship,3,1,shoot);	ship.curanim=0; //set ship''s current animation to normal flying	back.curframe=0; //right now, the back does not animate	CreateSprite(&hp,100,20,"hp.bmp");	CFFromCell(&hp,0,0,0);	//create bullet	CreateSprite(&bullet,2,17,"fire.bmp");	CFFromCell(&bullet,0,0,0);	assignrect(hp.frames[0],0,0,99,19);//get the back image into 0	hp.curframe=0;	initenemies();	Startup();	while (1) {		if(PeekMessage(&msg, NULL, 0,0, PM_REMOVE)) {			if (msg.message==WM_QUIT) break;			TranslateMessage(&msg);			DispatchMessage(&msg);		} else {			if(isbullet==false)bulletspeed=18;			UpdateEnemies();			hp.frames[0].right=CurHp*5;			hp.width=CurHp*5;			GetKeyStatus();			UseKeys();			if(keydown(DIK_ESCAPE)) break; // if you press escape, quit game			UseBounds(); // make sure the ship is within it''s boundaries			fillsurface(lpddsback,0); //fill the back surface with black			UpdateSprite(&ship); //Update the ships   anim			DrawSprite(&back, lpddsback); //''draw the background first, 			DrawSprite(&ship, lpddsback); // then draw the ship						for(int i=0; i<5; i++) {			if(enemies[i].isthere) DrawSprite(&enemies[i].ship,lpddsback);			if(enemies[i].isbullet)DrawSprite(&enemies[i].bullet,lpddsback);			}			if(isbullet){ //Move the bullet				bulletspeed--;				if (bulletspeed<10) bulletspeed=10;				bullet.y-=bulletspeed;				if(bullet.y<0)isbullet=false;				DrawSprite(&bullet,lpddsback);			}			if(isbullet) {			for(int z=0; z<5; z++) {				if(enemies[z].isthere==true) { //can''t hit a dead enemy!					if(contact(&bullet, &enemies[z].ship)){ enemies[z].curhp--; isbullet=false;} //hit the enemy if contact with sprite							}			}			}			DrawSprite(&hp, lpddsback);			lpddsprime->Flip(NULL, DDFLIP_WAIT); //flip the surfaces			Sleep(33); // and synchronize 30fps		}			}	Shutdown();	return(msg.wParam);}  

Jeeze... I know thats hard to keep clean, but hopefully you can pick out the unimportant stuff, because if I started taking it out, I know I would do a bad job, so bare with me.
Now, here is the code for initializing the enemies: (haha, I was about to do a { )
  oid initenemies() {			for(int i=0; i<5; i++) { //5 enemies atm		int enemfly[]= {0,1}; //frames for fly straight	CreateSprite(&enemies[i].ship,47,47,"enemy.bmp"); //load from enemy.bmp	CreateSprite(&enemies[i].bullet,2,17,"enemfire.bmp");	CFFromCell(&enemies[i].bullet,0,0,0);	enemies[i].bullet.curframe=0;	if(i<3) { //1-3 are from 0,0 in enemy.bmp	CFFromCell(&enemies[i].ship,0,0,0);	CFFromCell(&enemies[i].ship,1,2,0);	}	else{ 		CFFromCell(&enemies[i].ship,0,1,0); //4-5 are from 1,0 in enemy.bmp		CFFromCell(&enemies[i].ship,1,3,0); 	}	enemies[i].ship.y=100;	enemies[i].ship.x=200;	enemies[i].isthere=true;	enemies[i].curstate=attack;	enemies[i].oldstate=attack;	enemies[i].state.states[attack]=.2;	enemies[i].state.states[avoid]=.35;	enemies[i].state.states[randommove]=.4;	enemies[i].state.states[useold]=.9;	enemies[i].state.states[still]=1.0;		enemies[i].fullhp=3;	enemies[i].curhp=3;	enemies[i].damage=1;	enemies[i].isbullet=false;	enemies[i].ship.updatecounter=2;	MakeAnimation(&enemies[i].ship,0,1,enemfly);	enemies[i].ship.curanim=0;	enemies[i].curstate=still;	}  

I also edited a lot out of this, but it should be pretty straightforward.
I apologize for having such ugly code atm, Its much easier to pick through once you''ve seen it a hundred times.

Thanks in advance!

Sponge Factory
--Muzlack
--Muzlack
In case you don''t want to sift through this code, I can summarize the updatesprite and createanimation function-callings:
main {
Create the sprites for the ship, bullet, and background;
Initialize all the frames;
Make animations from these frames;
Make the background from an image;
call initenemies();
while(1) {
update the ship;
update the enemies; //here the animation is 0, -74082734871, who knows what, etc...
draw ship;
draw enemies;
sleep;
}
}
initenemies() {
for(int i=0;icreate enemy sprite;
initialize frames for that sprite;
make an animation from that sprite; // here, the animation is 0,1
}
}
--Muzlack
Hmm, does anyone know? there is not a change in the anims array ever, how can it change? and by the looks of it there is a pattern as it only happens to enemies, and the numbers are the same every go round..
--Muzlack
In the function initenemies:
Why is most of the code (except 1 line) outside of the scope of the for block? I mean everything starting from
CreateSprite(&enemies.ship,47,47,"enemy.bmp"); //load from enemy.bmp 

will be executed with i = 5.
Hmmm... I''m having a little trouble folowing your code but it looks like your passing a single dimentional array to a function and then accessing it as if it were a 2 dimentional array (of int''s):

Here you use fprintf on an int, a member of the 2 dimentional array anims (a problem in and of itself - which could be causing the crazy results)...

    void MakeAnimation(SPRITEPTR sprite, int animindex, int numframes, int anim[]) {	fprintf(fp2,"%d\n",sprite->anims[0][0]);	fprintf(fp2,"%d\n",sprite->anims[0][1]);	sprite->anims[animindex]=anim;	sprite->curanimframes=numframes;	sprite->indexinanim=0;}  


But you pass it a single dimentional array...
  	int fly[]= {0,1}; //frames for fly straight	ship.y=500; //ship''s pos	ship.x=200; //ship''s pos	ship.updatecounter=2; //how often does the ship update?	MakeAnimation(&ship,0,1,fly); //create the straight anim  


You should make use of stronger typing to prevent such errors.
// Ryan
No this is a problem of bad indenting, sorry. I originally had this correct, but I only had one enemy at the time. I added in the for statement too hasty and didn''t correctly fix my indents, those are in the for block if you look though.

Sponge Factory
--Muzlack
--Muzlack

This topic is closed to new replies.

Advertisement