错误:预期标识符或 xcode ios 5 中的 '('

发布于 2024-12-21 18:34:30 字数 5771 浏览 0 评论 0原文

xcode 4.2 ios 5 单视图模板

我对这种 ios 格式和 xcode 中的这些新功能非常陌生

这是我的所有代码:

#import "SimpleGame2ViewController.h"

#define kStateRunning 1
#define kStateGameOver 2

#define kLeftDown 1
#define kRightDown 2
#define kTouchesEnded 3

#define kPlatformWidth 55
#define kPlatformHeight 16

#define kMaxBallSpeed 10

#define kJumpPower 9

#define kGravity 0.195

@ implementation SimpleGame2ViewController @ synthesize ball;
@synthesize platform1;
@synthesize platform2;
@synthesize platform3;
@synthesize platform4;
@synthesize platform5;
@synthesize bg;
@synthesize gameState;
@synthesize ballVelocity, gravity;
@synthesize touchState;

-(void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

-(void)viewDidLoad {
    [super viewDidLoad];
    gameState = kStateRunning;
    ballVelocity = CGPointMake(0, 0);
    gravity = CGPointMake(0, kGravity);

    [NSTimer scheduledTimerWithTimeInterval: 1.0 / 60
        target: self
        selector: @selector(gameloop)
        userInfo: nil
        repeats:YES];

    // Do any additional setup after loading the view, typically from a nib.
}

-(void)gameloop {
    if (gameState == kStateRunning) {
        [self gameStatePlayNormal];
    } else if (gameState == kStateGameOver) {
        ballVelocity.x = 0;
        ballVelocity.y = 0;

        ball.center = CGPointMake(152 + 16, 326 + 16);
        platform1.center =
            CGPointMake(129 + (kPlatformWidth / 2),
                414 + (kPlatformHeight / 2));
        platform2.center =
            CGPointMake(34 + (kPlatformWidth / 2),
                316 + (kPlatformHeight / 2));
        platform3.center =
            CGPointMake(192 + (kPlatformWidth / 2),
                261 + (kPlatformHeight / 2));
        platform4.center =
            CGPointMake(146 + (kPlatformWidth / 2),
                179 + (kPlatformHeight / 2));
        platform5.center =
            CGPointMake(8 + (kPlatformWidth / 2),
                81 + (kPlatformHeight / 2));
    }
}

-(void)gameStatePlayNormal {
    ballVelocity.y += gravity.y;

    if (touchState == kLeftDown) {
        ballVelocity.x -= 0.2;
    }
    if (touchState == kRightDown) {
        ballVelocity.x += 0.2;
    }
    if (ballVelocity.x > kMaxBallSpeed) {
        ballVelocity.x = kMaxBallSpeed;
    }
    if (ballVelocity.x < -kMaxBallSpeed) {
        ballVelocity.x = -kMaxBallSpeed;
    }

    if (ball.center.x > self.view.bounds.size.width) {
        ball.center = CGPointMake(0, ball.center.y);
    }

    if (ball.center.x < 0) {
        ball.center =
            CGPointMake(self.view.bounds.size.width, ball.center.y);
    }

    ball.center =
        CGPointMake(ball.center.x + ballVelocity.x,
            ball.center.y + ballVelocity.y);

    if (ball.center.y > self.view.bounds.size.height) {
        gameState = kStateGameOver;

    }
}

// Check for a bounce

if (CGRectIntersectsRect(ball.frame, platform1.frame)) {
    if (ball.center.y + 8 < platform1.center.y) {
        if (ballVelocity.y > 0) {
            ballVelocity.y = -kJumpPower;
        }
    }
}

if (CGRectIntersectsRect(ball.frame, platform2.frame)) {
    if (ball.center.y + 8 < platform2.center.y) {
        if (ballVelocity.y > 0) {
            ballVelocity.y = -kJumpPower;
        }
    }
}

if (CGRectIntersectsRect(ball.frame, platform3.frame)) {
    if (ball.center.y + 8 < platform3.center.y) {
        if (ballVelocity.y > 0) {
            ballVelocity.y = -kJumpPower;
        }
    }
}

if (CGRectIntersectsRect(ball.frame, platform4.frame)) {
    if (ball.center.y + 8 < platform4.center.y) {
        if (ballVelocity.y > 0) {
            ballVelocity.y = -kJumpPower;
        }
    }
}

if (CGRectIntersectsRect(ball.frame, platform5.frame)) {
    if (ball.center.y + 8 < platform5.center.y) {
        if (ballVelocity.y > 0) {
            ballVelocity.y = -kJumpPower;
        }
    }
}

-(void)touchesBegan:(NSSet *)
touches withEvent:(UIEvent *) event
{
    if (gameState == kStateRunning) {
        UITouch *touch =[[event allTouches] anyObject];
        CGPoint location =[touch locationInView:touch.view];
        if (location.x < (self.view.bounds.size.width / 2)) {
            touchState = kLeftDown;
            ballVelocity.x -= 0.2;
        } else {
            touchState = kRightDown;
            ballVelocity.x += 0.2;
        }

    }
}

-(void)touchesEnded:(NSSet *)
touches withEvent:(UIEvent *) event
{
    touchState = kTouchesEnded;
}

-(void)viewDidUnload {
    [self setBall:nil];
    [self setPlatform1:nil];
    [self setPlatform2:nil];
    [self setPlatform3:nil];
    [self setPlatform4:nil];
    [self setPlatform5:nil];
    [self setBg:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)viewWillAppear:(BOOL) animated {
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL) animated {
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL) animated {
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL) animated {
    [super viewDidDisappear:animated];
}

-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation !=
        UIInterfaceOrientationPortraitUpsideDown);
}

@end

我有错误,

if (CGRectIntersectsRect(ball.frame, platform1.frame)) {
    if (ball.center.y + 8 < platform1.center.y) {
        if (ballVelocity.y > 0) {
            ballVelocity.y = -kJumpPower;
        }
    }
}

我不知道为什么会发生这种情况

xcode 4.2 ios 5 single view template

I am very new to this ios format and these new features in xcode

Here is all of my code:

#import "SimpleGame2ViewController.h"

#define kStateRunning 1
#define kStateGameOver 2

#define kLeftDown 1
#define kRightDown 2
#define kTouchesEnded 3

#define kPlatformWidth 55
#define kPlatformHeight 16

#define kMaxBallSpeed 10

#define kJumpPower 9

#define kGravity 0.195

@ implementation SimpleGame2ViewController @ synthesize ball;
@synthesize platform1;
@synthesize platform2;
@synthesize platform3;
@synthesize platform4;
@synthesize platform5;
@synthesize bg;
@synthesize gameState;
@synthesize ballVelocity, gravity;
@synthesize touchState;

-(void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

-(void)viewDidLoad {
    [super viewDidLoad];
    gameState = kStateRunning;
    ballVelocity = CGPointMake(0, 0);
    gravity = CGPointMake(0, kGravity);

    [NSTimer scheduledTimerWithTimeInterval: 1.0 / 60
        target: self
        selector: @selector(gameloop)
        userInfo: nil
        repeats:YES];

    // Do any additional setup after loading the view, typically from a nib.
}

-(void)gameloop {
    if (gameState == kStateRunning) {
        [self gameStatePlayNormal];
    } else if (gameState == kStateGameOver) {
        ballVelocity.x = 0;
        ballVelocity.y = 0;

        ball.center = CGPointMake(152 + 16, 326 + 16);
        platform1.center =
            CGPointMake(129 + (kPlatformWidth / 2),
                414 + (kPlatformHeight / 2));
        platform2.center =
            CGPointMake(34 + (kPlatformWidth / 2),
                316 + (kPlatformHeight / 2));
        platform3.center =
            CGPointMake(192 + (kPlatformWidth / 2),
                261 + (kPlatformHeight / 2));
        platform4.center =
            CGPointMake(146 + (kPlatformWidth / 2),
                179 + (kPlatformHeight / 2));
        platform5.center =
            CGPointMake(8 + (kPlatformWidth / 2),
                81 + (kPlatformHeight / 2));
    }
}

-(void)gameStatePlayNormal {
    ballVelocity.y += gravity.y;

    if (touchState == kLeftDown) {
        ballVelocity.x -= 0.2;
    }
    if (touchState == kRightDown) {
        ballVelocity.x += 0.2;
    }
    if (ballVelocity.x > kMaxBallSpeed) {
        ballVelocity.x = kMaxBallSpeed;
    }
    if (ballVelocity.x < -kMaxBallSpeed) {
        ballVelocity.x = -kMaxBallSpeed;
    }

    if (ball.center.x > self.view.bounds.size.width) {
        ball.center = CGPointMake(0, ball.center.y);
    }

    if (ball.center.x < 0) {
        ball.center =
            CGPointMake(self.view.bounds.size.width, ball.center.y);
    }

    ball.center =
        CGPointMake(ball.center.x + ballVelocity.x,
            ball.center.y + ballVelocity.y);

    if (ball.center.y > self.view.bounds.size.height) {
        gameState = kStateGameOver;

    }
}

// Check for a bounce

if (CGRectIntersectsRect(ball.frame, platform1.frame)) {
    if (ball.center.y + 8 < platform1.center.y) {
        if (ballVelocity.y > 0) {
            ballVelocity.y = -kJumpPower;
        }
    }
}

if (CGRectIntersectsRect(ball.frame, platform2.frame)) {
    if (ball.center.y + 8 < platform2.center.y) {
        if (ballVelocity.y > 0) {
            ballVelocity.y = -kJumpPower;
        }
    }
}

if (CGRectIntersectsRect(ball.frame, platform3.frame)) {
    if (ball.center.y + 8 < platform3.center.y) {
        if (ballVelocity.y > 0) {
            ballVelocity.y = -kJumpPower;
        }
    }
}

if (CGRectIntersectsRect(ball.frame, platform4.frame)) {
    if (ball.center.y + 8 < platform4.center.y) {
        if (ballVelocity.y > 0) {
            ballVelocity.y = -kJumpPower;
        }
    }
}

if (CGRectIntersectsRect(ball.frame, platform5.frame)) {
    if (ball.center.y + 8 < platform5.center.y) {
        if (ballVelocity.y > 0) {
            ballVelocity.y = -kJumpPower;
        }
    }
}

-(void)touchesBegan:(NSSet *)
touches withEvent:(UIEvent *) event
{
    if (gameState == kStateRunning) {
        UITouch *touch =[[event allTouches] anyObject];
        CGPoint location =[touch locationInView:touch.view];
        if (location.x < (self.view.bounds.size.width / 2)) {
            touchState = kLeftDown;
            ballVelocity.x -= 0.2;
        } else {
            touchState = kRightDown;
            ballVelocity.x += 0.2;
        }

    }
}

-(void)touchesEnded:(NSSet *)
touches withEvent:(UIEvent *) event
{
    touchState = kTouchesEnded;
}

-(void)viewDidUnload {
    [self setBall:nil];
    [self setPlatform1:nil];
    [self setPlatform2:nil];
    [self setPlatform3:nil];
    [self setPlatform4:nil];
    [self setPlatform5:nil];
    [self setBg:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)viewWillAppear:(BOOL) animated {
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL) animated {
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL) animated {
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL) animated {
    [super viewDidDisappear:animated];
}

-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation !=
        UIInterfaceOrientationPortraitUpsideDown);
}

@end

I have the error around

if (CGRectIntersectsRect(ball.frame, platform1.frame)) {
    if (ball.center.y + 8 < platform1.center.y) {
        if (ballVelocity.y > 0) {
            ballVelocity.y = -kJumpPower;
        }
    }
}

I dont know why this is happening

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

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

发布评论

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

评论(1

雨的味道风的声音 2024-12-28 18:34:30

我已经通过 Lindent 运行了您的代码(一个围绕 indent(1) 程序的小 shell 脚本,具有 Linux 内核开发人员喜欢的设置;它并不完美,但是这是一个好的开始)。

一旦我这样做了,就更容易发现您的一系列 if (CGRectIntersectsRect(..)) 测试实际上并不任何函数中。它们位于源的顶层。

所有语句和表达式必须位于函数内。 (声明和全局定义可以位于顶层。)

找出哪个函数应该“拥有”这一系列例程并将它们放置在函数体内。

I've run your code through Lindent (a small shell script around the indent(1) program with settings that the Linux kernel developers like; it isn't perfect, but it's a good start).

Once I did this, it was far easier to spot that your series of if (CGRectIntersectsRect(..)) tests aren't actually in any function. They're at the top level of your source.

All statements and expressions must be within a function. (Declarations and global definitions can be at top-level.)

Figure out which function should "own" that series of routines and place them within the function body.

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