keyDown 有效,但我听到蜂鸣声

发布于 2024-08-25 02:15:55 字数 5111 浏览 7 评论 0原文

我刚刚让我的 keydown 方法起作用了。但每次按下按键时我都会听到系统蜂鸣声。我不知道出了什么问题。谷歌搜索了几个小时,所有人都说,如果你有 keyDown 方法,你还应该实现acceptFirstResponder。这样做了,但仍然不起作用。

#import <Cocoa/Cocoa.h>
#import "PaddleView.h"
#import "BallView.h"

@interface GameController : NSView {
    PaddleView *leftPaddle;
    PaddleView *rightPaddle;
    BallView * ball;

    CGPoint ballVelocity;

    int gameState;

    int player1Score;
    int player2Score;
}

@property (retain) IBOutlet PaddleView *leftPaddle;
@property (retain) IBOutlet PaddleView *rightPaddle;
@property (retain) IBOutlet BallView *ball;

- (void)reset:(BOOL)newGame;

@end

#import "GameController.h"

#define GameStateRunning 1
#define GameStatePause 2

#define BallSpeedX 0.2
#define BallSpeedY 0.3

#define CompMoveSpeed 15

#define ScoreToWin 5

@implementation GameController

@synthesize leftPaddle, rightPaddle, ball;

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];  
    if(self) {      
        gameState = GameStatePause;
        ballVelocity = CGPointMake(BallSpeedX, BallSpeedY);
        [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];

    }
    return self;
}

- (void)gameLoop {
    if(gameState == GameStateRunning) {
        [ball setFrameOrigin:CGPointMake(ball.frame.origin.x + ballVelocity.x, ball.frame.origin.y + ballVelocity.y)];

        if(ball.frame.origin.x + 15 > self.frame.size.width || ball.frame.origin.x < 0) {
            ballVelocity.x =- ballVelocity.x;
        }

        if(ball.frame.origin.y + 35 > self.frame.size.height || ball.frame.origin.y < 0) {
            ballVelocity.y =- ballVelocity.y;
        }
    }

    if(CGRectIntersectsRect(ball.frame, leftPaddle.frame)) {
        if(ball.frame.origin.x > leftPaddle.frame.origin.x) {
            ballVelocity.x =- ballVelocity.x;
        }
    }

    if(CGRectIntersectsRect(ball.frame, rightPaddle.frame)) {
        if(ball.frame.origin.x +15 > rightPaddle.frame.origin.x) {
            ballVelocity.x =- ballVelocity.x;
        }
    }

    if(ball.frame.origin.x <= self.frame.size.width / 2) {
        if(ball.frame.origin.y < leftPaddle.frame.origin.y + 75 && leftPaddle.frame.origin.y > 0) {
            [leftPaddle setFrameOrigin:CGPointMake(leftPaddle.frame.origin.x, leftPaddle.frame.origin.y - CompMoveSpeed)];
        }
        if(ball.frame.origin.y > leftPaddle.frame.origin.y +75 && leftPaddle.frame.origin.y < 700 - leftPaddle.frame.size.height ) {
            [leftPaddle setFrameOrigin:CGPointMake(leftPaddle.frame.origin.x, leftPaddle.frame.origin.y + CompMoveSpeed)];
        }
    }

    if(ball.frame.origin.x <= 0) {
        player2Score++;
        [self reset:(player2Score >= ScoreToWin)];
    }
    if(ball.frame.origin.x + 15 > self.frame.size.width) {
        player1Score++;
        [self reset:(player1Score >= ScoreToWin)];
    }
}

- (void)reset:(BOOL)newGame {
    gameState = GameStatePause;
    [ball setFrameOrigin:CGPointMake((self.frame.size.width + 7.5) / 2, (self.frame.size.height + 7.5)/2)];
    if(newGame) {
        if(player1Score > player2Score) {
            NSLog(@"Player 1 Wins!");
        }
        else {
            NSLog(@"Player 2 Wins!");
        }

        player1Score = 0;
        player2Score = 0;
    }
    else {
        NSLog(@"Press key to serve");
    }

    NSLog(@"Player 1: %d",player1Score);
    NSLog(@"Player 2: %d",player2Score);
}

- (void)moveRightPaddleUp {
    if(rightPaddle.frame.origin.y < 700 - rightPaddle.frame.size.height) {
        [rightPaddle setFrameOrigin:CGPointMake(rightPaddle.frame.origin.x, rightPaddle.frame.origin.y + 20)];
    }
}

- (void)moveRightPaddleDown {
    if(rightPaddle.frame.origin.y > 0) {
        [rightPaddle setFrameOrigin:CGPointMake(rightPaddle.frame.origin.x, rightPaddle.frame.origin.y - 20)];
    }
}

- (BOOL)acceptsFirstResponder {
    return YES;
}

- (void)keyDown:(NSEvent *)theEvent {

    if ([theEvent modifierFlags] & NSNumericPadKeyMask) {
        NSString *theArrow = [theEvent charactersIgnoringModifiers];
        unichar keyChar = 0;
        if ( [theArrow length] == 0 ) {
            return;            // reject dead keys
        }

        if ( [theArrow length] == 1 ) {
            keyChar = [theArrow characterAtIndex:0];

            if ( keyChar == NSLeftArrowFunctionKey ) {
                gameState = GameStateRunning;
            }

            if ( keyChar == NSRightArrowFunctionKey ) {

            }

            if ( keyChar == NSUpArrowFunctionKey ) {
                [self moveRightPaddleUp];
            }

            if ( keyChar == NSDownArrowFunctionKey ) {
                [self moveRightPaddleDown];
            }

            [super keyDown:theEvent];       
        }
    }
    else {
        [super keyDown:theEvent];
    }
}


- (void)drawRect:(NSRect)dirtyRect {

}



- (void)dealloc {
    [ball release];
    [rightPaddle release];
    [leftPaddle release];
    [super dealloc];
}

@end

I just got my keydown method to work. But i get system beep everytime i press key. i have no idea whats wrong. Googled for hours and all people say is that if you have your keyDown method you should also implement the acceptsFirstResponder. did that to and it still doesn't work.

#import <Cocoa/Cocoa.h>
#import "PaddleView.h"
#import "BallView.h"

@interface GameController : NSView {
    PaddleView *leftPaddle;
    PaddleView *rightPaddle;
    BallView * ball;

    CGPoint ballVelocity;

    int gameState;

    int player1Score;
    int player2Score;
}

@property (retain) IBOutlet PaddleView *leftPaddle;
@property (retain) IBOutlet PaddleView *rightPaddle;
@property (retain) IBOutlet BallView *ball;

- (void)reset:(BOOL)newGame;

@end

#import "GameController.h"

#define GameStateRunning 1
#define GameStatePause 2

#define BallSpeedX 0.2
#define BallSpeedY 0.3

#define CompMoveSpeed 15

#define ScoreToWin 5

@implementation GameController

@synthesize leftPaddle, rightPaddle, ball;

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];  
    if(self) {      
        gameState = GameStatePause;
        ballVelocity = CGPointMake(BallSpeedX, BallSpeedY);
        [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];

    }
    return self;
}

- (void)gameLoop {
    if(gameState == GameStateRunning) {
        [ball setFrameOrigin:CGPointMake(ball.frame.origin.x + ballVelocity.x, ball.frame.origin.y + ballVelocity.y)];

        if(ball.frame.origin.x + 15 > self.frame.size.width || ball.frame.origin.x < 0) {
            ballVelocity.x =- ballVelocity.x;
        }

        if(ball.frame.origin.y + 35 > self.frame.size.height || ball.frame.origin.y < 0) {
            ballVelocity.y =- ballVelocity.y;
        }
    }

    if(CGRectIntersectsRect(ball.frame, leftPaddle.frame)) {
        if(ball.frame.origin.x > leftPaddle.frame.origin.x) {
            ballVelocity.x =- ballVelocity.x;
        }
    }

    if(CGRectIntersectsRect(ball.frame, rightPaddle.frame)) {
        if(ball.frame.origin.x +15 > rightPaddle.frame.origin.x) {
            ballVelocity.x =- ballVelocity.x;
        }
    }

    if(ball.frame.origin.x <= self.frame.size.width / 2) {
        if(ball.frame.origin.y < leftPaddle.frame.origin.y + 75 && leftPaddle.frame.origin.y > 0) {
            [leftPaddle setFrameOrigin:CGPointMake(leftPaddle.frame.origin.x, leftPaddle.frame.origin.y - CompMoveSpeed)];
        }
        if(ball.frame.origin.y > leftPaddle.frame.origin.y +75 && leftPaddle.frame.origin.y < 700 - leftPaddle.frame.size.height ) {
            [leftPaddle setFrameOrigin:CGPointMake(leftPaddle.frame.origin.x, leftPaddle.frame.origin.y + CompMoveSpeed)];
        }
    }

    if(ball.frame.origin.x <= 0) {
        player2Score++;
        [self reset:(player2Score >= ScoreToWin)];
    }
    if(ball.frame.origin.x + 15 > self.frame.size.width) {
        player1Score++;
        [self reset:(player1Score >= ScoreToWin)];
    }
}

- (void)reset:(BOOL)newGame {
    gameState = GameStatePause;
    [ball setFrameOrigin:CGPointMake((self.frame.size.width + 7.5) / 2, (self.frame.size.height + 7.5)/2)];
    if(newGame) {
        if(player1Score > player2Score) {
            NSLog(@"Player 1 Wins!");
        }
        else {
            NSLog(@"Player 2 Wins!");
        }

        player1Score = 0;
        player2Score = 0;
    }
    else {
        NSLog(@"Press key to serve");
    }

    NSLog(@"Player 1: %d",player1Score);
    NSLog(@"Player 2: %d",player2Score);
}

- (void)moveRightPaddleUp {
    if(rightPaddle.frame.origin.y < 700 - rightPaddle.frame.size.height) {
        [rightPaddle setFrameOrigin:CGPointMake(rightPaddle.frame.origin.x, rightPaddle.frame.origin.y + 20)];
    }
}

- (void)moveRightPaddleDown {
    if(rightPaddle.frame.origin.y > 0) {
        [rightPaddle setFrameOrigin:CGPointMake(rightPaddle.frame.origin.x, rightPaddle.frame.origin.y - 20)];
    }
}

- (BOOL)acceptsFirstResponder {
    return YES;
}

- (void)keyDown:(NSEvent *)theEvent {

    if ([theEvent modifierFlags] & NSNumericPadKeyMask) {
        NSString *theArrow = [theEvent charactersIgnoringModifiers];
        unichar keyChar = 0;
        if ( [theArrow length] == 0 ) {
            return;            // reject dead keys
        }

        if ( [theArrow length] == 1 ) {
            keyChar = [theArrow characterAtIndex:0];

            if ( keyChar == NSLeftArrowFunctionKey ) {
                gameState = GameStateRunning;
            }

            if ( keyChar == NSRightArrowFunctionKey ) {

            }

            if ( keyChar == NSUpArrowFunctionKey ) {
                [self moveRightPaddleUp];
            }

            if ( keyChar == NSDownArrowFunctionKey ) {
                [self moveRightPaddleDown];
            }

            [super keyDown:theEvent];       
        }
    }
    else {
        [super keyDown:theEvent];
    }
}


- (void)drawRect:(NSRect)dirtyRect {

}



- (void)dealloc {
    [ball release];
    [rightPaddle release];
    [leftPaddle release];
    [super dealloc];
}

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷荒 2024-09-01 02:15:55

如果您“消耗”该事件,请不要将其传递给 super。

If you "consume" the event, don't pass it along to super.

肤浅与狂妄 2024-09-01 02:15:55

捕获事件并防止默认,如:

e.preventDefault();

我从这里拿起这个: 避免在输入按键时发出蜂鸣声一个文本框

Grab the event and prevent default, as in:

e.preventDefault();

I picked this up from here: Avoid Beep sound when enter keypress in a textbox

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文