Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Just some newbie question — Gideros Forum

Just some newbie question

neverjoyneverjoy Member
edited November 2015 in General questions
Dear sirs, i recently started Gideros a day ago.. I am new to lua and OOP in general.
my first day was full of joy upon seeing my game alive simple it may seem,
but later as i try to create more complex games, i am now in despair.
so pardon my noobness(I dont understand Gideros OOP in full) and please help me in my distress.

so here it is..

this is my attempt at trying to make an existing code under my will.
the plan is to have a function from class "Obstacle" that does collision test.

from my Obstacle.lua
function Obstacle:touchDetect(sprite1,sprite2) --I plan to run this 60 times/sec 	
 
	local x1,y1,w1,h1 =  sprite1:getBounds(stage)
	local x2,y2,w2,h2 =  sprite2:getBounds(stage)
 
.... --detector code here--
end
from my main i call this function through this..
local avatar = Char.new()
stage:addChild(avatar)
 
local cactus1 = Obstacle.new()
stage:addChild(cactus1)
 
Obstacle:touchDetect(avatar,cactus1)
but upon running I get an error that it cannot do "getBounds" because sprite1 is nil.
what i cannot understand is that i set my "avatar" and "cactus" object
first before calling Obstacle:touchDetect so why nil?

Also, i wanted to add other Obstacle instance called "cactus2" now can I make it
like this?
local cactus2 = Obstacle.new()
stage:addChild(cactus2)
then call..
Obstacle:touchDetect(avatar,cactus2)
will it create collision detection separately, say a collision test for (avatar,cactus1) and another collision test for
(avatar,cactus2) simultaneously??

Comments

  • talistalis Guru
    edited November 2015 Accepted Answer
    Ok i don't know your full code but can share with you my knowledge about OOP in gideros.

    As usual OOP logic, in gideros ,a class is consisting of main set of rules, variables and functions.
    And a object is an element of this class. (member of a class using those definitions_

    So in your example if Obstacle is a class in order for you to run a function of this class you need an object that is defined.
    Like:
    local cactus2 = Obstacle.new()
    stage:addChild(cactus2)
     
    cactus2:touchDetect(avatar,cactus2)
    cactus2 is defined as an object of Obstacle class so can use all functions like touchdetetect and so on.

    It can raise nil error because there is no object defined as an obstacle. Or no object defined for the sprites in the constructor of those classes.


  • @talis, sir i sincerely thank you for the reply now i can see that what i am doing is wrong. but what about my last question if i make
    cactus1:touchDetect(avatar,cactus1)
    cactus2:touchDetect(avatar,cactus2)
    Will it perform touchDetect() simultaneously??
  • talistalis Guru
    edited November 2015 Accepted Answer
    By logic if avatar and cactus1 has sprite inside them it will work but don't get the logic so much.
    I mean if you want to use OOP method then your class should have some property like sprite.
    Inside the class constructor you will assign this property like

    Obstacle.sprite = SomeSprite_here

    Just to give you an example let's think a hero class.
    Hero= gideros.class(Sprite)
     
    function Hero:init(sprite, name , hp, alive)
    	self.sprite = sprite
    	self.name = name
    	self.hp = hp	
    	self.alive = alive
    --in here you can also add some event listeners if you want
    end
    --Now let's add some functions to our calls after our constructor(init)
     
    function Hero:attack(attrib,.... (any parameter here) )
    -- some code here	
     
    end
     
    --and it goes on.
    After you finish your class just rename it to Hero.lua and add it in your project that's it.
    For more details about classes look to Scenemanager class or button class. Or download some of the free shared code in the forum. There are so many class examples.
  • piepie Member
    edited November 2015 Accepted Answer
    Lines are executed as you read them, so I believe that there would be a slight delay between them but I don't think it's noticeable.

    There is a post by ar2rsawseen you may be interested in:
    http://giderosmobile.com/forum/discussion/comment/36713#Comment_36713

  • neverjoyneverjoy Member
    edited November 2015
    @talis, sir thanks for the tip, i didnt include the whole code but my obstacle class inherits from sprite.
    a question about your code.. so far ive seen codes that sets their init() variables as local. my question is how is that different from your self.someVariable ? thanks


    @pie sir ,thanks for the heads up actually this is the way i animate things. changing it's texture manually. thanks for the link, it really helped me a lot. thanks
  • simwhisimwhi Member
    Accepted Answer
    @neverjoy Using self.varname will allow you to access variables in any functions / methods that you create in your class. Local variables are just local to the function / method. You cannot access them in other functions / methods as they are destroyed after the function / method call ends.
  • @simwhi aha!! Thanks good sir... You are so kind so glad that i asked. One question sir, if by any chance i create from main a two instance of a class that has self.someVariable on it, will it create a two unique self.someVariable for each created instance?
  • @neverjoy. You are welcome. That is correct
  • neverjoyneverjoy Member
    edited November 2015
    I sincerely thank all of you guys . thank you.
  • john26john26 Maintainer
    Accepted Answer
    Just to add a bit more clarification. In your example,
    cactus1:touchDetect(avatar,cactus1)
    cactus2:touchDetect(avatar,cactus2)
    there is no need to pass the cactus1 and cactus2 arguments. It's better to write it like this.
    cactus1:touchDetect(avatar)
    cactus2:touchDetect(avatar)
    To get a reference to the object within the class method, just use "self" which is a hidden parameter passed in to all methods. So in Talis's example
    Hero= gideros.class(Sprite)
     
    function Hero:init(sprite, name , hp, alive)
    	self.sprite = sprite
    	self.name = name
    	self.hp = hp	
    	self.alive = alive
    --in here you can also add some event listeners if you want
    end
    --Now let's add some functions to our calls after our constructor(init)
     
    function Hero:attack(attrib,.... (any parameter here) )
    -- some code here	
     
    end
    You could also write the attack method as
    function Hero.attack(self,attrib,.... (any parameter here) )
    -- some code here	
     
    end
    That would have the same effect. The colon in Hero:attack just adds the "self" parameter automatically.

    Then you instantiate a hero like this
    roger=Hero.new("roger",4,true)
    roger:attack()
    In your first example you were effectively using a static/class method. You can do that as well (though it's less common)
    function Hero.howMany()
      return Hero.count  -- returns how many where instantiated
    end
    Note the dot rather than colon. You can increase Hero.count when you initialise a new Hero and then count acts like a static/class variable.
  • neverjoyneverjoy Member
    edited November 2015
    @john26 dear sir. Thanks for this wonderful tricks, its a shame i never get to implement this at the game im working at the time of posting this, but i will definitely apply this handy tricks to incoming projects.
Sign In or Register to comment.