Coin
2005.02.03, 12:41 AM
using them in togeather for the first time by myself and its failed miserably. here is what i have
crashView.h
#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;
@end
crashView.m
#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];
}
@end
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
-(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.
crashView.h
#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;
@end
crashView.m
#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];
}
@end
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
-(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.