NSTimer/NSAffineTransform
using them in togeather for the first time by myself and its failed miserably. here is what i have
crashView.h
crashView.m
what it does is just sit there, and do absolutly nothing, what i want it to do is move around when i hit the keys... duh
:edit:
i changed keyDown to this
none of the panels pop up....
i checked the view is the first responder of the window and obviously it accepts that its in the code.
crashView.h
Code:
#import <Cocoa/Cocoa.h>
enum{UP, DOWN, LEFT, RIGHT};
@interface crashView : NSView
{
NSBezierPath *obsticales;
NSBezierPath *sirCrashalot;
NSAffineTransform *at;
float dx, dy;
BOOL canChangedir;
}
- (void)moveADirection:(int)dir;
@endcrashView.m
Code:
#import "crashView.h"
@implementation crashView
- (id)initWithFrame:(NSRect)frame
{
NSRect sirCrashalotRect;
self = [super initWithFrame:frame];
if (self) {
dx = 0;
dy = 0;
at = [[NSAffineTransform transform] retain];
[at translateXBy:dx yBy:dy];
sirCrashalotRect = NSMakeRect(([self bounds].size.width/2)-5, 0, 10, 10);
sirCrashalot = [[NSBezierPath bezierPathWithOvalInRect:sirCrashalotRect] retain];
[NSTimer scheduledTimerWithTimeInterval:0.04
target:self
selector:@selector(stepAnimation:)
userInfo:nil
repeats:YES];
}
return self;
}
- (void)drawRect:(NSRect)rect
{
[[NSColor blueColor] set];
[sirCrashalot fill];
}
-(void)keyDown:(NSEvent *)event
{
NSString *key = [event charactersIgnoringModifiers];
switch ([key characterAtIndex:0]) {
case NSUpArrowFunctionKey:
[self moveADirection:UP];
break;
case NSDownArrowFunctionKey:
[self moveADirection:DOWN];
break;
case NSLeftArrowFunctionKey:
[self moveADirection:LEFT];
break;
case NSRightArrowFunctionKey:
[self moveADirection:RIGHT];
break;
}
}
//- (
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (void)moveADirection:(int)dir
{
//moving directions here
switch (dir) {
case UP:
dy = 3;
break;
case DOWN:
dy = -3;
break;
case LEFT:
dx = -3;
break;
case RIGHT:
dx = 3;
break;
}
}
- (void)stepAnimation:(NSTimer *)timer;
{
[sirCrashalot transformUsingAffineTransform:at];
[self setNeedsDisplay:YES];
}
@endwhat it does is just sit there, and do absolutly nothing, what i want it to do is move around when i hit the keys... duh
:edit:
i changed keyDown to this
Code:
-(void)keyDown:(NSEvent *)event
{
NSRunAlertPanel(@"down",nil,@"is a key",nil,nil);
NSString *key = [event charactersIgnoringModifiers];
switch ([key characterAtIndex:0]) {
case NSUpArrowFunctionKey:
NSRunAlertPanel(@"up",nil,@"d",nil,nil);
[self moveADirection:UP];
break;
case NSDownArrowFunctionKey:
NSRunAlertPanel(@"down",nil,@"d",nil,nil);
[self moveADirection:DOWN];
break;
case NSLeftArrowFunctionKey:
NSRunAlertPanel(@"left",nil,@"d",nil,nil);
[self moveADirection:LEFT];
break;
case NSRightArrowFunctionKey:
NSRunAlertPanel(@"right",nil,@"d",nil,nil);
[self moveADirection:RIGHT];
break;
}
}none of the panels pop up....
i checked the view is the first responder of the window and obviously it accepts that its in the code.
Naming your subclass "CrashView" is just asking for trouble. If you click on the view with the mouse, does it start accepting key presses?
yea i guess it is... and no, it was accepting keys in the begining then i added the timer and stuff and it stoped accepting keys
if you take out the timer does it work then? Are you sure that in IB you made your view a crashView? If you upload an example of your project Im sure I can puzzle it out - but nothing jumps out at me from what you posted here.
I tried the noting out of the timer and it still doesnt work. and i cant upload things, not sure why.
I experienced weirdness like this once. I know this sounds weird but I just moved the [NSTImer init] code out of the view's -init routine and into -awakeFromNib, and it worked fine.
Again, if you find a place to put the project, I'm sure someone can help you puzzle it out.
Again, if you find a place to put the project, I'm sure someone can help you puzzle it out.
grunt, didnt work, do i have to rebuild the NSAffineTransform object when i hit a button? im just editing its values...
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Using NSTimer in SDL Loop. | Talyn | 7 | 3,694 |
Oct 7, 2008 09:40 PM Last Post: OneSadCookie |
|
| NSTimer / animation framerate question | MonitorFlickers | 5 | 4,517 |
Dec 4, 2005 01:10 PM Last Post: MonitorFlickers |
|
| Cocoa Event Loop/NSTimer revisited | Fenris | 6 | 4,589 |
Oct 29, 2005 11:27 PM Last Post: maaaaark |
|
| NSTimer Running always or somtimes | unknown | 3 | 2,661 |
Jul 27, 2005 12:49 PM Last Post: unknown |
|
| NSAffineTransform Examples | dancedrummer | 2 | 3,845 |
Jul 1, 2005 09:34 PM Last Post: dancedrummer |
|

