4

I am trying to invoke a function that contains ccactioninterval in Cocos3d. I want to call that function at specific time intervals.When I tried NSTimer , i found that it works sometimes and sometimes not.

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

Here createTargets is the function that contains action events. when i run the function straightit works fine for single time. Problem comes when i try to schedule it. I ve tried different methods already explained for related questions . But nothing worked for me. . . .

Here is the code

-(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; 
}
Robert
  • 37,670
  • 37
  • 171
  • 213
Karan Alangat
  • 2,154
  • 4
  • 25
  • 56
  • 2
    What else did you try and what didn't work. Did you try `CCTimer`? – Wain Jun 22 '13 at 11:09
  • i tried scheduler and cctimer. call has been made but action is not performed – Karan Alangat Jun 27 '13 at 07:12
  • If the call is made but the actions aren't run I'd say the problem is with the actions, please show that code. – Wain Jun 27 '13 at 07:15
  • 1
    I ve used arc4random to set location of my meshnode. removeTarget function contains code to remove meshnode. – Karan Alangat Jun 27 '13 at 07:28
  • 1
    Have you debugged the method to check if `target` is found? – Wain Jun 27 '13 at 09:10
  • I gave counter in this method and found it s increasing continuously. So the counter seems to be working fine. When i run i can c only one meshnode moving, and at intervals a glimpse of another node comes as regular. – Karan Alangat Jun 27 '13 at 09:30
  • @KaranAlangat Please edit your question to include. 1) Your implementation of the `createTargets` method (+ any other relevant code it calls). 2) Exactly what parts of the code are being called and what parts are not beting called (use `NSLog`s). 3) What your symptoms are. 4) The context around creating your `NSTimer`, (are you creating multiple timers?) – Robert Jul 02 '13 at 09:55
  • I believe the code tells everything – Karan Alangat Jul 05 '13 at 04:19

1 Answers1

5

Without much code its hard to tell what you issues is, but here are some things to try apologies if any of this is obvious.


Are you holding onto a reference to the timer?

This might be useful for debugging. If you have a property called makeTargetTimer, then you could do this:

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)

The only way to stop a re-occurring timer is to invalidate it. Therefore you could check to see if its been invalidated.

BOOL isInvalidated = [self.makeTargetTimer isValid];

Also you might want to do this in your dealloc method anyway:

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

Are you scrolling when the even should be received?

If you want the timer to be fired while scrolling then you need to use NSRunLoopCommonModes. There is a excellent expiation in this question.

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

What is your implementation of createTargets like?

  • Have you put NSLog statements on the body of this method. Are you certain its not being called?
Community
  • 1
  • 1
Robert
  • 37,670
  • 37
  • 171
  • 213
  • thanks for your response . I have problem related to action. I have given a function to call. That function creates a meshnode and assigns an action to perform. I want that node with specified actions on specific intervals – Karan Alangat Jun 27 '13 at 07:17
  • My code has been given as comment @ Wain. So please have a look at it – Karan Alangat Jun 27 '13 at 07:32