dkl
Full Member
Posts: 234
|
Post by dkl on Sept 1, 2022 22:53:56 GMT -5
I am trying to teach myself how to use sprites. I have followed a number of tutorials which have helped and Mark Dunham's Youtube Tutorial helped a lot too. Thanks Mark:)
I understand how to get multiple versions of the same sprite works in a graphics windows. Although not sure I'd know what to do if I only wanted 1 to move and the rest stay fixed!
What I want to do is load lots of sprites (which will remain static unless selected) and they are named as follows..... P1.bmp, P2.bmp, P3.bmp, etc
I cannot get the correct syntax to add multiple sprites with the "addsprite" or "spritexy" command
This is what I have done for 1 sprite. It works fine.
n1X = 100 n1Y = 100
maxPieces = 3 dim Puz$(maxPieces)
ti=1 while ti <=3 grid$ = "N" + str$(ti) loadbmp grid$,grid$+".bmp" Puz$(ti) = grid$ ti = ti + 1 wend
WindowWidth = 500 WindowHeight = 500 UpperLeftX=int((DisplayWidth-WindowWidth)/2) UpperLeftY=int((DisplayHeight-WindowHeight)/2)
open " " for graphics_nf_nsb as #gr gosub [load.Sprite] wait
[load.Sprite] #gr, "addsprite N1 N1" #gr, "spritexy N1 ";n1X;" ";n1Y #gr, "drawsprites" return
I have studied Rod's code for his Scrabble programme as I am trying to do something along the lines of a grid and moving sprites around on the grid, but I get bogged down with all the arrays and what's going on within them. But at the moment I just want to get them placed on the screen.
I presume I need a loop for the "addsprite" command , but something like this doesn't work
#gr, "addsprite ";Puz$(i);" "; Puz$(i)
I have tried inserting 'chr$(34)' in various places, but haven't succeeded in getting anything to work.
#gr, "addsprite ";Puz$(i);" "; Puz$(i);chr$(34)
Surely, I don't need multiple sprite commands?
As always any help would be appreciated. Thank you.
|
|
|
Post by Rod on Sept 2, 2022 1:17:28 GMT -5
On my phone so no code. At the addsprite line just list the images you have already loaded that will create the multi image sprite.
So addsprite sp p1 p2 p3
Then if you want a particular image showing in that sprites position use spriteimsge command and name the image.
So spriteimage sp p3
The list is a literal list, no looping.
loadbmp "frog1", "frog1.bmp" loadbmp "frog2", "frog2.bmp" loadbmp "frog3", "frog3.bmp" #w.g "addsprite frog frog1 frog2 frog3" 'now animate the sequence #w.g "cyclesprite frog 1" 'or show a specific image #w.g "spriteimage frog frog3"
|
|
|
Post by Rod on Sept 2, 2022 8:28:53 GMT -5
I am wondering if I answered the question correctly. You can have a single sprite with multiple images contained within that sprite or you can have multiple sprites with either the same image or varying images.
If you are trying to create multiple sprites in a loop it can be done. There is code in my scrabble program.
Best to actually print the command line that you are trying to use. What does it look like, it should look exactly like a literal command line when printed.
|
|
dkl
Full Member
Posts: 234
|
Post by dkl on Sept 2, 2022 20:37:17 GMT -5
Hi Rod, thank you for taking the time to get back to me, especially as you seem to be busy travelling a lot.
It is in fact the 2nd idea I am trying to do. That is.....making a grid of multiple sprites that I will eventually be able to move around. I understand what you mean by 'printing out' the the command line and have been trying to do that, but eventually get very muddled when using the chr$(34) statement!
Below are 2 methods I have tried taken from your Scrabble programme - both work, but I cannot put the sprites adjacent to each other in fixed positions. They just display on top of each other and then disappear.
'Ver. 1 open f$ for graphics as #b ti = 1:r = 100:c = 100 while ti <4 g$ = "N"+str$(ti) loadbmp g$,g$ + ".bmp" print g$ 'for reference it's correct name #b "drawbmp ";g$;" 0 50" #b "getbmp ";g$;" 0 0 50 100" #b "addsprite ";g$;" ";g$ #b "spritexy ";g$;" ";c;" ";r ti = ti +1:c = c + 100':r = r + 100 input q$'to watch sprites being displayed wend #b "drawsprites" 'now they have disappeared!
wait
'Ver. 2 dim g$(4) open f$ for graphics as #b ti = 1:r = 100:c = 100 while ti <4
g$(ti) = "N" + str$(ti) loadbmp g$(ti),g$(ti)+".bmp" print g$(ti) 'for reference #b "drawbmp ";g$(ti);" 0 50" #b "getbmp ";g$(ti);" 0 0 50 100" #b "addsprite ";g$(ti);" ";g$(ti) #b "spritexy ";g$(ti);" ";c;r;" ";g$(ti) ti = ti +1:c = c + 100:r = r + 100 input q$ 'to check/watch sprites are being displayed wend #b "drawsprites" 'now they have disappeared!
wait
I am assuming that 'c' & 'r' adjust the positioning of the sprites so don't understand why they appear in the same place all the time and then vanish after the 'drawsprite' command!
I also tried really hard to use the routine to display the 'bricks' from your 'Breakout' programme, but didn't succeed there either!
|
|
|
Post by tsh73 on Sept 3, 2022 2:15:20 GMT -5
Fixed your #1 code problems: 1) sprite needs BLACK mask part above actual picture (I think I've seed same picture doubled) WHITE upper part does not draw a thing! 2) to draw that BLACK part you need to put pen DOWN first (3) also your third sprite was drawing out of screen, increased window width to 400
so
'Ver. 1 WindowWidth=400 open f$ for graphics as #b #b "trapclose [quit]"
'put the pen down, of you will not get black mask #b "down; fill white" ti = 1:r = 100:c = 100 while ti <4 SCAN g$ = "N"+str$(ti) loadbmp g$,g$ + ".bmp" print g$ 'for reference it's correct name
'make a black mask above picture #b "backcolor black" #b "place 0 0" #b "boxfilled 50 50"
#b "drawbmp ";g$;" 0 50" #b "getbmp ";g$;" 0 0 50 100" #b "addsprite ";g$;" ";g$ #b "spritexy ";g$;" ";c;" ";r ti = ti +1:c = c + 100':r = r + 100 input q$'to watch sprites being displayed wend
#b "drawsprites" 'now they have disappeared! '- Now thgey are fixed 'but resize window makes it all black 'so I put redraw on timer timer 100, [redraw] wait
[redraw] #b "drawsprites" wait
[quit] timer 0 close #b end
|
|
|
Post by tsh73 on Sept 3, 2022 2:22:40 GMT -5
Your ('Ver. 2) has same problems + an error in SPRITEXY Line
#b "spritexy ";g$(ti);" ";c;r;" ";g$(ti) produces
spritexy N1 100100 N1 You need
#b "spritexy ";g$(ti);" ";c;" ";r;" ";g$(ti) to get
spritexy N1 100 100
|
|
dkl
Full Member
Posts: 234
|
Post by dkl on Sept 3, 2022 19:53:21 GMT -5
Thank you tsh73. that was a lot of help. But I'm still a bit confused. Are you saying that as long I have the statements #b "backcolor black" #b "place 0 0" #b "boxfilled 50 50"
before adding the sprite, that I don't need to add a 'Mask' in 'photoshop' to the bitmap? If I use a bmp with a mask I either end up with 3 black squares or nothing on the screen depending on the commands I highlight out or keep! I couldn't get Ver2. to work correctly, even with the new 'spritexy' statement. I just get 3 half size black squares. dim g$(4) WindowWidth=400 open f$ for graphics as #b #b "trapclose [quit]" 'put the pen down, of you will not get black mask #b "down; fill white" ti = 1:r = 100:c = 100 while ti <4
g$(ti) = "N" + str$(ti) loadbmp g$(ti),g$(ti)+".bmp" print g$(ti) 'for reference
'make a black mask above picture #b "backcolor black" #b "place 0 0" #b "boxfilled 40 40"
#b "drawbmp ";g$(ti);" 0 50" #b "getbmp ";g$(ti);" 0 0 50 50" #b "addsprite ";g$(ti);" ";g$(ti) #b "spritexy ";g$(ti);" ";c;" ";r;" ";g$(ti) ti = ti +1:c = c + 100':r = r + 100 input q$ 'to check/watch sprites are being displayed wend #b "drawsprites" 'now they have disappeared! '- Now they are fixed 'but resize window makes it all black 'so I put redraw on timer timer 100, [redraw] wait
[redraw] #b "drawsprites" wait
[quit] timer 0 close #b end
I am assuming it is the 2nd version (using array) I will need to make it easier to address sprite positions later when I want to 'gettile' or move a 'sprite'? Thank you for your time
|
|
|
Post by Rod on Sept 4, 2022 2:27:49 GMT -5
It is pretty straightforward. In the lower part of the image black surrounds the coloured image and is never displayed. In the upper image white surrounds the SHAPE of the lower image drawn in black. This mask is what the blitter uses to draw the sprite. It looks at the mask if it is white it draws nothing if it is black it draws the pixel in the lower image pixel color.
If you have a black box as the mask it will draw the entire lower image. Sometimes this is useful sometimes not. It depends on whether you want the sprite to float over other sprites or just blank out whatever is there.
When you come to manage the placement and movement and status of a hundred sprites you will soon find that arrays are the cleanest option.
|
|
dkl
Full Member
Posts: 234
|
Post by dkl on Sept 4, 2022 23:08:57 GMT -5
Thank you, I think I'm on the right track now?! I can load/pick/move/drop sprites now. The "addsprite" code I kept getting an error for was because g$(ti) didn't hold the correct info! It does now! However, I don't understand why it is necessary to put g$(ti) at the end of the statement - #w.g "spritexy ";g$(ti);" ";c;" ";r;" ";g$(ti) when it isn't documented like that - #w.g, "spritexy SpriteName ";x;" ";y. It doesn't say #w.g, "spritexy SpriteName ";x;" ";y;" ";SpriteName - Great I did try to take tsh73's code to add a black mask before loading 20 bmp/sprites, but only ended up with 1/2 size sprites. It would be so much better if I could get tsh73's code to work as it would save having to add a mask in Photoshop for 200 bmp's! Ah!..... I see what I did wrong I left out the draw and getbmp statements!! Its working now!
|
|
|
Post by tsh73 on Sept 5, 2022 10:11:48 GMT -5
>>However, I don't understand why it is necessary to put g$(ti) at the end of the statement - #w.g "spritexy ";g$(ti);" ";c;" ";r;" ";g$(ti) It is not necessary My guess that LB just ignores extra data after space
|
|