Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Array Issues — Gideros Forum

Array Issues

CyberienceCyberience Member
edited January 2013 in General questions
Code is simple, but getting a message of attempt to index global 'assets' (a nil value) on the dots line. There must be somthing blaringly obvious here, but I cant see it. any ideas?

local dots = { assets:init("./images/1.png")}

function assets:init(texture)
local bitmap = Bitmap.new(Texture.new(texture))
self:addChild(bitmap)
end
REAL programmers type copy con filename.exe
---------------------------------------

Comments

  • OZAppsOZApps Guru
    Accepted Answer
    A couple of things

    1. What is assets? you need to have that instantiated before you use it. So adding a line like
    assets = {}
    would help
    2. You need to position the line
    local dots = ...
    after the function declaration
    3. Since you are using
    self:addChild
    , you need that assets is an object that is has the addChild function. So the line we added in #1 needs to change to
    assets = Core.class(Sprite).new()
    then this should work for you.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • well, slap me with a wet fish, I am moving away from javascript, and stumbling into the way od doing things properly again, thanks. it hard trying to find working examples or a good reference book to show how its done.
    REAL programmers type copy con filename.exe
    ---------------------------------------
  • BJGBJG Member
    edited January 2013
    I don't understand what this code is supposed to do. I was wondering if you intended something more like this...(but I'm probably talking nonsense...)
    Assets = Core.class(Sprite)
    function Assets:init(texture)
    local bitmap = Bitmap.new(Texture.new(texture))
    self:addChild(bitmap)
    end
    local dots = {Assets.new("./images/1.png")}
    Otherwise, what is the idea of:
    local dots = {	assets:init("./images/1.png")}
    (Just a novice here.)
  • john26john26 Maintainer
    I agree with @BJG. Also I think it should be
    local dots=Assets.new("/images/1.png")
    No need for curly brackets here I think (unless you really want dots to be a table whose first element is the newly-created asset.)

    I have used "/" not "./" as I think this is more correct. Gideros will automatically load your file from the Resources directory. The "." directory is really only meaningful when using a command prompt/Xterm window.

    Likes: Cyberience

    +1 -1 (+1 / -0 )Share on Facebook
  • yes, I get your meaning about the ./. thanks for that, as in terms of use, I am making an array of dots,, of which I solved the problem already, I needed to initialise the Assets class to work,
    REAL programmers type copy con filename.exe
    ---------------------------------------
  • the way it is now, and working is like this for reference, and hopefully will help others was like this.

    assets = Core.class(Sprite)

    function assets:init(texture)
    local bitmap = Bitmap.new(Texture.new(texture))
    bitmap:setAnchorPoint(0.5, 0.5)
    self:addChild(bitmap)
    end

    dots = { assets.new("./images/1.png"),
    assets.new("./images/2.png"),
    assets.new("./images/3.png"),
    assets.new("./images/4.png"),
    assets.new("./images/5.png") }

    local function onTouchesBegin(event)
    local dot = dots[event.touch.id]
    if dot then
    stage:addChild(dot)
    dot:setPosition(event.touch.x, event.touch.y)
    end
    end

    stage:addEventListener(Event.TOUCHES_BEGIN, onTouchesBegin)

    Theres more, like on move and touch end, but the above code is a complete cycle and works nice......
    REAL programmers type copy con filename.exe
    ---------------------------------------
Sign In or Register to comment.