onedaysnotice wrote:does the nested enemy.move override the base enemy.move or something?
Yes!
onedaysnotice wrote:Whats the difference between that and just calling the function within the other function and just passing variables to it? :s
I'm not quiiite sure what you mean sorry, feel free to elaborate if you're still unclear.
So basically.
In that example,
createEnemy is the main enemy factory. You tell it what you want in the form of (parameters, like, this), it gives you an enemy object.
<( Hi, uh, could I get an enemy, with an image of 'enemyWithNiceShirt.png', an x-position of 200, aaand a y-speed of like... I dunno... 100? )
<(
YOU CERTAINLY CAN. HERE IT IS. I HAVE "RETURNED" IT. AN ENEMY OBJECT JUST FOR YOU. IT HAS TWO FUNCTIONS, "UPDATE" AND "DRAW". I HOPE YOU LIKE IT AS MUCH AS I LIKED CREATING IT. )
<( Uh, th-thank you. )
thisIsAnEnemy = createSwervingEnemy('enemyWithNiceShirt.png', 100, 100)
createSwervingEnemy is like some... enemy customizer shop. It gets an enemy manufactured from the main factory like everyone else, keeps it in its studio
locally, and then adds and replaces some parts of the object, and then returns its own, customized version!
<( Hey Swerve, could I get the usual? )
<( Aight. )
someEnemyWithTheirSwerveOnWhateverThatMeans = createSwervingEnemy('enemyWithQuestionableShirt.png', 152, 100)
<(
GOOD EVENING MISTER SWERVINGSWORTH. HOW CAN I... )
<( 'enemyWithQuestionableShirt.png', 152, 100. Also don't call me that. )
<(
... K )
local enemy = createEnemy(image, x, ySpeed)
(Which is equivalent to...)
local enemy = createEnemy('enemyWithQuestionableShirt.png', 152, 100)
The enemy returned by createEnemy has the following elements:
x, which is 152
y, which is -50
ySpeed, which is 100
image, which is the image of enemy.png
move, which is a function
draw, which is also a function
Now it's time for
createSwervingEnemy to do its thing.
<( I am tinkering on this object. And talking to myself apparently. )
Code: Select all
enemy.startTime = love.timer.getTime()
enemy.fixedX = x
enemy.move = function(dt)
enemy.x= enemy.fixedX + math.cos((love.timer.getTime() - enemy.startTime) * 10) * 10
enemy.y = enemy.y + enemy.ySpeed * dt
end
Now it has a couple of new variables, startTime and fixedX. As for the "move" already existing in the table to begin with, that's just like setting a variable from one value to another, just like this would print 20 instead of 10.
Like Robin said,
enemy.move = function(dt) is another way of writing function
enemy.move(dt), and I've written it like that above. It looks like just changing a variable! And it basically totally is!
So then createSwervingEnemy returns its customized enemy object, and... and everyone lived happily ever after.
I hope this helps... somehow!
EDIT: I actually agree with Inny below if all this seems insanely confusing. You can see an example of using Middleclass
here.