2013-06-22 54 views
4

我正试图调用一个函数,其中包含ccactioninterval in Cocos3d。我想以特定的时间间隔呼叫该功能。当我尝试NSTimer时,我发现它有时可用,有时不起作用。在iOS中调用特定时间间隔的动作

 NSTimer makeTarget=[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(createTargets) userInfo:nil repeats:YES]; 

这里createTargets是包含动作事件的函数。当我运行函数时,可以单次工作。当我尝试安排它时,问题就来了。我已经尝试了不同的方法已经解释了相关的问题。但没有为我工作。 。 。 。

下面是代码

-(void) addTargets {  
    NSTimer *makeTarget = [NSTimer scheduledTimerWithTimeInterval:2.0 
       target:self selector:@selector(createTargets) userInfo:nil repeats:YES]; 
} 

-(void)createTargets { 
    CC3MeshNode *target = (CC3MeshNode*)[self getNodeNamed: @"obj1"];  
    int minVal=-5; 
    int maxVal=5;  
    float avgVal; 
    avgVal = maxVal- minVal;  
    float Value = ((float)arc4random()/ARC4RANDOM_MAX)*avgVal+minVal ;   
    [target setLocation:cc3v(Value, 5.0, 0.0)];  
    CCActionInterval *moveTarget = [CC3MoveBy actionWithDuration:7.0 moveBy:cc3v(0.0, -10.0, 0.0)];  
    CCActionInterval *removeTarget = [CCCallFuncN actionWithTarget:self selector:@selector(removeTarget:)];  
    [target runAction:[CCSequence actionOne:moveTarget two:removeTarget]]; 
} 

-(void)removeTarget:(CC3MeshNode*)targ { 
    [self removeChild:targ]; 
    targ=nil; 
} 
+2

你还试过什么,什么都不起作用。你尝试过CCTimer吗? – Wain

+0

我试过调度器和cctimer。已拨打电话但未执行操作 –

+0

如果已拨打电话但未执行操作,我会说问题出在操作上,请显示该代码。 – Wain

回答

5

没有太多的代码,它很难告诉你什么是问题,但这里有一些事情要尝试道歉,如果任何的,这是显而易见的。


你是否坚持对定时器的引用?

这对调试可能很有用。如果你有一个名为makeTargetTimer属性,那么你可以这样做:

NSTimer * makeTargetTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(createTargets) userInfo:nil repeats:YES]; 
self.makeTargetTimer = makeTargetTimer // Save to a property for later use (or just use an iVar) 

只有这样,才能终止重新出现的计时器是invalidate它。因此你可以检查它是否失效。

BOOL isInvalidated = [self.makeTargetTimer isValid]; 

而且你可能想这样做在你的dealloc方法反正:

- (void) dealloc { 
    [_makeTargetTimer invalidate]; // Stops the timer from firing (Assumes ARC) 
} 

你滚动时,甚至应该被接受?

如果你想在滚动的时候触发定时器,那么你需要使用NSRunLoopCommonModesthis question有很好的补偿。

[[NSRunLoop currentRunLoop] addTimer:makeTargetTimer forMode:NSRunLoopCommonModes]; 

什么是你的createTargets实现什么样的?

  • 你把NSLog声明放在这个方法的主体上。你确定它没有被叫?
+0

感谢您的回复。我有与行动有关的问题。我给了一个函数来调用。该函数创建一个meshnode并分配一个动作来执行。我想在特定时间间隔内使用指定操作的节点 –

+0

我的代码已作为评论@ Wain给出。所以请看看它 –

相关问题