Help improve my code ? (HardonCollider & Middleclass inside)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Help improving my code ?

Post by bartbes »

I am pretty sure this is a problem with your Dynamic code:

Code: Select all

Dynamic.initialize(shape, x, y, image)
which should be:

Code: Select all

Dynamic.initialize(self, shape, x, y, image)
As for your second post:
Lynesth wrote:It is actually possible to do something like that :

Code: Select all

myvar = a
myvar.x = 0
myvar.y = 0
No it isn't. Well, not unless a is a table (hint: objects are tables).
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Help improving my code ?

Post by Robin »

Lynesth wrote:It just doesn't work. Any idea ?
Of course it doesn't work, because self no longer points to the new instance, but to a. If you want to use a to "prototype" self, you can do this:

Code: Select all

for k,v in pairs(a) do
    self[k] = v
end
Lynesth wrote:How can I retrieve "myvar.x" from "somevar" ?
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:

Code: Select all

a._instance = self
you could use somevar._instance.x. But that is probably not what you want.
Help us help you: attach a .love.
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: Help improving my code ?

Post by Lynesth »

<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:

Code: Select all

a._instance = self
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 :)
Attachments
tagada.love
(26.73 KiB) Downloaded 85 times
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: Help improve my code ? (HardonCollider & Middleclass ins

Post by Lynesth »

Bumping to let you guys know what I finally got to.
I managed the "._instance" Robin told me about with a function in main.lua called getObjectfromShape().

What needs to be done :

- Make Circle Dynamics rotate when pushed by player.
- Make player not walk on Rectangle Dynamics when jumping and "running" while close to the Dynamic.
- Anything else ?
Attachments
tagada.love
(26.86 KiB) Downloaded 95 times
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
Post Reply

Who is online

Users browsing this forum: No registered users and 166 guests