<edit>
Robin wrote:You can't . The value that
somevar points to has no reference to the instance. Of course, if you did something like this in the initialiser:
you could use
somevar._instance.x. But that is probably not what you want.
This doesn't work (I think I just did it wrong but I don't know). I'm getting a "trying to index field '_instance' (a nil value)" when I want to use it.
I've added my .love attached to this post if you want to have a look.
</edit>
Thank you both for your answers.
I'll make things explicit. I'm trying to rewrite my code with Middleclass to make it a bit better (I'll try at least).
Actually there's stuff like this :
Code: Select all
player = collider:addCircle(100, 100, 20)
player.image = love.graphics.newImage('ball.png')
player.x = 0
player.y = 0
player.velocity = {x=0, y=0, a=0}
player.mtv = {x=0, y=0}
player.onGround = false
player.onBox = false
player.boxVelocity = {x=0, y=0}
player.isJumping = false
player.typ = 'player'
player.form = 'circle'
This is what I wanna turn in a class to make things easier when I'll need to create many objects and all.
The thing is that I'm using HardonCollider, and my on_collide callback looks like this :
Code: Select all
function on_collide(dt, a, b, x,y)
local player, moveable, world
if a.typ == 'world' or b.typ == 'world' then
if b.typ == 'world' then
moveable, world = a, b
else
moveable, world = b, a
x, y = -x, -y
end
moveable:move(x,y+1)
v = vector(x,y)
v:normalize_inplace()
moveable.onGround = moveable.onGround or (v.y < -.7)
else
if a.typ == 'player' then
player, moveable = a, b
else
player, moveable = b, a
x, y = -x, -y
end
player:move(x/2, y+1)
moveable:move(-x/2, 0)
v = vector(x,y)
v:normalize_inplace()
player.onBox = player.onBox or (v.y < -.7)
if player.onBox then
player.boxVelocity.x = moveable.velocity.x
player.boxVelocity.y = moveable.velocity.y
end
end
end
a and b being the shape created with HardonCollider. That's why it's ok with my actual code because, for example, if "a == player", "a.typ == player.typ". Which will not be the case with the way my Class is actual defined :
Code: Select all
function Dynamic:initialize(shape, x, y, image)
self.shape = shape
if image then self.image = love.graphics.newImage(image) end
self.x = x
self.y = y
self.velocity = {x=0, y=0, a=0}
self.mtv = {x=0, y=0}
self.onGround = false
self.onBox = false
self.boxVelocity = {x=0, y=0}
self.isJumping = false
self.typ = 'dynamic'
end
Here, if "a == player.shape", "a.typ" would point to "player.shape.typ" which is... undefined. (I really don't know if I made myself clear cause my english isn't very good but eh, I tried to :p)
Thanks again to everyone helping me out
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.