Hi
I’m new to Codea, and Lua… but I am really enjoying it.
I have a question about inheritance.
I want to create a class that encapsulates a physics body, and draws a sprite where the body is. To do this, I created a base class, which includes a string for the desired sprite.
The problem is that if I create two objects, each from a different subclass the sprite string always changes to that of the last object that was instantiated.
Block base class
Block = class()
body = nil
srcSprite = nil
function Block:init(x, y, w, h, aSprite)
self.body = physics.body(POLYGON, vec2(0,0), vec2(0,h), vec2(w,h), vec2(w,0))
self.body.x = x
self.body.y = y
self.type = STATIC
srcSprite = aSprite
end
function Block:draw()
pushMatrix()
translate(self.body.x, self.body.y)
sprite(srcSprite, 0, 0)
popMatrix()
end
Subclass 1
WoodBlock = class(Block)
function WoodBlock:init(x, y, w, h)
-- you can accept and set parameters here
Block.init(self, x, y, w, h, "Planet Cute:Wood Block")
end
function WoodBlock:draw()
Block.draw(self)
end
Subclass 2
GrassBlock = class(Block)
function GrassBlock:init(x, y, w, h)
-- you can accept and set parameters here
Block.init(self, x, y, w, h, "Planet Cute:Grass Block")
end
function GrassBlock:draw()
Block.draw(self)
end
Main
supportedOrientations(LANDSCAPE_ANY)
woodBlock = nil
grassBlock=nil
function setup()
woodBlock = WoodBlock(0, 0, 101, 160)
grassBlock = GrassBlock(400, 0, 101, 160)
end
function draw()
woodBlock:draw()
grassBlock:draw()
end
Both blocks will show the second image (grass). If I switch the order in setup(), they both show the wood sprite.
Please help.