How to override class methods? Is it possible to extend a method or redefine it completely? How about constructors - say I would want to extend the Sprite.init?
Owltwins. Smart design and creative code. »Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
You can both overwrite/redefine methods or extend any existing class
Here's example of overriding Bitmap class to save reference to the texture it was created with
--save new method as some other method
Bitmap.__new = Bitmap.new
--redefine new methodfunction Bitmap.new(texture)--create real Bitmap instance
self = Bitmap.__new(texture)--save texture as property
self.texture = texture
return instance
return self
end--additional method to return texturefunction Bitmap:getTexture()return self.texture
end
And here's of extending Bitmap class with new constructor:
--extend new class from Bitmap
newBitmap = Core.class(Bitmap)--define new constructor (should accept atleast same arguments)function newBitmap:init(texture)--Bitmap constructor is automatically called here--so this class will have all standard methods and properties--of parent class--we only need to add new ones
self.texture = texture
end--and define method to return texturefunction newBitmap:getTexture()return self.texture
end
ähm, yes, I was looking for something like the first example.. but I can't get it to work. What's wrong here?
dolocal spriteInit = Sprite.new --cachefunction Sprite.new()local instance = spriteInit()--call default class behavior
instance.coordinate ={--extend class by a table
x =0,
y =0}return instance
endend
Sprite.coordinate doesn't exist after calling Sprite.new()
EDIT: Also tried this way, which I think is the same:
Sprite.__new = Sprite.new
function Sprite.new()
self = Sprite.__new()
self.coordinate ={
x =0,
y =0}return self
end
Owltwins. Smart design and creative code. »Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
dolocal spriteInit = Sprite.new --cachefunction Sprite.new()local instance = spriteInit()--call default class behavior
instance.coordinate ={--extend class by a table
x =0,
y =0}return instance
endendlocal sprite = Sprite.new()print(sprite.coordinate.x)print(sprite.coordinate.y)
If you want Sprite class and not instance) to have coordinates, then it's easy, you could have done it simply:
Sprite.coordinate ={--extend class by a table
x =0,
y =0}
Because Shape class extends Sprite class somewhere internally, much before you modify Sprite class
I'm still a stupid noob to Gideros, maybe because I aint work regularly with the SDK. The whole FAQ was due to my anchorPoint dilemma some time before, where I asked you about manipulating the getPosition() output. I wanted to rewrite/modify the Sprite class and have the anchorPoints working for all the classes inherited from the Sprite (e.g. Shape). BUT as you pointed out - I have a problem now. To get it work with Sprite, Shape, etc. I have to do it for every class separately, am I right?
Ufff... maybe I should forget about development. Maybe I'm just not the right guy for this stuff.
Owltwins. Smart design and creative code. »Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
No, don't get frustrated. Everyone makes the mistakes while in learning phase.
So what do you do, if you are not a developer?
But to answer your question, yes basically, you'll need to implement setAnchorPoint for every class you want to have it. And it's quite easy to do that.
You can create simple function (I can't remember implementation of anchor point from the top of my head so I'll use what you had):
function implementAnchorpoint(anyClass)local newInit = anyClass.new --cachefunction anyClass.new()local instance = newInit()--call default class behavior
instance.coordinate ={--extend class by a table
x =0,
y =0}return instance
endend--apply to all needed classes
implementAnchorpoint(Sprite)
implementAnchorpoint(Shape)--etc
oh my god.. the solution is elegant and seems now to be so obvious... thank you @ar2rsawseen for being here! Ok. I try my best.
PS: I'm actually designer. But a few years ago I did some web development in php, mysql, html, css and all that stuff. Then I dropped because of the army.. now I'm trying to find my niche.
Owltwins. Smart design and creative code. »Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
I written it down, and I think it should work correctly but somehow it doesn't. Why? Here is my class for AnchorPoints functionality.
localfunction AnchorPoint( Class )
Class.__new = Class.new --cachefunction Class.new()local instance = Class.__new()--call default class behavior--add AnchorPoint methods...
instance.anchorPoint ={
x =0,
y =0,
offsetX =0,
offsetY =0,
}
instance.__setX = Class.setX
instance.__setY = Class.setY
instance.__getX = Class.getX
instance.__getY = Class.getY
function instance:getOffsetX()return self:getWidth()* self.anchorPoint.x
endfunction instance:getOffsetY()return self:getHeight()* self.anchorPoint.y
endfunction instance:setAnchorPoint( cx, cy )
self.anchorPoint.x, self.anchorPoint.y = cx, cy
self:setPosition( self.__getX(self), self.__getY(self))--refreshendfunction instance:getAnchorPoint()return self.anchorPoint.x, self.anchorPoint.y
endfunction instance:setX( x )
self.anchorPoint.offsetX = self:getOffsetX()
self.__setX( self, x - self.anchorPoint.offsetX )endfunction instance:setY( y )
self.anchorPoint.offsetY = self:getOffsetY()
self.__setY( self, y - self.anchorPoint.offsetY )endfunction instance:getX()local x = self.__getX(self) + self.anchorPoint.offsetX
self.anchorPoint.offsetX =0return x
endfunction instance:getY()local y = self.__getY(self) + self.anchorPoint.offsetY
self.anchorPoint.offsetY =0return y
endfunction instance:setPosition( x, y )
self:setX(x)
self:setY(y)endfunction instance:getPosition()print("#", self:getX(), self:getY())--returns (0,0)!return self:getX(), self:getY()endreturn instance
endend--apply to classes
AnchorPoint( Sprite )
AnchorPoint( Shape )
If I create a Shape and call :getPosition() I somehow reach the original behavior not my modified class function.
local rect = Primitive.new()--equivalent to Shape.new()
rect:newRect(0,0, 101,101)--method of Primitive; 101x101px at screen origin
rect:setAnchorPoint(.5, 0)print("R",rect:getPosition())--returns (-59,0)???
My function should have nulled it out by "+self.anchorPoint.offsetX"...
Owltwins. Smart design and creative code. »Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
Well hard to tell without deeper debugging. (BTW you can debug by putting print function inside new added/overwritten methods, to see if everything happens how it's expected.)
Will try to check it deeper, but first comment would be, try not to use reserved variables. As variable Class is a global variable for extendind/creating new classes
Waaahh.. I'm going crazy! @ar2rsawseen I've modified the class and it seems working now, but I can't get the :setRotation method to work! Could you be so good and help me out again? PLEASE! I've prepared the project for you:
I figured out where the issue lies... After the Object was rotated, the rotation point is shifted a little, because the overall shape changed (width, height). So anyone any idea how to get this shifted length?
local dx = self:getAnchorPointOffsetX()local dy = self:getAnchorPointOffsetY()local cosine =math.cos(math.rad(n))local sine =math.sin(math.rad(n))local newX = dx - (dx*cosine - dy*sine)local newY = dy - (dy*cosine + dx*sine)
self:setPosition(self:getX() + newX, self:getY() + newY)
But is does not work as I expected. It still misses about 10px for 15deg and 20px for 30deg on both axis. Don't know where they come form, or what I'm not taking into consideration.
Getting too late here, maybe I should leave it to other evening
Comments
You can both overwrite/redefine methods or extend any existing class
Here's example of overriding Bitmap class to save reference to the texture it was created with
What's wrong here?
EDIT:
Also tried this way, which I think is the same:
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
PS: If I extend the Sprite class with this table, WHY the Shape class doesn't has it too, when a call Shape.new()?
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
The whole FAQ was due to my anchorPoint dilemma some time before, where I asked you about manipulating the getPosition() output.
I wanted to rewrite/modify the Sprite class and have the anchorPoints working for all the classes inherited from the Sprite (e.g. Shape). BUT as you pointed out - I have a problem now. To get it work with Sprite, Shape, etc. I have to do it for every class separately, am I right?
Ufff... maybe I should forget about development. Maybe I'm just not the right guy for this stuff.
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
So what do you do, if you are not a developer?
But to answer your question, yes basically, you'll need to implement setAnchorPoint for every class you want to have it. And it's quite easy to do that.
You can create simple function (I can't remember implementation of anchor point from the top of my head so I'll use what you had):
Likes: talis, jack0088
PS: I'm actually designer. But a few years ago I did some web development in php, mysql, html, css and all that stuff. Then I dropped because of the army.. now I'm trying to find my niche.
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
Meaning you'll be fine, just keep learning
Likes: techdojo
Here is my class for AnchorPoints functionality.
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
Will try to check it deeper, but first comment would be, try not to use reserved variables. As variable Class is a global variable for extendind/creating new classes
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
First thing I noticed, that this value changes. If you comment out print function inside instance:getPosition()
And instead you'll call print("R",rect:getPosition()) --returns (-59,0)??? like you did in the in the end of the code it returns 0,0 coordinate.
Now try calling print("R",rect:getPosition()) two times. First one will return 0,0 and second one will return -59,0
It is because inside getX and getY methods you reset self.anchorPoint.offsetX = 0
So next time you call it anchorPoint is zero again
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
I've prepared the project for you:
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
It still misses about 10px for 15deg and 20px for 30deg on both axis.
Don't know where they come form, or what I'm not taking into consideration.
Getting too late here, maybe I should leave it to other evening
You don't need to reset anchor point to 0 to rotate and then put it back. It can stay in same anchor, somehow I missed that.
So here's a working project:
http://appcodingeasy.com/anchorPointFixed.zip
Now it's definitely good night for me
Edited:
Don't know why I couldn't upload the file to forum. It always ended up as 0byte file
Likes: chipster123, jack0088
Likes: chipster123
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!