It looks like you're new here. If you want to get involved, click one of these buttons!
function Page0:init( pageNo, parent) -- [[ _QUESTION_1_: is ".no" a variable of the function Page0:init? ".background" is also a variable of the same function? ]]-- self.no = pageNo self.background = Bitmap.new(TextureRegion.new(Texture.new("assets/bg.png", true))) self.button= Bitmap.new(TextureRegion.new(Texture.new("assets/button.png", true))) -- just read texture pack dog local dogTexturePack = TexturePack.new("assets/dog.txt", "assets/dog.png") local dogLoader = CTNTAnimatorLoader.new() dogLoader:loadAnimations("assets/dog.tan", dogTexturePack, true) --[[ _QUESTION_2_ According to the example provided with TNTanimator I need to declare the dog class as Sprite: does using CORE.Class(Sprite) instead of GIDEROS.Class(Sprite) (as seen in other examples) make any difference? ]] dog = Core.class(Sprite) function dog:init() self.dog= CTNTAnimator.new(dogLoader) self.dog:setAnimation("DOG_STOP") -- [[ _QUESTION_3_ : What does the following line exactly do? If I remove it, I remove the dog from stage. But I expected that it was self:addChild(object) "giving birth" to things. (see below, QUESTION5 ) ]]-- self.dog:addToParent(self) self.dog:playAnimation() -- _QUESTION_4_: why SELF: and not SELF.DOG? (self.dog places dog to 0,0 on screen) self:setPosition((Swidth /2),(Sheight -150)) end self:addChild(self.background) self:addChild(self.button) --[[ _QUESTION_5_ (this is the line I was referring to in question3) . Is there another way/method to "call my dog?" ]]-- self:addChild(dog.new()) --this is coming from page4 and needed for touch event self:addEventListener(Event.ADDED_TO_STAGE, self.onAddedToStage, self) self:addEventListener(Event.REMOVED_FROM_STAGE, self.onRemovedFromStage, self) end --this is also coming from Page4: I just changed the names accordingly. function Page0:onMouseDown(event) --[[ _QUESTION_6_ : The following line of code on touch gives ERROR "attempt to index field 'dog' (a nil value)" ]]-- if (self.dog:hitTestPoint(event.x, event.y) == true) then print("TOUCH") --[[ If I change it this way: if (dog:hitTestPoint(event.x, event.y) == true) then print("TOUCH") (dog instead of self.dog) I get this other error "index '__userdata' cannot be found" What am I doing wrong? What is __userdata? I also tried changing the hittestPoint area to a Bitmap (button in my code), which is working: but then I have another error trying to GTWeen the dog. (gtween.lua:163: attempt to index field 'target' (a nil value)) Gtween was working when the dog was an AnimatedSprite() I suppose I can't refer to TNTanimatedObjects as I would refer to AnimatedSprites or Bitmaps in GTween lib. How do I address those? ]]-- end end function Page0:onAddedToStage() -- we need mouse functions to interact with the toy self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self) end function Page0:onRemovedFromStage() end |
Comments
Answer 2: Core.class is the standard way now, and Gideros.class was an old way of doing it (although it is still supported AFAIK)
Answer 3: addToParent adds to whatever Display (Spirte) object is higher up in the hierarchy, I would assume (I don't see it in the normal Gideros documentation however)
Answer 4: This really depends on your setup, but in this case you have a containing sprite that dog is a part of. This can be useful for a number of reasons (maybe you have an effect that appears on top of the dog and you want to be able to move the dog and the effect together)
Answer 5: dog.new() is making an instance of a dog class. This allows you to make multiple dogs. The "addChild" etc functions are what make those objects actually get displayed. You could have classes that are only for data, but have nothing to display on the screen, so you might not use "addChild" with them.
Answer 6: Your Page0 class doesn't seem to have a dog variable set. "self.dog = " is in your dog class, but the function "Page0:onMouseDown(event)" would have no access to that, so you need to store your dog in a Page0 instance if you need to access it:
So if anything does not make sense, just ask for more
QUESTION 0
self is a keyword in Gideros class system that references the instance of the class internally. If that makes any sense, if not I can try explaining with some examples
But basically yes as you said, only you can use it across all functions of same class. Meaning it will be the same in all function where you have Page0 defined before the name of the function.
QUESTION_1_
You can define your own arguments as you please, it's not something predefined. It is assigned so it could be accessed from any method (method is a function of a class) so if inside init (the first function which is called when class is created) you define self.no then inside any method you can use self.no value or define any other value or define any other property (property is a variables that instance has as self.no )
_QUESTION_2_
Gideros.class is deprecated and should not be used (because it can be dropped at any moment and your code will just break), so you should use Core.class.
In idea there should be no difference (only if @atilim keeps updating Core.class function and not gideros.class, then Core.class may have some additional features)
_QUESTION_3_
As all the provided animations are somehow handled inside CTNTAnimator class @GregBUG decided that it is more convenient for the user to provide the parent, so he could do all the addChild himself inside the class as he pleases to. Basically it allows to operate inside the class more freely. There is still addChild inside there somewhere.
_QUESTION_4_
Because if you look, you are inside dog:init method now, which means that there self is referenced to dog class instance. Basically you will have a hierarchy as:
Page0 -> dog(which is Sprite) -> dog(which is CTNTAnimator)
This another layer dog(which is Sprite) is added for convenience, basically Sprite object allows you to group and layer you hierarchy, but you could do all the same without it.
_QUESTION_5_
And here basically you add you dog(which is Sprite) instance to Page0. And as you remember inside dog:init method you have provided dog(which is Sprite) as a parent to dog(which is CTNTAnimator)
_QUESTION_6_
Yes because dog is a class and not an instance of a class. And self.dog was created inside dog:init method which also was another scope (in dog class scope).
If you would like to access it from any Page0 method, you need to make it as property and define as self.dogAnimation for example inside Page0:init method. Then it can be accessed as self.dogAnimation in any method of Page0 like this:
For printing I'm usually using this function:
Likes: pie
There's still something I can't figure out at first glance: it's time for me to experiment some more "under those lights"
thank you again,
kind regards
)
Likes: phongtt, pie, tytadas