NSTimer/NSAffineTransform

Member
Posts: 102
Joined: 2005.01
Post: #1
using them in togeather for the first time by myself and its failed miserably. here is what i have

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;
@end


crashView.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];
}

@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
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.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2006.10
Post: #2
Naming your subclass "CrashView" is just asking for trouble. If you click on the view with the mouse, does it start accepting key presses?
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #3
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
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #4
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.
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #5
I tried the noting out of the timer and it still doesnt work. and i cant upload things, not sure why.
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2004.10
Post: #6
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.
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #7
grunt, didnt work, do i have to rebuild the NSAffineTransform object when i hit a button? im just editing its values...
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Using NSTimer in SDL Loop. Talyn 7 4,212 Oct 7, 2008 09:40 PM
Last Post: OneSadCookie
  NSTimer / animation framerate question MonitorFlickers 5 5,120 Dec 4, 2005 01:10 PM
Last Post: MonitorFlickers
  Cocoa Event Loop/NSTimer revisited Fenris 6 5,273 Oct 29, 2005 11:27 PM
Last Post: maaaaark
  NSTimer Running always or somtimes unknown 3 2,954 Jul 27, 2005 12:49 PM
Last Post: unknown
  NSAffineTransform Examples dancedrummer 2 4,980 Jul 1, 2005 09:34 PM
Last Post: dancedrummer