Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Functions error and g_state CASE problem

Author
Message
Midget Blaster
AGK Developer
7
Years of Service
User Offline
Joined: 7th May 2016
Location:
Posted: 7th May 2016 17:27
I have a function, to initialise the game from the beginning

function init()
headerimg = LoadImage ( "header.png" )
headerplayimg = LoadImage ( "headerplay.png" )

header = CreateSprite ( headerimg )
headerplay = CreateSprite (headerplayimg )

endfunction

This init() is in case 1, of g_state. Now also in the above code, when the header sprite is pressed
if ( GetPointerPressed ( ) = 1 )
if GetSpriteHitTest(headerplay,getpointerx(),getpointery())=1
g_state = 2
delinit()
endif
endif

Delinit() is a function containing
function delinit()
DeleteSprite(headerplay)
DeleteSprite(header)
endfunction

When I try to launch, I get the error message
main.agc:42: error: Variable "header" is used without being defined or initialised
Do I need to use it in global or something? it is defined in the init() function.

Also, is this a good way of going from a title screen to a level selection, and then use something similar in case 3 to launch the actual game?
I have global g_State = 1 from the beginning, and I have the cases in a do---loop, like this
select g_state

case 1:
init()
endcase
///////
case 2:
playlevel()
endcase
///////


Thanks a lot, any help is appreciated
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 7th May 2016 18:04
Yes, you need to declare it as global, because you are using it in different functions. I like to set these things up in a type, then you can take advantage of the codesense.



The state machine you are creating is also okay, although you should reorder the statements so that the most used (playlevel()) is at teh top, and the ones used only once (init(), deinit()) are at the end. it makes the code slightly faster. For this part of the program it is not essential, but it is a good habit to get into.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Midget Blaster
AGK Developer
7
Years of Service
User Offline
Joined: 7th May 2016
Location:
Posted: 7th May 2016 19:43
I placed that at the top of my code, but I still have the same issue. I tried putting "as integer" because that"s how it is in the "Smack It" code. Anything else I can try?
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 8th May 2016 12:01 Edited at: 8th May 2016 12:01
What does the code look like now? It will work if declared properly.

Quote: " I tried putting "as integer" because that"s how it is in the "Smack It" code"


If you don't declare the type, AppGameKit assumes that you are declaring an integer. The exceptions are when suffixed...

myVariable# = float
myVariable$ = text string
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt

Login to post a reply

Server time is: 2024-04-24 01:28:52
Your offset time is: 2024-04-24 01:28:52