Page 1 of 2
n00b question: #Sprite Object not initialized
Posted: Sun Oct 28, 2007 5:07 am
by gas01ine
Hi folks. It's been quite a while since my last PB project and I seem to have troubles with the basics. I am messing around with my little program (see below) and just can't get past the "#Sprite Object not initialized" Error. Any ideas?
Code: Select all
Enumeration
#windowMain
#sprite_PlayerIdle01
EndEnumeration
; the usual init stuff here
If InitSprite() = 0 Or InitKeyboard() = 0
MessageRequester("Error", "Can't open DirectX 7 or later", 0)
End
EndIf
; opening a window...
If OpenWindow(#windowMain,20,30,640,480,"Test") = 0
MessageRequester("Error","Window",0)
EndIf
; setting up a windowed screen
If OpenWindowedScreen(WindowID(#windowMain),5,5,320,300,0,0,0)
LoadSprite(#sprite_PlayerIdle01, "imgs\playerSprite01.png", 0)
; ^ yes, the filename is valid.
Repeat
FlipBuffers()
; i know, the order is a bit awkward but that's not my problem (or is it?)
ClearScreen(RGB(0,0,0))
DisplaySprite(#sprite_PlayerIdle01, 100, 100)
;^ Here's the ERROR: "#Sprite Object not initialized." wtf?
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Up)
Quit = 1
EndIf
Until Quit = 1
EndIf
Thanks in advance!
Phil
Posted: Sun Oct 28, 2007 5:37 am
by nco2k
you just forgot:
but i have modified your code a little. maybe it will become usefull:
Code: Select all
UsePNGImageDecoder(); (NEW) use the png image decoder if you need *.png support
Enumeration
#windowMain
#sprite_PlayerIdle01
EndEnumeration
; the usual init stuff here
If InitSprite() = 0 Or InitKeyboard() = 0
MessageRequester("Error", "Can't open DirectX 7 or later", 0)
End
EndIf
; opening a window...
If OpenWindow(#windowMain,20,30,640,480,"Test") = 0
MessageRequester("Error","Window",0)
End; (NEW) it failed and we cant keep going, so lets just end the app
EndIf
; setting up a windowed screen
If OpenWindowedScreen(WindowID(#windowMain),5,5,320,300,0,0,0)
If LoadSprite(#sprite_PlayerIdle01, "imgs\playerSprite01.png", 0); (NEW) check if the loading was successful
Repeat
;(NEW) flush window events
Repeat
WinEvent = WindowEvent()
If WinEvent = #PB_Event_CloseWindow
Quit = 1
EndIf
Until WinEvent = 0
; check user input
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Up)
Quit = 1
EndIf
; do the calculations here
; ...
; display stuff
ClearScreen(RGB(0,0,0))
DisplaySprite(#sprite_PlayerIdle01, 100, 100)
FlipBuffers()
Until Quit = 1
EndIf
EndIf
c ya,
nco2k
Posted: Sun Oct 28, 2007 1:50 pm
by gas01ine
nco2k wrote:you just forgot:
just lol! figuring out that myself would have taken me another 8 hours of banging my head against the keyboard first (if InitKeyboard() <> 0)

.
and thanks heaps for the now neatly organized code! i just luv the PB-community!
Posted: Thu Aug 14, 2008 3:06 am
by wolfwood2x
I am having a similar issue. It has been a while since I have tried to use graphics in PB and I am having a few issues remembering how to get this working.
Code: Select all
nitSprite()
InitKeyboard()
OpenWindowedScreen(1, 2,2,800,600,0,0,0)
UseJPEGImageDecoder()
LoadSprite(1000, "image2.jpg", 0)
DisplaySprite(1000,0,0)
Repeat
FlipBuffers()
ClearScreen(0)
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
The program dies on Display sprite and states that #Sprite Object not initialized.
Help me out here what am I doing wrong?
Posted: Thu Aug 14, 2008 3:21 am
by Rook Zimbabwe
Just a guess:
nitSprite()
InitKeyboard()
should be
InitSprite()
all else looks OK except you
OpenWindowedScreen(1, 2,2,800,600,0,0,0)
there is no window to open the screen into and you request 0 color mode?
try: OpenScreen(Width, Height, Depth, Title$)
Code: Select all
InitSprite()
InitKeyboard()
OpenScreen(800,600,16,"GAME 0")
UseJPEGImageDecoder()
LoadSprite(1000, "image2.jpg", 0)
Repeat
ClearScreen(0)
DisplaySprite(1000,0,0)
FlipBuffers()
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
instead...
Posted: Thu Aug 14, 2008 3:26 am
by wolfwood2x
the nitSprite was a cut and paste error it is actually correct in the code..
If I change it to OpenScreen instead it still does the same thing. The screen or window opens, it tries to load the sprite and then the program crashes and I get the error #Sprite object not initialized.
Posted: Thu Aug 14, 2008 3:40 am
by Rook Zimbabwe
OK the only thing I can think of is that the image is NOT in the dirtectory with the code when you run it.
Try this:
Code: Select all
Enumeration
#Sprite
#Cursor
EndEnumeration
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
MessageRequester("Error", "DirectX 7 Fail!", 0)
End
EndIf
wide = 800
High = 600
If OpenScreen ( wide, High,32,"Standard") = 0
MessageRequester("Error", "No screen could be Initialized...", 0)
End
EndIf
LoadSprite(#Sprite, "door2.bmp",0)
LoadSprite(#Cursor, "zcursor5.bmp",#PB_Sprite_Memory)
Maus.Point
Repeat
ClearScreen(RGB(0,0,0))
ExamineMouse()
ExamineKeyboard()
Maus\x = MouseX() - 16
Maus\y = MouseY() - 16
If KeyboardPushed(#PB_Key_Left) Or KeyboardPushed(#PB_Key_A)
SPRITEX = SPRITEX - 3
EndIf
If KeyboardPushed(#PB_Key_Right) Or KeyboardPushed(#PB_Key_D)
SPRITEX = SPRITEX + 3
EndIf
;
If KeyboardPushed(#PB_Key_Up) Or KeyboardPushed(#PB_Key_W)
SPRITEY = SPRITEY + 2
EndIf
If KeyboardPushed(#PB_Key_Down) Or KeyboardPushed(#PB_Key_S)
SPRITEY = SPRITEY - 2
EndIf
; you are going to have to put in off screen trapping routines
DisplaySprite(#Sprite, SPRITEX, SPRITEY)
DisplayTransparentSprite(#Cursor, Maus\x, Maus\y)
FlipBuffers()
If KeyboardPushed(#PB_Key_Escape)
Quit = 1
EndIf
Delay(3)
Until Quit
Use the wasd or arrow buttons on your sprite and have a transparent BMP for the cursor... Start with BMP images first... at the size of a sprite there isn't really any size savings!
Transparent BMP color is RGB(0,0,0) = BLACK BLACK... this can be altered...
Posted: Thu Aug 14, 2008 3:47 am
by Rook Zimbabwe
Eeeeps! I made a dyslexic mistake again:
Code: Select all
If KeyboardPushed(#PB_Key_Up) Or KeyboardPushed(#PB_Key_W)
SPRITEY = SPRITEY - 2
EndIf
If KeyboardPushed(#PB_Key_Down) Or KeyboardPushed(#PB_Key_S)
SPRITEY = SPRITEY + 2
EndIf
That will move it up and down... r at an agle if you hold 2 keys!
Posted: Thu Aug 14, 2008 11:27 am
by Kaeru Gaman
wolfwood2x wrote:I am having a similar issue. It has been a while since I have tried to use graphics in PB and I am having a few issues remembering how to get this working.
Code: Select all
nitSprite()
InitKeyboard()
OpenWindowedScreen(1, 2,2,800,600,0,0,0)
UseJPEGImageDecoder()
LoadSprite(1000, "image2.jpg", 0)
DisplaySprite(1000,0,0)
Repeat
FlipBuffers()
ClearScreen(0)
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
The program dies on Display sprite and states that #Sprite Object not initialized.
Help me out here what am I doing wrong?
1. if you are on Windows, open a Window first, and then put the WindowedScreen onto it, using WindowID(#MyWindow) as first parameter.
Code: Select all
OpenWindow(0,2,2,800,600,"Windowed Screen")
OpenWindowedScreen(WindowID(0),0,0,800,600,0,0,0)
2. make sure you are acessing the right directory and the spritefile is in it.
you can easily check this with
Code: Select all
If Not LoadSprite(1000, "image2.jpg", 0)
MessageRequester("Error","No Sprite, man!") : End
EndIf
3. you need to display the sprite in each frame, that means between clearing the screen and flipping the buffers.
Code: Select all
Repeat
ExamineKeyboard()
ClearScreen(0)
DisplaySprite(1000,0,0)
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
4. Next time please provide more exactly problem information than "I am having a similar issue."

Posted: Thu Aug 14, 2008 2:29 pm
by wolfwood2x
4. Next time please provide more exactly problem information than "I am having a similar issue." Wink
Sorry I thought
The program dies on Display sprite and states that #Sprite Object not initialized.
Help me out here what am I doing wrong?
was pretty exact... What other details should I have included to make diagnosis easier?
PS this is not a flame... i am asking
Posted: Thu Aug 14, 2008 2:58 pm
by Kaeru Gaman
well sorry, as stated above you had two other mistakes in your code that would prevent correct execution.
so I think you work on Linux, since you seem not to need a window for the windowed screen in advance, right?
check if the picture is loaded by implementing the check and the msgreq as in 2.
if this loading does not work (= error message pops up), you have to find out why.
most times you are in the wrong current directory, so you have to get the path of your exe first
and change the current dir or provide a complete path parameter for loading the sprite file.
since I only work under Windows, I don't know if there are additional
issues regarding access rights of files to take care of under Linux.
it is also possible that the format the sprite is saved in is not compatible to the implemented image format decoders.
Posted: Thu Aug 14, 2008 3:38 pm
by wolfwood2x
Well the image and code worked under 3.9 I believe. But that was in windows and not linux. (you are correct I am working in linux now)I am porting over some old spike code to play with and re-learn this stuff. So unless it is no longer compatible the image should still be alright.
Since I am at work now I can't verify the image was in the same directory although I am pretty sure it was. Ill spike out something in a few and see if maybe that was the issue.
Thanks for the suggestions
Posted: Thu Aug 14, 2008 6:53 pm
by wolfwood2x
Alright I have verified that the issue is not the path to the image. I had it in the same directory with the pb file and even tried putting the full path to the image file there and I still get an error. If I use your code I get the no sprite man pop up message. I have also tried using a different image to ensure there was not something wrong with the file and I set the permissions to 777 on it just to make sure it had nothing to do with file system permissions.
Posted: Thu Aug 14, 2008 9:06 pm
by Rook Zimbabwe
So my code did not work for you? I tried Kaeru's code and it works as well...
in windows XP Pro...
Posted: Thu Aug 14, 2008 9:41 pm
by Kaeru Gaman
ok, try a PNG then and the PNG decoder...
I think you need a decoder in every case since there is no BMP under Linux....?
also, try to open a textfile for reading in the same directory,
to make entirely sure you are accessing the correct dir.
and perhaps alter the Title of the topic to put a fat LINUX into it to make the Linux proggers look into here...