Sep 18, 2017
Hi, I would like to ask you how do i get co-ordinates from my game. What I want to do is have a menu with buildings and place them where my mouse is. So what I planned to do is move the cursor, press right mouse button, gets co-ords from the mouse cursor and say DrawImage farm cursor co-ords. How should i do that?

Also, i tried to draw a character in a specific pixel (lets say 300 by 300), however when I launched it, I couldnt move the character because on what i suspect is, I took out x, y. So I got another DrawImage character with x and y and what I ended up with is 2 same sprites, one in 300,300 and other at the top right corner which I can move. How do i make a sprite appear on special pixel and being able to move it?

Tried to use example from this:


Import mojo

Class CGame Extends App
    Field lastTick:Int
    Field secs:Int
    Field mins:Int
    Field hrs:Int
    Field dys:Int
    Field wood:Float
     Field loopedtimeforwood:Int
    Field x:Float
    Field y:Float
     Field character:Image
     Field p1:CGame

    Method OnCreate:Int()
        lastTick = Millisecs()
        loopedtimeforwood = 0
        wood = 0
        character = LoadImage("character.png")
        SetUpdateRate 60
        Return 0
    End Method
    Method OnUpdate:Int()
        Local tick:Int = Millisecs()
        If tick>=(lastTick+1000)
            lastTick = tick
            secs += 1
        If secs > 59 Then
            secs = 0
            mins +=1
            If mins > 59 Then
                mins = 0
                hrs +=1
                If hrs > 23 Then
                    hrs = 0
                    dys += 1
       If loopedtimeforwood >= 20
               If KeyDown (KEY_A) Then wood+=1
           loopedtimeforwood =0
       If KeyDown (KEY_LEFT) Then x = x - 4
       If KeyDown (KEY_RIGHT) Then x = x + 4
       If KeyDown (KEY_UP) Then y = y-4
       If KeyDown (KEY_DOWN) Then y = y +4
       'keydown means keep moving until key is down where keyhit is do a thing until
        Return 0
    End Method
    Method OnRender:Int()
        Cls 124, 256, 0
        DrawText("Mins: "+secs, 200, 200)
        DrawText("Min: "+mins, 570, 10)
        DrawText("Hour: "+hrs, 510, 10)
        DrawText("Day: "+dys, 460, 10)''''''''''''''''
        DrawText("Wood: "+wood, 400, 80)
        DrawImage character, 300, 300
        DrawImage character, x, y
        Return 0
    End Method

End Class

Function Main:Int()
    New CGame()

       Return 0
End Function

Gerry Quinn

Active Member
Jun 24, 2017
The functions you care about are MouseX:Float(), MouseY:Float(), MouseHit:Int( button ), and MouseDown:Int( button ).

MouseX and MouseY tell you where the mouse is (relative to the top left of your window). MouseHit tells you how many times the mouse button - MOUSE_LEFT, MOUSE_RIGHT or MOUSE_MIDDLE - was pressed since the last update. And MouseDown tells you whether a button is being pressed right now.

You can use MouseHit or MouseDown (or both!) depending on what sort of interface you want. MouseHit is enough if you want to click on an object to select it, then click somewhere else to place it. If you want to drag it around the screen with the mouse you'll need MouseDown.



Sep 18, 2017
So if I would want to put something in x,y I could make some variables that take the current mouse position using Mousex and MouseY
and tell the object place at x = variable name y = variable name?

Gerry Quinn

Active Member
Jun 24, 2017
Exactly: if you want to make a sprite move with the mouse, just set its position to the values of XMouse and YMouse on every update.
Top Bottom