I need help with my coding to change game states! i cant identify the problem

Yoel12

New Member
Joined
Mar 9, 2019
Likes
0
#1
Function main()


'This is all the game code



Class Game_app Extends App



Field Mainmenu:Image

Field Options:Image

Field paused:Image

Field gameover:Image

Global gamestate:String = "Mainmenu"





Method onCreate ()

'All the intialisation are her:

SetUpdaterate 60

Mainmenu = Loadimage ("Mainmenu.png")





End



Method onUpdata()

'All the game logic goes here:

'This is the code to allow the user to chnage between the game states

Select gamestate

Case "MAINMENU"

If KeyHit (KEY_O) Then gamestate="OPTIONS"

If KeyHit (KEY_SPACE) Then gamestate="PLAYED"

Case "PLAYING"

If KeyHit (KEY_P) Then gamestate="PAUSED"

Case "OPTIONS"

If KeyHit (KEY_ESCAPE) Then gamestate="MAINMENU"

Case "PAUSED"

If KeyHit (KEY_P) Then gamestate="PLAYING"

If KeyHit (KEY_ESCAPE) Then gamestate ="MAINMENU"



End





End



Method onRender()

'All the graphics drawing goes here:

Select gamestate

Case "MAINMENU"

DrawImage menu

Case "PLAYING"

Playing = Loadimage ("Playingstate.png")

DrawImage playing

Case "OPTIONS"

Options = Loadimage ("Instructions.png")

DrawImage playing

Case "PAUSED"

paused = Loadimage ("Paused.png")

DrawImage paused



End



End





End
 

Yoel12

New Member
Joined
Mar 9, 2019
Likes
0
#3
in your select case there are no "PLAYED" but got Case "PLAYING"

Another issue is, you must load image insi
1552161771180.png
de oncreate(). Example
1552161818478.png

1552161851824.png

1552161872881.png
Thank you so much for that spot! but I still keep getting this message, bear with me im new to this :)
 

Phil7

Active Member
Joined
Jun 26, 2017
Likes
61
#5
Could you please post your whole code as inserted as code? Just click on the symbol insert (next to the smiley ) in the forum post editor.
This way your code is much easier to read.

One thing I noticed in your first code post is, that Function Main needs an 'end' statement to close it.

Another thing is that it is helpful to start your code with 'Strict'. Then you have to write a little more, but it helps finding bugs.
 

Yoel12

New Member
Joined
Mar 9, 2019
Likes
0
#6
Could you please post your whole code as inserted as code? Just click on the symbol insert (next to the smiley ) in the forum post editor.
This way your code is much easier to read.

One thing I noticed in your first code post is, that Function Main needs an 'end' statement to close it.

Another thing is that it is helpful to start your code with 'Strict'. Then you have to write a little more, but it helps finding bugs.
Cerberus X:
Function main()

 'This is all the game code
 
 Class Game_app Extends App
 
    Field Mainmenu:Image
    Field Options:Image
    Field paused:Image
    Field gameover:Image
    Global gamestate:String = "Mainmenu"
    
    
    Method onCreate ()
    'All the intialisation are her:
            SetUpdaterate 60
            Mainmenu = Loadimage ("Mainmenu.png")
            Paused = Loadimage ("Paused.png")
            Playing = Loadimage ("Playingstate.png")
            Options = Loadimage ("Instructions.png")
   End
          
           Method onUpdate()
           'All the game logic goes here:
           'This is the code to allow the user to chnage between the game states
                  Select gamestate
              Case "MAINMENU"
                  If KeyHit (KEY_O) Then gamestate="OPTIONS"
                  If KeyHit (KEY_SPACE) Then gamestate="PLAYING"
              Case "PLAYING"
                  If KeyHit (KEY_P) Then gamestate="PAUSED"
              Case "OPTIONS"
                  If KeyHit (KEY_ESCAPE) Then gamestate="MAINMENU"
              Case "PAUSED"
                  If KeyHit (KEY_P) Then gamestate="PLAYING"
                  If KeyHit (KEY_ESCAPE) Then gamestate ="MAINMENU"
                  
              End
              
              
   End
  
        Method onRender()
        'All the graphics drawing goes here:
              Select gamestate
                Case "MAINMENU"
                   DrawImage menu
                Case "PLAYING"
                   Playing = Loadimage ("Playingstate.png")
                   DrawImage playing
                Case "OPTIONS"
                   Options = Loadimage ("Instructions.png")
                   DrawImage playing
                Case "PAUSED"
                   paused = Loadimage ("Paused.png")
                   DrawImage paused
                  
                End
                
          End
          
          
     End
 

Phil7

Active Member
Joined
Jun 26, 2017
Likes
61
#7
You had a lot of typos in your code. It is very important to write correct uppercase and lowercase letters!
This should work now.

Working code, but without ImageDraw:
Import mojo ' You need mojo to use the App class and drawing commands.

Function Main()
    New Game_app
    'This is all the game code
End

Class Game_app Extends App

    Field mainmenu:Image
    Field options:Image
    Field paused:Image
    Field gameover:Image
    Field playing:Image
    Global gamestate:String = "MAINMENU"
  
  
    Method OnCreate()
        'All the intialisation are her:
        SetUpdateRate 60
        mainmenu = LoadImage("Mainmenu.png")
        paused = LoadImage("Paused.png")
        playing = LoadImage("Playingstate.png")
        options = LoadImage("Instructions.png")
    End
        
    Method OnUpdate()
        'All the game logic goes here:
        'This is the code to allow the user to chnage between the game states
        Select gamestate
            Case "MAINMENU"
                If KeyHit (KEY_O) Then gamestate="OPTIONS"
                If KeyHit (KEY_SPACE) Then gamestate="PLAYING"
            Case "PLAYING"
                If KeyHit (KEY_P) Then gamestate="PAUSED"
            Case "OPTIONS"
                If KeyHit (KEY_ESCAPE) Then gamestate="MAINMENU"
            Case "PAUSED"
                If KeyHit (KEY_P) Then gamestate="PLAYING"
                If KeyHit (KEY_ESCAPE) Then gamestate ="MAINMENU"
                
        End
            
            
    End

    Method OnRender()
        'All the graphics drawing goes here:
        Cls(0, 0, 0) 'Clears the screen before the new rendering beginns.
        Select gamestate
            Case "MAINMENU"
'                DrawImage mainmenu, 0, 0 'DrawImage is commented out, because I have no Images.
                DrawText "mainmenu", 10, 10
            Case "PLAYING"
'                DrawImage playing, 0, 0
                DrawText "playing", 10, 10
            Case "OPTIONS"
'                DrawImage options, 0, 0
                DrawText "options", 10, 10
            Case "PAUSED"
'                DrawImage paused, 0, 0
                DrawText "paused", 10, 10
                
        End
              
    End
        
        
End
 
Last edited:

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
Likes
198
#8
First welcome to the forums.

Now let's examine the code:
Cerberus is a case sensitive language which means that identifiers and function names must be consistent throughout. For example: myVar, Myvar, myvar would refer to three different variable identifiers. As this also applies to functions and methods, you will find that the method names in your code will not override the inherited ones from App.
For example:
Method onRender() would be a new method and not override the defaultMethod OnRender()

Function main()
Two things here:
  1. The compiler will be looking for the application entry point as Main and not main.
  2. You have not completed the application function entry point block, which is why you are getting the error you are seeing. You will always see errors that are some times cryptic like this if you do not properly terminate a code block such as a loops, class, method, function or conditionals. It's usually a pain to figure out what has gone wrong as the error could be much further up from where the compiler stops.

As you are trying to use what appears to be the mojo module for rendering, you need to import it first with Import mojo
Module imports need to be places at the top of a source file.

Method onRender()
As stated above. Variable and function identifiers are case sensitive, therefore onRedner would be a new method and not override the inherited OnRender from the App class.
You also haven't passed the full parameters in the DrawImage function. You need a minimum of three: the image handle, the X position coordinate and the Y position coordinate.

TIPS:
To save confusion with code blocks. Cerberus allows your to write them like so:
Cerberus X:
Class Game Extends App
    Method OnCreate()
    End Method
 
    Method OnRender()
        Select state
            Case GSTATE_MENU
                'Do menu
        End Select
    End Method
End Class
Instead of using strings for game states, use constant integer data types as it saves memory and misspelling words. It also makes it easier to make changes without having to go through all the code.

You are constantly calling LoadImage which will slow down the render. You should load images in the OnCreate method or somewhere in the program that is not going to be called every game loop.

If you use the Strict mode, then you must explicitly declare the data types for variables and the method and function headers.
E.G.
Cerberus X:
Function MyAddFunction:Int(leftValue:Int, rightValue:Int)
    Retrun leftValue+rightValue
End Function

Function PrintHelloMessage:Void(msg:String = "")
    Local str:="Hellow message "
    Print str+msg
End Function
I would recommend that you explore the examples and read tutorial, especial the online ones.
 

magic

Member
3rd Party Tool Dev
Joined
Mar 5, 2018
Likes
18
#9
Simplify code about GameState:
Import mojo
Function Main();New MyApp;end   
Class MyApp Extends App
    Const MAINMENU=0,PLAYING=1,OPTIONS=2,PAUSED=3
    Field game_state:Int
    Method OnUpdate()
        Select game_state
        Case MAINMENU
            If MouseHit() Then game_state=PLAYING
        Case PLAYING
            If MouseHit() Then game_state=OPTIONS
        Case OPTIONS
            If MouseHit() Then game_state=PAUSED
        Case PAUSED
            If MouseHit() Then game_state=MAINMENU
        End
    End
    Method OnRender()
        Cls
        Select game_state
        Case MAINMENU
            DrawText("GAME MENU",0,0)
        Case PLAYING
            DrawText("GAME PLAYING",0,0)
        Case OPTIONS
            DrawText("OPTIONS",0,0)
        Case PAUSED
            DrawText("GAME PAUSED",0,0)
        End
    End
End
This code is simply version of your GameState using constant.
Avoid using string cos it a bit slow.
 

Phil7

Active Member
Joined
Jun 26, 2017
Likes
61
#11
Just take my code example and remove the comment ' in front of each DrawImage command. Be careful that the spelling of the image filenames is correct and you have them in the "yourAppName.data" folder next to your "yourAppName.cxs" file.
You did your program almost right, just only load the images in the OnCreate Method ... and the comments of the other members here.

BTW: You can see, a lot of people here are trying to help you. Maybe you can help us with this and tell us, what your programming background is. For how long have you been programming and which language? What infos/tutorials did you read to get to where you are with Cerberus? What is your first game, you want to create?
...And also: How did you come to Cerberus in the first place?

And something I should have done some time ago: Welcome to the forums from my side.
 
Last edited:

Yoel12

New Member
Joined
Mar 9, 2019
Likes
0
#12
Ahh yhh I just got the hand of it now its working fine thanks!!

And my bad I really appreciate everyone being patient with me and helping me out its amazing to see, This is basically my first week of programming ever tbh which may be evident looool, and I was recommended by a friend to uuse Cerberus to make a little game project so Ive been researching and deconstructing exemplars to get where i'am as I have realised there isn't many tutorials on Cerberus. And thank you again I really do appreciate it, feel very much welcomed!!:)
 

magic

Member
3rd Party Tool Dev
Joined
Mar 5, 2018
Likes
18
#13
hmm so if I was going to Load a background how would I implement it?? thanks!
Basically, same as other items, you can load the image in OnCreate() and draw it in OnRender().
You just need to draw background first then other item, so that other items will be draw on top of it.
 
Top Bottom