• 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

Moving an object when a button is clicked

Yoel12

New member
Joined
Mar 9, 2019
Messages
8
Hey everyone, I was wondering how I can move my goalkeeper along the y-axis when the space bar is pressed this is my coding:
Cerberus:
Import mojo

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

Class Game_app Extends App

    Field mainmenu:Image
    Field optionsmenu:Image
    Field options:Image
    Field paused:Image
    Field gameover:Image
    Field playing:Image
    Global gamestate:String = "MAINMENU"
    Field playingSound:Sound
    Field goalkeeper:Image
    Field football:Image
    
    
    Method OnCreate()
        'All the intialisation are her:
        SetUpdateRate 60
        mainmenu = LoadImage("Mainmenu.png.png")
        paused = LoadImage("Paused.png.png")
        playing = LoadImage("playingstate.png.png")
        optionsmenu = LoadImage("Instructionsstage.png.png")
        options = LoadImage("Instructionsstage.png.png")
        goalkeeper = LoadImage("goalkeeper.png")
        playingSound = LoadSound("playingstatesound.wav")
        football = LoadImage("soccerball.png")
      
        PlaySound(playingSound, 0, 1)
        
    End
        
    Method OnUpdate()
        'All the game logic goes here:
        Select gamestate
            Case "MAINMENU"
                If KeyHit (KEY_O) Then gamestate="OPTIONSMENU"
                If KeyHit (KEY_ENTER) Then gamestate="PLAYING"
            Case "PLAYING"
                If KeyHit (KEY_P) Then gamestate="PAUSED"
                If KeyHit (KEY_ESCAPE) Then gamestate="MAINMENU"
            Case "OPTIONSMENU"
                If KeyHit (KEY_ESCAPE) Then gamestate="MAINMENU"
                If KeyHit (KEY_O) Then gamestate="MAINMENU"
            Case "PAUSED"
                If KeyHit (KEY_P) Then gamestate="PLAYING"
                If KeyHit (KEY_O) Then gamestate="OPTIONS"
                If KeyHit (KEY_ESCAPE) Then gamestate ="MAINMENU"
            Case "OPTIONS"               
                If KeyHit (KEY_ESCAPE) Then gamestate="MAINMENU"
                If KeyHit (KEY_O) Then gamestate="PAUSED"
                If KeyHit (KEY_P) Then gamestate="PLAYING" 
        End
            
            
    End

    Method OnRender()
        'All the graphics drawing goes here:
        Cls(0, 0, 0)
        Select gamestate
            Case "MAINMENU"
                DrawImage mainmenu , 0, 0
            Case "PLAYING"
                DrawImage playing, 0, 0
                DrawImage goalkeeper, -405,-20
                DrawImage football, -250, -72.5
            Case "OPTIONS"
                DrawImage options, 0, 0   
            Case "OPTIONSMENU"
                DrawImage optionsmenu, 0, 0
            Case "PAUSED"
                DrawImage paused, 0, 0   
        End
          
            
    End
        
        
End
 
To move any image or game object you need to store it's position in variables
e.g.
Cerberus:
Field playerX:Float= 0
Field playerY:Float= 0

You then update those variables in the OnUpdate Method like so:
Cerberus:
If KeyDown(KEY_LEFT) And playerX > 0 Then playerX -= 1
If KeyDown(KEY_RIGHT) And playerX < DeviceWidth() Then playerX += 1
If KeyDown(KEY_UP) And playerY > 0 Then playerY -= 1
If KeyDown(KEY_DOWN) And playerY < DeviceHeight() Then playerY += 1

In the OnRender, you then Draw the player image like so:
Cerberus:
DrawImage PlayerImage, PlayerX, PlayerY

See the tutorials.
 
when I draw the player it places it in a random place of the window instead of the coordinates I want it to be in which is x:-405 and y:-20 how do I go about doing this
 
send the code again
 
when I draw the player it places it in a random place of the window instead of the coordinates I want it to be in which is x:-405 and y:-20 how do I go about doing this
I take it that you have added a method to control your playing characters, but have you made sure that when you restart from the menu that the positions have been reset?

Note that negative values are out side of the screens display area.
As magic say. Post some updated code, but do not start any new topics for the this code.

You need to do something like this:
Cerberus:
Select gamestate
            Case "MAINMENU"
                If KeyHit (KEY_O) Then gamestate="OPTIONSMENU"
                If KeyHit (KEY_ENTER) Then
                    gamestate="PLAYING"
                    
                    ' Reset the positions
                    goalkeeperX = 405
                    goalkeeperY = 20
                    ballX = 250
                    ballY = 72.5
                Endif
            Case "PLAYING"
                If KeyHit (KEY_P) Then gamestate="PAUSED"
                If KeyHit (KEY_ESCAPE) Then gamestate="MAINMENU"
                
                If KeyDown(KEY_UP) And goalkeeperY > 0 Then goalkeeperY-=2
                If KeyDown(KEY_DOWN) And goalkeeperY < DeviceHeight()-goalkeeper.Height() Then goalkeeperY+=2
                
            Case "OPTIONSMENU"
                If KeyHit (KEY_ESCAPE) Then gamestate="MAINMENU"
                If KeyHit (KEY_O) Then gamestate="MAINMENU"
            Case "PAUSED"
                If KeyHit (KEY_P) Then gamestate="PLAYING"
                If KeyHit (KEY_O) Then gamestate="OPTIONS"
                If KeyHit (KEY_ESCAPE) Then gamestate ="MAINMENU"
            Case "OPTIONS"               
                If KeyHit (KEY_ESCAPE) Then gamestate="MAINMENU"
                If KeyHit (KEY_O) Then gamestate="PAUSED"
                If KeyHit (KEY_P) Then gamestate="PLAYING"
 
Last edited:
Maybe have a look at how coordinates work. Try this code and move the mouse around.
Cerberus:
Strict

Import mojo

Class Game Extends App
    Method OnCreate:Int()
        SetUpdateRate(60)
        Return 0
    End

    Method OnRender:Int()
        Cls(30, 30, 30)
        DrawCircle(MouseX(), MouseY(), 10)
        DrawText("MouseX: " + MouseX() + ", MouseY: " + MouseY(), 10, 10)
        Return 0
    End
    
    
End

Function Main:Int()
    
    New Game
    Return 0
End
 
Back
Top Bottom