PDA

View Full Version : NSTimer/NSAffineTransform


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.

belthaczar
2005.02.03, 02:09 AM
Naming your subclass "CrashView" is just asking for trouble. If you click on the view with the mouse, does it start accepting key presses?

Coin
2005.02.03, 02:43 AM
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

codemattic
2005.02.04, 01:49 AM
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.

Coin
2005.02.04, 02:30 PM
I tried the noting out of the timer and it still doesnt work. and i cant upload things, not sure why.

jfaller
2005.02.04, 05:31 PM
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.

Coin
2005.02.04, 09:23 PM
grunt, didnt work, do i have to rebuild the NSAffineTransform object when i hit a button? im just editing its values...