Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:09 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM2 Sprite movement with KEYDOWN(n)

Author Message
shaputer
Newbie

Joined: 25/07/2020
Location: United States
Posts: 38
Posted: 04:17pm 29 Aug 2020
Copy link to clipboard 
Print this post

Hi Everyone,

For the life of me I can't figure out why my Sprite will only move once.  The sprite is a .png image.  A 64 X 64 small car.  Here is the code I am using.  Does anyone see any reason why my sprite only moves once when I press the arrow keys yet the value of the key pressed will print repeatedly.  I would like my sprite to move repeatedly.

Thanks !


CLS

LOAD BMP "SPLASH1.BMP"  'LOAD START SCREEN
SPRITE LOADPNG 1, "CarR.png"  'CAR FACING RIGHT SPRITE
SPRITE SHOW 1, 1, 16, 1  'SHOW THE CAR FACING RIGHT SPRITE


'random 'Sub Routine for Random Moves

DIM INTEGER I

X=0 'X COORDINATE
Y=0 'Y COORDINATE

DO
 IF KEYDOWN(0) THEN 'AT LEAST 1 KEY PRESSED
   IF TIMER > 200 THEN  'OPTIONAL-CHANGE TO ALTER RESPONSE TO KEY PRESSES
    TIMER = 0
    FOR I = 1 TO KEYDOWN(0) ' CHECK FOR ARROW KEYS AND ESC KEY
     
      PRINT KEYDOWN(I)
      PRINT
     
      IF KEYDOWN(I) = 27 THEN EXIT DO  'ESCAPE KEY TO EXIT
      IF KEYDOWN(I) = 130 THEN LEFT
      IF KEYDOWN(I) = 131 THEN RIGHT
      IF KEYDOWN(I) = 128 THEN UP
      IF KEYDOWN(I) = 129 THEN DOWN
    NEXT I
  END IF
 END IF  

LOOP

CLS
PRINT "THANKS FOR PLAYING !"
END

SUB LEFT
 SPRITE NEXT 1, X + (-56), Y
 SPRITE MOVE 1
 PAUSE 100
END SUB

SUB RIGHT
 SPRITE NEXT 1, X + 56, Y
 SPRITE MOVE 1
 PAUSE 100
END SUB

SUB UP
 SPRITE NEXT 1, X, Y + (-36)
 SPRITE MOVE 1
 PAUSE 100
END SUB

SUB DOWN
 SPRITE NEXT 1, X, Y + 36
 SPRITE MOVE 1
 PAUSE 100
END SUB



SUB random
DO

 SPRITE NEXT 1, RND * 750, RND * 550
 SPRITE MOVE 1  'MOVE CAR RANDOMLY ON THE SCREEN
 PAUSE 500
         
LOOP

END SUB

If what you are doing is bringing out the worst in you than stop doing it.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 04:32pm 29 Aug 2020
Copy link to clipboard 
Print this post

Because you are not changing X or Y. Moves are absolute not relative

BTW there is no reason to use NEXT and MOVE except in particular circumstances where many sprites are moving - just use SHOW
Edited 2020-08-30 02:37 by matherp
 
shaputer
Newbie

Joined: 25/07/2020
Location: United States
Posts: 38
Posted: 11:24pm 29 Aug 2020
Copy link to clipboard 
Print this post

Thanks, Matherp !

That helped.  Got it moving now, but why is the MM.HPOS readonly variable not working like the MM.HRES ?  Is that variable no longer supported ?
If what you are doing is bringing out the worst in you than stop doing it.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 11:36pm 29 Aug 2020
Copy link to clipboard 
Print this post

  shaputer said  Thanks, Matherp !

That helped.  Got it moving now, but why is the MM.HPOS readonly variable not working like the MM.HRES ?  Is that variable no longer supported ?


MM.INFO(HPOS)
MM.INFO(VPOS)
VK7JH
MMedit
 
shaputer
Newbie

Joined: 25/07/2020
Location: United States
Posts: 38
Posted: 04:13pm 30 Aug 2020
Copy link to clipboard 
Print this post

Thanks a lot !  Success !!!  Now to figure out Sprite Interupt
You hold down the right arrow key to make the car go.

I thought I would share so maybe it will help someone else.    



LOAD BMP "SPLASH1.BMP"  'LOAD START SCREEN
SPRITE LOADPNG 1, "CarR.png"  'CAR FACING RIGHT SPRITE
SPRITE SHOW 1, 1, 16, 1  'SHOW THE CAR FACING RIGHT SPRITE

DIM INTEGER I

START:

DO
 IF KEYDOWN(0) THEN 'AT LEAST 1 KEY PRESSED
   IF TIMER > 200 THEN  'OPTIONAL-CHANGE TO ALTER RESPONSE TO KEY PRESSES
    TIMER = 0
    FOR I = 1 TO KEYDOWN(0) ' CHECK FOR ARROW KEYS AND ESC KEY
     
      'PRINT KEYDOWN(I) 'PRINT KEY THAT IS PRESSED
      'PRINT
       
      IF KEYDOWN(I) = 27 THEN EXIT DO  'ESCAPE KEY TO EXIT
      IF KEYDOWN(I) = 130 THEN LEFT
      IF KEYDOWN(I) = 131 THEN RIGHT
      IF KEYDOWN(I) = 128 THEN UP
      IF KEYDOWN(I) = 129 THEN DOWN
    NEXT I

  END IF
 END IF  

LOOP

CLS
PRINT "THANKS FOR PLAYING !"
END


SUB RIGHT

 FOR XR = XR TO MM.HRES STEP 26    
  IF KEYDOWN(1) = 131 THEN  
    SPRITE SHOW 1, XR, 1, 1

    'PRINT XR
    'PRINT

    PAUSE 200
  ELSE
    GOTO START

  END IF
 NEXT XR
END SUB


If what you are doing is bringing out the worst in you than stop doing it.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025