Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
stage:addEventListener(Event.MOUSE_DOWN — Gideros Forum

stage:addEventListener(Event.MOUSE_DOWN

Hello there,

I have a couple of questions regarding the title =)

I have this piece of code in my main.lua:
local myGame = require("game")
 
stage:addEventListener(Event.MOUSE_DOWN, function(e)
	myGame.mouseDown(e.x, e.y)
end)

and in my game.lua:
local Game = {}
function Game.mouseDown(ex, ey)
--	local mposx = ex / TILESIZE
--	local mposy = ey / TILESIZE
--	print(mposx, mposy)
	print(ex, ey)
end

My problem is that when I try to do ex / TILESIZE gideros throws the error: attempt to perform arithmetic on global 'TILESIZE' (a nil value)
That's weird because print(ex, ey) works just fine. Do I need to convert ex and ey to some kind of int?

Thank you for your help. Peace!

my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Tagged:

Comments

  • Apollo14Apollo14 Member
    edited March 2019 Accepted Answer
    e.x/e.y are integers, they don't have to be converted.
    You didn't declare global variable 'TILESIZE' (or declared it as local somewhere outside of 'game.lua' file).

    Btw if you want to avoid globals like
    TILESIZE=32
    You can you Gideros' macro constants:
    TILESIZE@32
    They are fast and convenient (of course if you don't need to change them)
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • Thank you very much apollo14, you nailed it!

    I have declared TILESIZE as Game.TILESIZE. My mistake was using TILESIZE and not Game.TILESIZE. Super quick answer thank you a ton!

    Thank you also for the macro.

    My gideros quest continues, youpi!

    Peace and love!
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • The macros act like constants, in certain situations they could end up being faster than variables as the tokeniser within the lua bytecode compiler will be able to optimise them.

    eg:

    tileWidth@32
    gameWidth@20
    zoom=5

    pixels=tileWidth*gameWidth*zoom

    will effectively be in bytecode:
    pixels=640*zoom

    So the interpreter will only need to search for one variable (zoom), if you had them as variables then it would have had to search for all 3 variables so it would have taken longer.

    Likes: Apollo14, MoKaLux

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.