News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Coding Question

Started by Mick Berg, November 09, 2005, 11:50:17 AM

Previous topic - Next topic

Mick Berg

I'm wondering why this doesn't work for me;

Ed=GETFREESPRITE()
CREATESPRITE Ed

To make it work (ie for the sprite to be created) I have to add the line;
CONSTANT Ed=1

Strange as all the help examples are written as something like

MySprite=GETFREESPRITE()
CREATESPRITE MySprite

Can anyone explain what I'm not getting? Thanks.

empty

Works for me

img = GetFreeImage()
CreateImage img,32,32
RenderToImage img
Cls 255

RenderToScreen
Ed = GetFreeSprite()
CreateSprite Ed
SpriteImage ed, img
PositionSprite ed, 100,100
DrawAllSprites
Sync
WaitKey


Make sure that after calling GetFreeSprite() you don't accidently create a sprite elsewhere with that same sprite number.

Mick Berg

#2
I realised that all was OK outside of the PSub, but not within the PSub. Here is some code to demonstrate. You have to comment out the "CONSTANT MyBox=1" line to see the effect. (I couldn't work out a way to automate removal of a line of code, or make it ineffective.) Sorry there's so much code, couldn't see a way to reduce it further. With the CONSTANT line in, you see the image in the PSub, and with it out, you see it outside the Psub. I would expect to see it twice.

PlayBASIC Code: [Select]
SETFPS 100
INK RGB (0,0,0)
LOADFONT "COMIC SANS MS",2,36,1
SETFONT 2
BOXC 100,100,150,150,1,RGB(255,0,0)
GETIMAGE 1,100,100,150,150
CLS 0
BOXC 100,100,150,150,1,RGB(0,0,255)
GETIMAGE 3,100,100,150,150
CLS 0
MyBox = GETFREESPRITE()
CREATESPRITE MyBox
GLOBAL i=1;variable that changes images
;****************HERE's THE PROBLEM*********************
CONSTANT MyBox=1 ;has to be here to see image in the PSub
; has to NOT be there to see image outside of PSub
;*******************************************************
TYPE MyBoxType
X,Y
ENDTYPE
DIM MyBoxPos AS MyBoxType
MyBoxPos.X=300: MyBoxPos.Y=300
DO
INC counter ;timer to toggle animation
m = MOD(counter,20)
IF m=0 THEN i=(i*-1)
CLS RGB(150,200,255)
SPRITEIMAGE MyBox,1 ;Image NOT from the PSub
POSITIONSPRITE MyBox, 100,100
MyBoxJump()
TEXT 160,125,"IMAGE FROM OUTSIDE PSUB"
TEXT 375,300,"IMAGE FROM PSUB"
DRAWALLSPRITES
SYNC
LOOP
PSUB MyBoxJump()
SPRITEIMAGE MyBox,2+i ;Animated Image from PSub
POSITIONSPRITE MyBox,MyBoxPos.X,MyBoxPos.Y
ENDPSUB





empty

#3
You need to make MyBox Global to use it in the Psub:
PlayBASIC Code: [Select]
SetFPS 100
Ink RGB (0,0,0)
LoadFont "COMIC SANS MS",2,36,1
SetFont 2
BoxC 100,100,150,150,1,RGB(255,0,0)
GetImage 1,100,100,150,150
Cls 0
BoxC 100,100,150,150,1,RGB(0,0,255)
GetImage 3,100,100,150,150
Cls 0
Global MyBox = GetFreeSprite()
CreateSprite MyBox
Global i=1;variable that changes images
Type MyBoxType
X,Y
EndType
Dim MyBoxPos As MyBoxType
MyBoxPos.X=300: MyBoxPos.Y=300
Do
Inc counter;timer to toggle animation
m = Mod(counter,20)
If m=0 Then i=(i*-1)
Cls RGB(150,200,255)
SpriteImage MyBox,1;Image NOT from the PSub
PositionSprite MyBox, 100,100
MyBoxJump()
Text 160,125,"IMAGE FROM OUTSIDE PSUB"
Text 375,300,"IMAGE FROM PSUB"
DrawAllSprites
Sync
Loop
Psub MyBoxJump()
SpriteImage MyBox,2+i;Animated Image from PSub
PositionSprite MyBox,MyBoxPos.X,MyBoxPos.Y
EndPsub




QuoteI would expect to see it twice.
It's only one sprite so it uses the last position/layout (which is defined in the Psub).

Mick Berg

QuoteYou need to make MyBox Global to use it in the Psub:
It's only one sprite so it uses the last position/layout (which is defined in the Psub).

There you go, thanks for the explanation.

empty