Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 676103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:53:28+00:00 2026-05-14T00:53:28+00:00

I just got my keydown method to work. But i get system beep everytime

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:53:29+00:00Added an answer on May 14, 2026 at 12:53 am

    If you “consume” the event, don’t pass it along to super.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just got a script to work by changing $('#thisElement').show(); to $('#thisElement').css({'display':'block'}); where #thisElement
I just got this question on a SE position interview, and I'm not quite
I just got finished watching a railscast episode #189 and the was using a
We just got a new developer and I'm trying to set him up with
i Just got the book Linux Kernel Development by Robert Love . It has
I just got my copy of Expert F# 2.0 and came across this statement,
I just got rejected on my Free app from app store. I uploaded 3
I just got into a new company and much of the code base uses
I just got some space on a VPS server(running on ubuntu 8.04), and I'm
I have a listview with a dozen of rows binded to xml. I'd like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.