Updating weapon to sprite
trieveonc

I'm Having an issue adding a weapon to my player

my aim is to get the image to shoot out in the direction the player is going.

whats happening is the image appears once then stops for a while then a few spacebars later itll appear again mimicing the players movement. I cant seem to find where im going wrong.

#SelectExpand
1void FireBullet(Bullet bullet[], int size, SpaceShip &ship) 2{ 3 for( int i = 0; i < size; i++) 4 { 5 if(!bullet[i].live) 6 { 7 bullet[i].x = ship.x ; 8 bullet[i].y = ship.y; 9 bullet[i].live = true; 10 11 al_play_sample(shot, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, 0); 12 13 break; 14 } 15 } 16}

#SelectExpand
1void UpdateBullet(Bullet bullet[], int size,SpaceShip &ship) 2{ 3 for(int i = 0; i <= size; i++) 4 { 5 if(bullet[i].live) 6 { 7 if(keys[UP]) 8 9 bullet[i].y+=bullet[i].speed; 10 if(bullet[i].x > 200) 11 bullet[i].live = false; 12 13 else if(keys[DOWN]) 14 15 bullet[i].y-=bullet[i].speed; 16 if(bullet[i].x > 200) 17 bullet[i].live = false; 18 19 else if(keys[LEFT]) 20 21 bullet[i].x-=bullet[i].speed; 22 if(bullet[i].x > 200) 23 bullet[i].live = false; 24 25 else if(keys[RIGHT]) 26 27 bullet[i].x+=bullet[i].speed; 28 if(bullet[i].x > 200) 29 bullet[i].live = false; 30 31 32 33 } 34 } 35}

then I update it

LennyLen

You're only updating the bullet's position if a direction key is being pressed. And since I assume the ship is also moving when those keys are pressed the bullet will appear to be moving in time with the ship.

#00JMP00

Further to LennyLen, there is secure logical problem:

bullet[i].y-=bullet[i].speed;
16 if(bullet[i].x > 200)
17 bullet[i].live = false;
18
19 else if(keys[LEFT])
20
21 bullet[i].x-=bullet[i].speed;
22 if(bullet[i].x > 200)
23 bullet[i].live = false;

As I do not know the directions of your coords, line 16 or
line 22 hast the wrong comparison.

Same goes to y.

I saw this only now, it seems, as if in the frist routine y and x is messed up:

Probably should be:

if (bullet[i].y < 200)

The original bug can be, you spawn bullets, which vanish, the bullet seen later is the bullet after...maybe.

trieveonc

ok i condensed the code to get to the root of the problem, the problem is if i press an enum direction and spacebar(fire option) at same time it updates the position of the next bullet.insead of just resseting the animation to the ships ship.x and ship.y;
basically if im pressing spacebar and a movement key rapidly its not reseting the pos fast enough.

#SelectExpand
1void FireBullet(Bullet bullet[], int size, SpaceShip &ship) 2{ 3 for( int i = 0; i < size; i++) 4 { 5 if(!bullet[i].live) 6 { 7 bullet[i].x = ship.x; 8 bullet[i].y = ship.y; 9 bullet[i].live = true; 10 11 al_play_sample(shot, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, 0); 12 13 break; 14 } 15 } 16} 17void UpdateBullet(Bullet bullet[], int size,SpaceShip &ship) 18{ 19 20 for(int i = 0; i < size; i++) 21 { 22 if(bullet[i].live) 23 { 24 bullet[i].x += bullet[i].speed; 25 bullet[i].y+=bullet[i].speed 26 if(bullet[i].x > 500) 27 if (bullet[i].y<500) 28 bullet[i].live = false; 29 } 30 } 31}

is this a better approach i tried doing it got errors regarding the showplayer function

*****also what would my sprite sheet looks like 3 directions and a sword in row?**

#SelectExpand
1enum FACING = { 2 LEFT = 0, 3 RIGHT = 1, 4 UP = 2, 5 DOWN = 3 6}; 7int facing = LEFT; 8double player_frame_time = 0.0; 9double player_animation_time_total = 3.0;// or however long you want your animation to last 10const int NUMFRAMES = 3; 11ALLEGRO_BITMAP* player_frames[4][NUMFRAMES]; 12 13// load frames 14void LoadFrames(const char* spritesheet) {/*...*/} 15 16 17void AdvanceAnimationTime(double dt) { 18 player_frame_time += dt; 19 player_frame_time = fmod(player_frame_time , player_animation_time_total); 20} 21 22// show frame 23void ShowPlayer() { 24 al_draw_bitmap(player_frames[facing][(int)((double)NUMFRAMES*(player_frame_time/player_animation_time_total))] , playerx - camx , playery - camy , 0); 25}

Thread #614349. Printed from Allegro.cc