Note that there are some explanatory texts on larger screens.

plurals
  1. POmiddleclass problems
    text
    copied!<p>I'm using the LUA middleclass library now after some problems and I have a situation that I can't seem to figure out. </p> <p>Say I have my class: EDIT: Had a typo: here is the actual functions</p> <pre><code>require "middleclass" weaponCanon2 = class("weaponCanon2") function weaponCanon2:onWeaponCollision(event) if (event.phase == "began") then if (event.other.name ~= "ground") then self.canonBall.inAir = false end end end function weaponCanon2:initialize(atX, atY, inGroup) self.name = "some value" self.someObject:addEventListener("touch", **weaponCanon2.onWeaponCollision**) ... end </code></pre> <p>When I do this, every variable such as self.name in the above example is now nil. I believe this to be because my function is:</p> <pre><code>function weaponCanon2:onWeaponCollision(event) ... end </code></pre> <p>Then setting a collision event varible like self.collisionEvent = weaponCanon2.onWeaponCollision is not the same thing. I am not 100% sure what the difference between the : and . operator is in terms of LUA but these give me different problems. </p> <p>Now another example is that I have a reset function. A timer goes off and then calls a reset function. If I do this:</p> <pre><code>timer.performWithDelay(100, weaponCanon2.resetShot, 1) </code></pre> <p>Then in 100ms it will call weaponCAnon2.resetShot 1 time. When it does this all my self.name etc variables are nil. Now if I create my class:</p> <pre><code>require("weaponCanon2") local canon = weaponCanon2:new("someName") canon:saveInstance(canon) </code></pre> <p>then back in my class file I have:</p> <pre><code>function saveInstance(value) self.instance = value end </code></pre> <p>Now I can use this timer by calling it like so:</p> <pre><code>timer.performWithDelay(100, function() self.instance:resetShot(); end, 1) </code></pre> <p>This will work without any of my member variables (self.name) being == to nil. Is there a better/easier way to do this when using your library or in LUA?</p> <p>Sorry for being unclear I'm having trouble wrapping my mind around this problem and explaining it is being very difficult.</p> <p>Thanks for the help,</p> <p>-d</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload