Tutorial 1 Part 3

So far our sprite has been moving automatically by the program. We will now add code that will make the sprite move under our control.

Add the following lines of code to the project you created in part 2.

  • Add another global variable called "FRAME_NUM" then set it to equal 1. This variable will keep track of the current frame of the animation sequence.

  • Next turn off the animation so that the man isn't continually walking (Sprite Animate 1 Off).

  • Set Sprite_X to equal 288 so that it starts in the middle of the screen.

  • We now need to change the code inside the repeating loop so that the sprite only moves when a cursor key is pressed. Check for the left cursor key (If CursorLeft()), now decrement FRAME_NUM (FRAME_NUM = FRAME_NUM - 1) and change the animation sequence frame (SetSequenceFrame(1,FRAME_NUM)).

  • Check to see if the sequence has reached the end. and if it has, reset it.

  • Do the same again except check for the right cursor key and increment FRAME_NUM.

  • As we will be pressing the cursor keys we will need to change the Until statement to check for a specific keypress such as "q".

    Your code should now look like this :

    NOTE: If you do not wish to type the code below you may download or open this text file and simply copy and paste the code into your project.


    //TUTORIAL 1 PART 3

    //DEFINE VARIABLES

    Global KEYPRESS$, SPRITE_X, FRAME_NUM

    //DEFINE PROCEDURES
    DefProc INIT_DISPLAY()
    DefProc CREATE_SPRITES()

    //---- START MAIN CODE ----

    //CALL THE PROCEDURE TO CREATE THE SCREEN

    INIT_DISPLAY()

    //CALL THE PROCEDURE TO CREATE THE SPRITES
    CREATE_SPRITES()

    FRAME_NUM = 1 //START WITH FRAME ONE
    Sprite Animate 1 Off //TURN OFF ANIMATION SO THAT WE CAN MANUALLY ANIMATE THE SPRITE
    SPRITE_X = 288 //START THE SPRITE IN THE MIDDLE OF THE SCREEN

    //START LOOP - THIS LOOP WILL REPEAT UNTIL A KEY IS PRESSED
    Repeat

      If CursorLeft() Then //HAS THE LEFT CURSOR BEEN PRESSED?

        SetSequenceFrame(1,FRAME_NUM) //GO TO THE PREVIOUS FRAME IN THE ANIMATION
        FRAME_NUM = FRAME_NUM - 1 //DECREMENT THE FRAME_NUM VARIABLE

        If FRAME_NUM < 1 Then //HAS THE SEQUENCE REACHED THE START?
          FRAME_NUM = 30 //IF IT HAS THEN GO BACK TO THE END
        EndIf

        Sprite Move 1,SPRITE_X,208 //MOVE SPRITE #1 TO THESE CO-ORDS
        SPRITE_X = SPRITE_X - 1 //DECREMENT SPRITE_X

        If SPRITE_X = -64 Then //IS THE SPRITE AT THE END OF THE SCREEN?
          SPRITE_X = 640 //IF SO, THEN MOVE SPRITE TO JUST OFF THE RIGHT SIDE
        EndIf

      EndIf

      If CursorRight() Then //HAS THE RIGHT CURSOR BEEN PRESSED?

        SetSequenceFrame(1,FRAME_NUM) //GO TO THE NEXT FRAME IN THE ANIMATION
        FRAME_NUM = FRAME_NUM + 1 //INCREMENT THE FRAME_NUM VARIABLE

        If FRAME_NUM > 30 Then //HAS THE SEQUENCE REACHED THE END?
          FRAME_NUM = 0 //IF IT HAS THEN GO BACK TO THE START
        EndIf

        Sprite Move 1,SPRITE_X,208 //MOVE SPRITE #1 TO THESE CO-ORDS
        SPRITE_X = SPRITE_X + 1 //INCREMENT SPRITE_X

        If SPRITE_X = 640 Then //IS THE SPRITE AT THE END OF THE SCREEN?
          SPRITE_X = -64 //IF SO, THEN MOVE SPRITE TO JUST OFF THE LEFT SIDE
        EndIf

      EndIf

      KEYPRESS$ = Inkey$()
      Update Display

    Until KEYPRESS$ = "q"

    //THE INIT_DISPLAY PROCEDURE CREATES A SCREEN AND A PLANE
    Procedure INIT_DISPLAY()

      Create Map 640,480,1,1 In Bank 50 //CREATE A MAP IN BANK 50
      Set Tile 0,0,1 Using Bank 50 //CREATE A TILE USING A BACKGROUND IMAGE IN BANK 1
      Create Plane 1 Using Bank 50 //CREATE PLANE 1 USING THE MAP IN BANK 50
      //SET WHITE TO BE TRANSPARENT INSTEAD OF DEFAULT BLACK AS OUR SPRITE CONTAINS BLACK PIXELS
      Set Bitmap 2 Transparency To 255,255,255

    EndProc

    //THIS PROCEDURE WILL CREATE THE SPRITE
    Procedure CREATE_SPRITES()

      Create Sprite 1,288,208 Using Bank 3 //CREATE SPRITE #1 AT 288,208 USING THE SEQUENCE IN BANK 3
      Sprite On 1 //ENABLE THE SPRITE
      Sprites On //NOW TURN ON THE SPRITE SYSTEM

    EndProc

    EndProg

  • The man should now start off motionless in the middle of screen until you press the left or right cursor keys. He should then walk in that direction and wrap around when he reaches the edge of the screen.

    Previous Page  Next Page