• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

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

Yoel12

New member
Joined
Mar 9, 2019
Messages
8
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
 
gamestate="PLAYED"
in your select case there are no "PLAYED" but got Case "PLAYING"

Another issue is, you must load image inside oncreate(). Example
paused = Loadimage ("Paused.png")
 
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 :)
 
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.
 
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:
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
 
You had a lot of typos in your code. It is very important to write correct uppercase and lowercase letters!
This should work now.

Code:
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:
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:
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:
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.
 
Code:
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.
 
hmm so if I was going to Load a background how would I implement it?? thanks!
 
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:
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!!:)
 
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.
 
Back
Top Bottom