views:

61

answers:

4

I'm creating an app that is basically an extension of a tutorial (I am very new to the app development world). The tutorial had a cyan coloured box moving around the screen to the point where you touch.

What I did was change the background to an ImageView so it looks like the deep sea and I put a ImageView inside the cyan UIView to display the white silhouette of a diver.

What I want from this experiment/exercise is to trigger bubbles to appear and disappear every couple of seconds both from the diver and the seaweed at the bottom of the screen.

I set up my timer using this Quickie link but I'm getting all sorts of errors from the NSTimer declaration.

Here is my code:

NSTimer *bubbleTimer;

bubbleTimer = [NSTimer scheduledTimerWithTimeInterval: 0.5
                                     target: bubble
                                   selector: @selector(moveBubble:)
                                   userInfo: nil
                                    repeats: YES];

The line NSTimer *bubbleTimer; does not colour the NSTimer, and an error said that it's defaulting bubbleTimer to an int.

As I've said, I'm still trying to find my feet in this world so any help you can give will be appreciated

EDIT:

here's my .h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface TouchView : UIView {

IBOutlet UIView *stretchView; //This defines an object in Interface Builder called stretchView
IBOutlet UIView *bubble;
NSTimer *bubbleTimer;
}

@end

and my .m

#import "TouchView.h"


@implementation TouchView

//First we need a method that processes each touch of the screen
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

//Begin an animation - The animation is given a name which in this case is
//Move and Stretch
[UIView beginAnimations:@"MoveAndStretch" context:nil];
//We set a duration for this animation in seconds
[UIView setAnimationDuration:1];
//Start the animation from the screen's current state
[UIView setAnimationBeginsFromCurrentState:YES];
//Any location changes between here and Commit Animations is animated.
//Firstly we need to process a simple touch which 
if([touches count] == 1) {
    //make a single touch variable
    UITouch *touch = [touches anyObject];
    //change the location of stretchview to the location of the touch
    stretchView.center = [touch locationInView:self];
}
[UIView commitAnimations];
}

- (void)moveBubble:(NSTimer *)theTimer {
CGPoint start;
start.x=480;
start.y=300;
CGPoint end;
end.x=0;
end.y=300;
bubble.center=start;
[UIView beginAnimations:@"Bubble" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationBeginsFromCurrentState:YES];
bubble.center=end;
[UIView commitAnimations];
}

bubbleTimer = [NSTimer scheduledTimerWithTimeInterval: 0.5
                                                        target: self
                                                      selector: @selector(moveBubble:)
                                                          userInfo: nil
                                                           repeats: YES];

@end
+1  A: 

Sounds like you're missing an import.

#import <Foundation/NSTimer.h>

Alex Deem
+1  A: 

Are you including at least the following header at the top of your file:

#import <Foundation/Foundation.h>

and linking with the Foundation framework?

Related: see helpful answer from another Stack Overflow post

petert
I added that to my .h file and now I'm getting a Data definition has no type or storage class
Daniel Hanly
+1  A: 

Might be your formatting, but is ur NSTimer created in a method? It seems to be floating out by itself.

Rudiger
no it's not, I thought that if I declared it outside a method, it'd do it automatically without any triggering (like viewDidLoad or something). I'll try and declare this inside a different method to see if that helps... (The switchover between languages is rough, I'm falling into all my AS3 and PHP habits)
Daniel Hanly
thanks, this fixed it. Cheers
Daniel Hanly
np, if you want to declare it when it loads do it in the init method. -(id)init and make sure you return self. There will be examples on the net of the correct structure of the init method if you wanna do the preferred way.
Rudiger
A: 

The code you posted will not work since you are performing an initialization of the instance variable bubbleTimer outside of an instance method. Just call the scheduledTimer: method when you want the timer to start, but from inside another instance method (like touchesBegan: maybe? But be careful or you'll continue to spawn timers...) and not in the open like a static variable initialization.

sigsegv