Cocos2d Timer to delete objects - iphone

Im trying to create a timer in Cocos2d in which an object is created and deleted 5 seconds after it is created. Does anyone know how to make this?

I'm assuming you mean you are trying to delete a CCSprite when you said object. You setup a timer to call a method which then deletes the sprite by the tag number that you assigned it when you created the sprite (lets pretend you assigned the tag number 10 for the example below)
[self schedule: #selector(delete) interval:5];
-(void)delete{
[self removeChildByTag:10 cleanup:YES];
}

Alternatively, You can use NSTimer like this :
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(removeSprite) userInfo:nil repeats:YES];
-(void)removeSprite {
[self removeChild:spriteName cleanUp:YES];
}

Related

Scroll UISlider Automatically

So currently I have a UISlider in a UIViewcontroller that is meant to start animations within subviews when the user slides.. Basically when the user slides I have this battery with a filling in it that fills the empty battery image with a bar to indicate power within a cell, and the user can slide to see the energy the battery has at certain times of the day.
At the moment, when the View loads I would like the UISlider to AUTOMATICALLY start sliding from the beginning of the slider and scroll to the end within, lets say 5 seconds.
I implemented a loop that cycles through all the values of the uislider using this loop
for (int i = 0; i < [anObject count] - 2; i++)
{
sleep(.25);
NSUInteger index = (NSUInteger)(slider.value + 0.5); // Round the number.
[slider setValue:index animated:YES];
}
[anObject count] - 2 is equal to 62 at this time of day but will change and increment every 15 seconds because I'm fetching data from a server.
But that aside, why doesn't this work? The loop?
EDIT:
So heres what I did with NSTIMER
[NSTimer timerWithTimeInterval:0.25 target:self selector:#selector(animateSlider) userInfo:nil repeats:NO];
and animateSlider looks like this:
- (void)animateSlider:(NSTimer *)timer
{
NSLog(#"Animating");
NSUInteger index = (NSUInteger)(slider.value + 0.5); // Round the number.
[slider setValue:index animated:YES];
}
But no luck... Why isn't NSTimer "firing"..... I remmeber vaguely there was a method that FIRES an nstimer method but not sure if that's needed...
EDIT:
Ahh it does need "Fire"....
NSTimer *timer = [NSTimer timerWithTimeInterval:0.25 target:self selector:#selector(animateSlider) userInfo:nil repeats:NO];
[timer fire];
But for some reason it only fires once.... Any ideas ?
"for some reason it only fires once..."
If you changed the NSTimer set up to this:
NSTimer *timer =
[NSTimer scheduledTimerWithTimeInterval:0.25
target:self
selector:#selector(animateSlider:)
userInfo:nil
repeats:YES];
This would schedule the timer on the current run loop immediately.
And since the "repeats" parameter is "YES", you'd then repeat the timer every quarter second, until you invalidate the timer (which you should do when the ending condition is reached, like when the slider reaches its destination).
P.S. You'd need to change the selector method declaration of your timer's target slightly. According to Apple's documentation, "The selector must correspond to a method that returns void and takes a single argument. The timer passes itself as the argument to this method."
So declare "animateSlider" like this instead:
- (void)animateSlider: (NSTimer *) theTimer;

How can I stop my NSTimer thread from further executing the selector

Currently I am creating a NSTimer that calls my method every one second.
I do this like so:
[NSTimer scheduledTimerWithInterval:1 target:self selector:#selector(clocktick) userInfo:nil repeats:YES];
How can I stop this from calling clocktick?
I have tried assigning it like so: NSTimer *myTimer = [NSTimer ... and then using myTimer = nil; but that did nothing.
You need [myTimer invalidate] that will kill the timer for you.
You have to hold a reference to it, and then send the invalidate message to it:
[myTimer invalidate];
I hope it helps!
Have you tried using 'invalidate' ([timerInstance invalidate]; for example)?
Or is the problem that you don't have a reference to the timer instance anymore?

call method every 5 minutes in my UIView

I want to call a certain method in my UIView code say every 5 minutes - how do I accomplish that?
You can use an NSTimer:
Put the following in your viewDidLoad (where 300 is the number of seconds):
[NSTimer scheduledTimerWithTimeInterval:300.0f
target:self
selector:#selector(updateMethod:)
userInfo:nil
repeats:YES];
And then create your update method:
- (void)updateMethod:(NSTimer *)theTimer {
// Your code goes here
}
You can use NSTimer to do this.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html
Specifically, the
timerWithTimeInterval:target:selector:userInfo:repeats:
class method.
There is an easy way to do this, in you update method
-(void)update{
//Your update methods here
[self performSelector:#selector(update) withObject:nil afterDelay:300.0];
}

image change every a fixed time in iPhone

I have 4 images. I want to load image in first screen and change images after every 10 second. when we click any images than next screen come up.
please give me help i am new in iPhone.
Use NSTimer to trigger the image change. If the user taps an image and you want to stop the image rotation, you can invalidate the timer before switching to the next view.
You can try by creating a method that changes the image in the imageView and calling it using a NSTimer which repeatedly calls the method every 10 seconds.
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(changeImage) userInfo:nil repeats:YES];
- (void)changeImage {
// Change image here
}
HI Avinash,
I think you need below code...
******* IN VIEW DID LOAD
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(changeImage) userInfo:nil repeats:YES];
- (void)changeImage {
[self.imgview setImage:[UIImage imageNamed:#"test.png"]];
}
Thx

How to pause and resume NStimer [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How can I programmatically pause an NSTimer?
I have a question. How can I pause a countdown using a timer? I am developing a game. In the game, I need to go to next view when the timer pauses, and after coming back I want to resume it.
I try this code in the view:
[mytimer pause];
// to resume
[mytimer resume];
I try that code, but I get a warning saying: "NSTimer may not respond to 'pause'"
I build with that warning and when I press the pause button, the app crashes.
NSTimer indeed doesn't have resume and pause methods so you can end up with a crash in runtime after such a warning. Generally you can create 2 kinds of timers (see NSTimer class reference) one that implements only once and the second, that repeats. Example:
This way you create a timer, that will enter callback myMethod each second.
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(myMethod:)
userInfo:nil
repeats:YES];
You probably will choose this one for your purpose where in your class you should maintain some
BOOL pausevariable and in the callback myMethod do the following:
- (void) myMethod:(NSTimer *) aTimer
{
if (!pause) {
// do something
// update your GUI
}
}
where you update pause accordingly somewhere in your code.
To stop the timer (and release it) call
[myTimer invalidate];
good luck
What you want, is what OpenGLES application brings up to you. You should create 2 methods like this:
- (void)startAnimation
{
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:#selector(selector) userInfo:nil repeats:YES];
}
- (void)stopAnimation
{
[animationTimer invalidate];
animationTimer = nil;
}
It's something like this.
Refer to the NSTimer Class Reference, there is no pause method.