![]() |
Forum Index : Microcontroller and PC projects : CMM2 Sprite movement with KEYDOWN(n)
Author | Message | ||||
shaputer Newbie ![]() Joined: 25/07/2020 Location: United StatesPosts: 38 |
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) 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 KingdomPosts: 10310 |
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 StatesPosts: 38 |
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: AustraliaPosts: 6283 |
MM.INFO(HPOS) MM.INFO(VPOS) VK7JH MMedit |
||||
shaputer Newbie ![]() Joined: 25/07/2020 Location: United StatesPosts: 38 |
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 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 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. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |