Problem in % sprite continue when using a variable instead literal value

This is where most of us started. Classic Spectrum basic.

Moderator: Programming Moderators

oscarbraindead
Posts: 7
Joined: Wed May 13, 2020 9:43 am

Problem in % sprite continue when using a variable instead literal value

Postby oscarbraindead » Mon Jul 06, 2020 3:34 pm

Hi all,
I'm doing some tests with the nextbasic's sprite functions. It works really well but I've found a problem with the %sprite continue function when using a variable with the sprite number instead the literal number.
If I try this code:
  • 1 rem
    5 RUN AT 3: BORDER 1: PAPER 0: INK 7: CLS
    10 LOAD "nextnoon.spr" BANK 12: SPRITE BANK 12: SPRITE CLEAR : SPRITE PRINT 1: SPRITE BORDER 0
    1000 SPRITE STOP :done=0
    1010 SPRITE 50,32,128,0,1: SPRITE CONTINUE 50,32 TO 256 STEP 1 RUN ,,1, BIN 00000010
    2000 REPEAT : WHILE done=0
    2026 SPRITE MOVE INT
    2036 v=% SPRITE CONTINUE 50: PRINT AT 5,5;v : rem shows 5 when moving, it is correct
    2046 n=50: z=%SPRITE CONTINUE n: PRINT AT 6,5;z : rem shows 0 when moving, it is wrong
    3500 REPEAT UNTIL 0
    4000 STOP
The first print (that uses the value 50 with literal) shows 5 while moving (1 because the sprite has continuos movement + 4 because the x axis is being updated).
The second print (that uses the variable n with the value 50) shows 0 while moving... but it reffers to the same sprite that above print.

Am I doing something wrong? It would be cool that the "%SPRITE CONTINUE n" could variable values for use in loops, for example.

Thanks in advance,
Best regards.

User avatar
SevenFFF
Posts: 565
Joined: Mon Jun 05, 2017 5:30 pm
Location: USA

Re: Problem in % sprite continue when using a variable instead literal value

Postby SevenFFF » Mon Jul 06, 2020 5:57 pm

Try

Code: Select all

2046 %n=%50: z=%SPRITE CONTINUE n: PRINT AT 6,5;%z
instead. %n is an integer variable which is completely separate from the floating point variable n. There are 26 integer variables (%a..%z), all preinitialised to zero.

When used in an expression, the % prefix indicates that the entire expression is evaluated with the separate integer expression evaluator, with strict left to right precedence. Any variable names appearing inside the integer expression implicitly refer to integer variables rather than floating point variables.

You can include an integer sub-expression inside a floating point expression with INT { ... }, and you can include an floating point sub-expression inside an integer expression with SGN{ ... }.

See pages 70..76 in the manual for a full explanation of this.
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel NXTP ESP Update ESP Reset CSpect Plugins

oscarbraindead
Posts: 7
Joined: Wed May 13, 2020 9:43 am

Re: Problem in % sprite continue when using a variable instead literal value

Postby oscarbraindead » Tue Jul 07, 2020 5:44 am

Hi SevenFFF.
Thanks for the tips, I think that I already tried before but it does not work... I change as you said and it returns always 0.
It's weird :S
Thank you!

garrylancaster
Posts: 36
Joined: Mon Feb 19, 2018 2:44 pm

Re: Problem in % sprite continue when using a variable instead literal value

Postby garrylancaster » Wed Jul 08, 2020 1:08 pm

SevenFFF wrote:
Mon Jul 06, 2020 5:57 pm
Try

Code: Select all

2046 %n=%50: z=%SPRITE CONTINUE n: PRINT AT 6,5;%z
This fixes the error in the original code, but introduces a new one :D

Now you are assigning the result of the %SPRITE CONTINUE function to floating-point variable z, but PRINTing integer variable %z.

The correct code would be:

Code: Select all

2046 %n=%50: %z=%SPRITE CONTINUE n: PRINT AT 6,5;%z
or:

Code: Select all

2046 %n=%50: z=%SPRITE CONTINUE n: PRINT AT 6,5;z

User avatar
SevenFFF
Posts: 565
Joined: Mon Jun 05, 2017 5:30 pm
Location: USA

Re: Problem in % sprite continue when using a variable instead literal value

Postby SevenFFF » Wed Jul 08, 2020 1:13 pm

Dammit, how did I not see that!
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel NXTP ESP Update ESP Reset CSpect Plugins

oscarbraindead
Posts: 7
Joined: Wed May 13, 2020 9:43 am

Re: Problem in % sprite continue when using a variable instead literal value

Postby oscarbraindead » Wed Jul 08, 2020 2:17 pm

Garry and SevenFFF, thank you guys. It's working fine now!


Who is online

Users browsing this forum: No registered users and 3 guests