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
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:
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.
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.
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.
There is a post by ar2rsawseen you may be interested in:
http://giderosmobile.com/forum/discussion/comment/36713#Comment_36713
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
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Then you instantiate a hero like this
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975