中产阶级问题 [英] middleclass problems

查看:97
本文介绍了中产阶级问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在出现一些问题之后,我现在正在使用LUA中产阶级图书馆,但我似乎无法弄清这种情况.

I'm using the LUA middleclass library now after some problems and I have a situation that I can't seem to figure out.

说我有我的课:输入错误:这是实际功能

Say I have my class: Had a typo: here is the actual functions

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

当我这样做时,上面示例中的每个变量(例如self.name)现在都为nil.我相信这是因为我的职能是:

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:

function weaponCanon2:onWeaponCollision(event)
   ...
end

然后设置一个类似于self.collisionEvent =武器Canon2.onWeaponCollision的碰撞事件变量是不一样的.我不确定100:和之间的区别.运算符是关于LUA的,但是这些给我带来了不同的问题.

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.

现在另一个例子是我具有重置功能.计时器关闭,然后调用复位功能.如果我这样做:

Now another example is that I have a reset function. A timer goes off and then calls a reset function. If I do this:

timer.performWithDelay(100, weaponCanon2.resetShot, 1)

然后在100毫秒内将调用武器CAnon2.resetShot 1次.当这样做时,我所有的self.name等变量均为零.现在,如果我创建我的课程:

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:

require("weaponCanon2")
local canon = weaponCanon2:new("someName")
canon:saveInstance(canon)

然后回到我的班级文件中:

then back in my class file I have:

function saveInstance(value)
   self.instance = value
end

现在,我可以像下面这样调用它来使用此计时器:

Now I can use this timer by calling it like so:

timer.performWithDelay(100, function() self.instance:resetShot(); end, 1)

这将在我的任何成员变量(self.name)==等于nil的情况下工作.在使用您的库或在LUA中,是否有更好/更简便的方法来做到这一点?

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?

很抱歉,不清楚,我在解决这个问题时遇到了麻烦,并且很难解释.

Sorry for being unclear I'm having trouble wrapping my mind around this problem and explaining it is being very difficult.

感谢您的帮助,

-d

推荐答案

好吧,我想我现在已经明白了这个问题.

Ok I think I understand the problem now.

在lua中,这样做:

function something:foo(bar, baz)

与进行此操作相同:

function something.foo(self, bar, baz)

换句话说:':'运算符只是添加一个幻像"自身参数.同样,当您使用它调用函数时:

In other words: the ':' operator simply adds a "phantom" self parameter. Similarly, when you do invoke a function with it:

something:foo(bar, baz)

:"会自动使用某物的值填充"自身参数.等效于:

The ':' is automatically "filling in" the self parameter with the value of something. It's equivalent to:

something.foo(something, bar, baz)

简而言之:武器Canon2.onWeaponCollision实际上接受两个参数:自我和事件.

In short: weaponCanon2.onWeaponCollision takes two parameters in reality: self and event.

但是Corona只会向其传递一个参数:event.您必须诱使Corona传递所需的参数.一种可能的解决方案是将您的函数包装到另一个函数中,如下所示:

But Corona will only pass it one parameter: event. You have to trick Corona into passing the parameter you want; A possible solution is wrapping your function into another function, like this:

self.someObject:addEventListener("touch", function(event) self:onWeaponCollision(event) end)

我希望这可以弄清楚整个:".

I hope this clarifies the whole ":" thing.

我已经完成了Lua教程,该教程介绍了有关Lua的内容以及其他内容.在这里:

I have done a Lua tutorial that explains this, and other things, regarding Lua. It's here:

http://github.com/kikito/lua_missions

它是交互式的;您在Lua编程时会学习Lua.有一章介绍了':'运算符(在table_and_functions内部).它还说明了什么是关闭"以及其他内容.

It's interactive; you learn Lua while programming in Lua. There's a chapter explaining the ':' operator (inside tables_and_functions). It also explains what a "closure" is, as well as other things.

无论如何,我希望这会有所帮助.

In any case, I hope this helps.

致谢!

这篇关于中产阶级问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆