If I have enemies and I create Shadows for each of them, is there a way I can pass a continuous positioning of the enemy?
I know I can initiate the Shadow with something like self.shadow = Shadows(self.x,self.y) and then include self.shadow:moveTo(self.x-5, self.y+20) in the parent object update, but if I have to do further calculations, how would I pass the parent parameters into the Shadow:update?
In other words, in my Shadows:update function, I currently have a global variable of PLAYER_X. However, I want to use self.x from the parent object for any object. Thanks!
class('Shadows').extends(AnimatedSprite)
function Shadows:init(x,y)
self.shadowsSprite = gfx.imagetable.new("images/shadows")
Shadows.super.init(self, self.shadowsSprite)
self:moveTo(x, y)
self:playAnimation()
end
function Shadows:update()
self.shadowFrame = math.floor(((PLAYER_X-moonPosX)/#self.shadowsSprite)+(#self.shadowsSprite/2))
end
That's super helpful! However, not sure I'm doing it correctly.
I can called a child through the parent with: self.shadow = Shadows(self.x,self.y,self) then I added the "parent" into function Shadows:init(x,y, parent) as you pointed out. This allows me to print the parent table print(parent) or even positioning `print(parent.x)' in the init function (this is great, but can also do this with x and y for positioning in the init). However, in the child update, I get nil when trying to print the same (below).
function Shadows:update()
print(parent.x)
end
Error: shadows.lua:22: attempt to index a nil value (global 'parent')