Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Scope problem? — Gideros Forum

Scope problem?

I am rewriting my code to implement scenemanager. Being just a hobby programmer, I ran in various expected scope problems. But this one really surprised me.

So my main.lua for now is
application:setBackgroundColor(0x000000)
 
print("Starting...")
 
--define scenes
sceneManager = SceneManager.new({
    --start scene
    ["start"] = start,    
    ["level1"] = level1,   
    ["gameover"] = GameOver
})
 
 
--add manager to stage
stage:addChild(sceneManager)
 
sceneManager:changeScene("level1")
My level1.lua starts with this line:
level1 = gideros.class(Sprite)
Somewhere, I have a
function level1:moveRedMotherShip(dt)
And its moveBlueMotherShip(dt) equivalent.
Finally, here is my onEnterFrame function:
function level1:onEnterFrame(e) 
 
	local dt = e.deltaTime
 
	local numSprite = stage:getNumChildren()
 
	level1:moveRedMotherShip(dt) -- move red ship
	level1:moveBlueMotherShip(dt)
end
Everything works here. What does not work is when I change
local numSprite = stage:getNumChildren()
into
local numSprite = level1:getNumChildren()
It spits "index '__userdata' cannot be found"

Yet,
level1:moveBlueMotherShip(dt)
works.
Why the latter works and not level1:getNumSprite() ? I thought that "level1" being a sprite could call any sprite method. Please enlight me. There is something here I need to understand.

Comments

  • MoKaLuxMoKaLux Member
    edited July 2020
    Dafb said:

    level1 = gideros.class(Sprite)
    change it to Core.class(Sprite), it's better.
    Dafb said:

    local numSprite = stage:getNumChildren()
    into
    You need to call:
    local numSprite = self:getNumChildren()
    Dafb said:

    Why the latter works and not level1:getNumSprite() ? I thought that "level1" being a sprite could call any sprite method. Please enlight me. There is something here I need to understand.

    I can't really explain why :o but I think level1 is a class and self is a sprite.

    Likes: hgy29

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • rrraptorrrraptor Member
    edited July 2020
    Dafb said:

    Why the latter works and not level1:getNumSprite() ? I thought that "level1" being a sprite could call any sprite method. Please enlight me. There is something here I need to understand.

    MoKaLux said:

    I can't really explain why :o but I think level1 is a class and self is a sprite.

    "level1" is a class. And "self" refers to object instance. In your case instance is created by sceneManger.

    @Dafb
    You can read about "self" here: http://www.lua.org/pil/16.html

    Also "level1:getNumSprite()" can be used as "level1.getNumSprite(self)"

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • DafbDafb Member
    Thanks to you guys, I fixed the problem.

    I believe the main issue was I forgot to add

    require ("scenemanager")

    in main.lua.

    Silly stuff! I also use now as advised

    Core.class instead of gideros.class

    Everything works now :) and I will read the provided link.

    Thank you again.



    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.