Moving an object when a button is clicked

Yoel12

New Member
Joined
Mar 9, 2019
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 X:
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
 

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
To move any image or game object you need to store it's position in variables
e.g.
Cerberus X:
Field playerX:Float= 0
Field playerY:Float= 0
You then update those variables in the OnUpdate Method like so:
Cerberus X:
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 X:
DrawImage PlayerImage, PlayerX, PlayerY
See the tutorials.
 

Yoel12

New Member
Joined
Mar 9, 2019
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
 

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
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 X:
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:

Phil7

Active Member
Joined
Jun 26, 2017
Maybe have a look at how coordinates work. Try this code and move the mouse around.
Cerberus X:
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
 
Top Bottom