Sprite Not Moving

This is a discussion on Sprite Not Moving within the Game Programming forums, part of the General Programming Boards category; Hello! I have made my script and it is using allegro, The only thing that isn't working is moving the ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    2

    Sprite Not Moving

    Hello!
    I have made my script and it is using allegro, The only thing that isn't working is moving the sprite. Why doesn't my sprite move?

    Code:
    #include <allegro.h>
    
    
    
    void myAllegroSetup()
    {
        allegro_init();
        install_keyboard();
        set_color_depth(32);
        set_gfx_mode( GFX_AUTODETECT_WINDOWED, 600, 400, 0, 0);
        
    
    
    
    
    }
    
    
    void displayIntro()
    {
              BITMAP *introImage;    
              introImage = load_bitmap("open.bmp",NULL);
              
              stretch_blit(introImage, screen, 0, 0, introImage->w, introImage->h, 0, 0, 600, 400); // Draws the stretched image onto the screen
              textout_ex(screen, font, "Press any key to continue", 380, 380, makecol(255, 255, 255), -1);
              readkey();
    
              stretch_blit(introImage, screen, 0, 0, introImage->w, introImage->h, 0, 0, 600, 400); // Draws the stretched image onto the screen
              textout_ex(screen, font, "How to play", 400, 220, makecol(255, 255, 255), -1);      
              textout_ex(screen, font, "___________", 400, 223, makecol(255, 255, 255), -1);         
              
              textout_ex(screen, font, "         z  =  Talk/Action", 300, 247, makecol(255, 255, 255), -1);         
              textout_ex(screen, font, "         x  =  Cancel", 300, 257, makecol(255, 255, 255), -1);         
              textout_ex(screen, font, "Arrow Keys  =  Move/Navigate Menus'", 300, 267, makecol(255, 255, 255), -1);               
              textout_ex(screen, font, "Press any key to continue", 380, 380, makecol(255, 255, 255), -1);
              
              readkey();        
    }
    
    void movePlayer(){
        
        int playerX = 100;
        int playerY = 100;
        
        BITMAP *player;    
        player = load_bitmap("sprite/walk_none.bmp",NULL);
        
        if( key[KEY_LEFT]){
            playerX - 20;
            draw_sprite( screen, player, playerX, playerY);
        } else if( key[KEY_RIGHT]){
            playerX + 20;
            draw_sprite( screen, player, playerX, playerY);
        } else if( key[KEY_UP]){
            playerY + 20;
            draw_sprite( screen, player, playerX, playerY);
        } else if( key[KEY_DOWN]){
            playerY - 20;
            draw_sprite( screen, player, playerX, playerY);
        }
    }
    }
            
    
    
    int main(){
     
    
     
        myAllegroSetup();
        displayIntro();
    
        
        movePlayer();
    
        return 0;
    }   
    END_OF_MAIN();

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Check your operators in your movePlayer() function. It is a very obvious mistake.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    You need loops.

    You only call the function once, and if a key isn't pressed at that moment, nothing will happen.

    Edit: good catch by Desolation as well
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Also, I might suggest you another way to code this that I think is much cleaner:

    Code:
    if( key[KEY_LEFT]) playerX - 20;
    else if( key[KEY_RIGHT]) playerX + 20;
    else if( key[KEY_UP]) playerY + 20;
    else if( key[KEY_DOWN]) playerY - 20;
    
    draw_sprite( screen, player, playerX, playerY);

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by Salem View Post
    You need loops.

    You only call the function once, and if a key isn't pressed at that moment, nothing will happen.
    Even if keys are pressed when the function is executed, the sprite will not move. There is another error.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    2
    Sorry, I changed the script to what I wanted it to be (Hence why I was always drawing the sprite).

    Code:
    #include <allegro.h>
    #include <string>
    
    
    
    void myAllegroSetup()
    {
        allegro_init();
        install_keyboard();
        set_color_depth(32);
        set_gfx_mode( GFX_AUTODETECT_WINDOWED, 600, 400, 0, 0);
    }
    
    
    void displayIntro()
    {
              BITMAP *introImage;    
              introImage = load_bitmap("bg/open.bmp",NULL);
              
              stretch_blit(introImage, screen, 0, 0, introImage->w, introImage->h, 0, 0, 600, 400); // Draws the stretched image onto the screen
              textout_ex(screen, font, "Press any key to continue", 380, 380, makecol(255, 255, 255), -1);
              readkey();
    
              stretch_blit(introImage, screen, 0, 0, introImage->w, introImage->h, 0, 0, 600, 400); // Draws the stretched image onto the screen
              textout_ex(screen, font, "How to play", 400, 220, makecol(255, 255, 255), -1);      
              textout_ex(screen, font, "___________", 400, 223, makecol(255, 255, 255), -1);         
              
              textout_ex(screen, font, "         z  =  Talk/Action", 300, 247, makecol(255, 255, 255), -1);         
              textout_ex(screen, font, "         x  =  Cancel", 300, 257, makecol(255, 255, 255), -1);         
              textout_ex(screen, font, "Arrow Keys  =  Move/Navigate Menus'", 300, 267, makecol(255, 255, 255), -1);               
              textout_ex(screen, font, "Press any key to continue", 380, 380, makecol(255, 255, 255), -1);
              
              readkey();        
    }
    
    void movePlayer(){
        
        int playerX;
        int playerY;
        int abc;
            
        BITMAP *walk_left  = load_bitmap( "sprite/walk_left.bmp", NULL);
        BITMAP *walk_right = load_bitmap( "sprite/walk_right.bmp", NULL);
        BITMAP *walk_up    = load_bitmap( "sprite/walk_up.bmp", NULL);
        BITMAP *walk_down  = load_bitmap( "sprite/walk_down.bmp", NULL);
        BITMAP *walk_none  = load_bitmap( "sprite/walk_none.bmp", NULL);
              
          
        while(!key[KEY_ESC]) {
         
        if( key[KEY_LEFT]){
            playerX - 20;
            draw_sprite( screen, walk_left, playerX, playerY);
        } else if( key[KEY_RIGHT]){
            playerX + 20;
            draw_sprite( screen, walk_right, playerX, playerY);
        } else if( key[KEY_UP]){
            playerY + 20;
            draw_sprite( screen, walk_up, playerX, playerY);
        } else if( key[KEY_DOWN]){
            playerY - 20;
            draw_sprite( screen, walk_down, playerX, playerY);
        }
        else draw_sprite( screen, walk_none, playerX, playerY);
    }
    }
            
    
    
    int main(){
     
    
     
        myAllegroSetup();
        displayIntro();
     
        rectfill( screen, 0, 0, 600, 400, makecol( 0, 0, 0));    // Temporary background
    
        movePlayer();
    
        return 0;
    }   
    END_OF_MAIN();
    I know I need to say where playerX and playerY should go but I cant include this in the function as it is repeated, So where would I declare them?

    Sorry if this sounds stupid or w/e but I havent slept for 37 hours

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > Sorry if this sounds stupid or w/e but I havent slept for 37 hours
    Well there's your problem!
    Get some sleep, and enjoy the "OMG what was I thinking moment in the morning"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving sprite relative to another sprite
    By Akkernight in forum Game Programming
    Replies: 5
    Last Post: 04-13-2009, 05:00 PM
  2. Object not moving to angle
    By mike_g in forum C Programming
    Replies: 1
    Last Post: 06-22-2007, 09:30 AM
  3. DirectX 9 sprite issue
    By maxthecat in forum Game Programming
    Replies: 9
    Last Post: 05-18-2006, 04:04 AM
  4. Replies: 3
    Last Post: 05-12-2003, 05:49 PM
  5. problem in allegro with sprite leaving trail
    By Leeman_s in forum C++ Programming
    Replies: 18
    Last Post: 09-13-2002, 03:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21