Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
A little help with class and inheritance please... — Gideros Forum

A little help with class and inheritance please...

harlock74harlock74 Member
edited September 2012 in General questions
Hi, got something here that is throwing me off...

I have a class square.lua:
Square = Core.class(Sprite)
 
function Square:init(id) 	
	local squareNormal = Bitmap.new(Texture.new("assets/square.png", true));
	local squareHighlight = Bitmap.new(Texture.new("assets/square_highlight.png", true));
 
	self.id = id;
 
	self:addChild(squareNormal);
	self:addChild(squareHighlight);
 
        self:hideHighlight();
end
 
function Square:showHighlight()
	squareHighlight:setVisible(true);
end
 
function Square:hideHighlight()
	squareHighlight:setVisible(false);
end
and in another class (gamePlay.lua), I create a few instances (using a loop) of the Square class like this:
local squareInst;
squareInst = Square.new(counter);  --Create new instance and give it the current counter as its id              
 
squareInst:setPosition(xPos, yPos);
 
squares[counter] = squareInst;  -- Add instance to table so we can access it later
Now, I have the function below, that is supposed to turn the highlight ON for any of these square... this is where I'm having the problem. This code is in the same gamePlay.lua file that the code that creates the squares.
local function showHighlightedSquare(squareId)
        print("squareId: " .. squareId);
	print("instance id: " .. squares[squareId].id);
	squares[squareId]:showHighlight();
end
So for example, if I call showHighlightSquare(3), the prints show that I AM accessing the correct instance and its id match the one I'm trying to hit... but the last line, which should execute the showHighlight method of this instance, actually executes it for the very last square that got created... so if I make 10 squares, square #10 is the one that highlights all the time. What gives!?!?!

I'm sure it's some silly thing I'm totally confused about. Any help would be greatly appreciate it. Thanks!

Comments

  • MellsMells Guru
    edited September 2012 Accepted Answer
    Hi,

    in that case I would do :
    function Square:init(id) 	
    	self.squareNormal = Bitmap.new(Texture.new("assets/square.png", true));
    	self.squareHighlight = Bitmap.new(Texture.new("assets/square_highlight.png", true));
     
    	self.id = id;
     
    	self:addChild(self.squareNormal);
    	self:addChild(self.squareHighlight);
     
            self:hideHighlight();
    end
     
    function Square:showHighlight()
    	self.squareHighlight:setVisible(true);
    end
     
    function Square:hideHighlight()
    	self.squareHighlight:setVisible(false);
    end
    (Note that I have changed local squareHighlight to self.squareHighlight., and also in showHighlight / hideHighlight.

    In your example my understanding is that squareHighlight is not accessible from Square:showHighglight() because it was declared as local to init()
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • What @Mells said :)

    It's because you want to keep a reference to each square (hence the self. ) but in your first example squareHighlight was local to the init function (and only accessible from there).

    You must have a global elsewhere called "squareHighlight" or else the code would have generated an error as it would have been trying to access a field (setVisible) from a nil value (the default value of any previously unseen variable)

    Hope this helps.

    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • Ahhh.. thank you. That did it!
  • @harlock74
    After posting and away from keyboard I was also wondering why you didn't get a "trying to access a nil value" error in Gideros Studio.

    So, please note that my answer was completed by @techdojo and check your code again if you didn't, else you will get some errors in the future :

    You must have a global elsewhere called "squareHighlight"
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • Yes, I did.. I had left a local squareHighlight at the top of the file and had not realized it was there. Thanks again!
Sign In or Register to comment.