这是一个基本问题,可能会“揭露”我对事物的误解。
使用一些物理引擎我正在创建自己的函数。
他们中的一些人正在使用计时器(在我的情况下是cocos2d计时器:CCSchedulde @selector()
)。
我的目标是建立一些课程,我可以在任何需要做的地方使用。
问题:
2.我可以在另一个类中创建该函数,因此我可以像使用cocos2d引擎一样使用它:
id action=[CCActionName ....] //than i can use action. where is the allocation and initialization?
他们的方式似乎比:
classA *action=[classA alloc]init];
[action functionName:argument]
如何创建我的其他课程看起来像他们的?
现在我这样做:
ClassA.h
int A1;
int B1;
classA.m
-(void)initWith:(int)A AndB:(int)B
{
A1=A;
B1=B;
[run timer function] //COCOS OR NS
}
-(void)timer:(cctime)dt //does not work -its other class and not main thread
{
do the action till something .
}
答案 0 :(得分:0)
答案:
1
当您尝试在另一层上使用cocos2d计时器而不添加该层时,它将无法启动计时器(这就是添加它们的CCLayer
的性质..)因此请创建NSObject类。
2
我找到了一些我喜欢的结构,我希望如果我错了,这里的人会纠正我。
所以我正在创建rootClass
。它有一些函数(动作),在执行时,他创建另一个类的实例 - classA
,其中这个在启动时设置计时器:
rootclass.m
-(void)doActionWith:(int)a
{
classA *A=[classA alloc]init];
[A doActionWith:(int)a];
}
A.m
-(void)doActionWith:(int)a
{
localV=a;
[start NSTimer];
}
-(void)timer:(NSTimer*)me
{
use the localV that got its values
update things till done...
}
,当我们想要使用它时,我们继承rootClass
而不是使用A中的动作而不必分配或创建实例,我们只是这样做:
[self doActionWith:a ] // calls the root class that create a NEW instance of an action class A
我们可以在没有分配的情况下完成我们想要的操作。 可以将许多类似的操作添加到rootClass中。 希望它有所帮助,并且这是一种良好的态度。