游戏中的未知错误(XCode)

时间:2016-02-16 04:46:38

标签: ios objective-c iphone xcode

我在XCode上制作了第二个游戏,但代码似乎有问题。它是一个太空射击游戏,玩家跟随你的手指,然后点击释放导弹。问题是......当我按下“开始游戏”时,一切都是隐藏的,不会弹出。这是我的viewcontroller.h和viewcontroller.m

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

int score;
int lives;
int enemyAttackOccurence;
int enemyPosition;
int randomSpeed;
float enemySpeed;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

@implementation PlayViewController

-(void)viewDidAppear:(BOOL)animated {

    // Images that are to be hidden
    playerShip.hidden = YES;
    enemyShip.hidden = YES;
    missile.hidden = YES;
    earth.hidden = YES;

    // Hidden Labels
    scoreLabel.hidden = YES;
    livesLabel.hidden = YES;

    // Set score and lives remaining
    score = 0;
    lives = 0;

    // Strings
    scoreString = [NSString stringWithFormat:@"Score: 0"];
    liveString = [NSString stringWithFormat:@"Lives: 0"];

    // Initial Label Text
    scoreLabel.text = scoreString;
    livesLabel.text = liveString;

    // Image starting positions
    playerShip.center = CGPointMake(150, 658);
    enemyShip.center = CGPointMake(175, 20);
    missile.center = CGPointMake(playerShip.center.x, playerShip.center.y);

}

-(IBAction)startGame:(id)sender {

    // Hide buttons
    startButton.hidden = YES;
    exitButton.hidden = YES;

    // Images to show
    playerShip.hidden = NO;
    enemyShip.hidden = NO;
    earth.hidden = NO;

    // Labels
    scoreLabel.hidden = NO;
    livesLabel.hidden = NO;

    [self positionEnemy];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];

    playerShip.center = CGPointMake(point.x, playerShip.center.y);

}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [missileMovementTimer invalidate];
    missile.hidden = NO;
    missile.center = CGPointMake(playerShip.center.x, playerShip.center.y);

    missileMovementTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(missileMovement) userInfo:nil repeats:YES];

}

-(void)positionEnemy {

    // Random enemy position
    enemyPosition = arc4random() % 249;
    enemyPosition = enemyPosition + 20;

    // Enemy Image Location
    enemyShip.center = CGPointMake(enemyPosition, -40);

    // Set enemy speed
    randomSpeed = arc4random() % 3;
    switch (randomSpeed) {
        case 0:
            enemySpeed = 0.03;
            break;
        case 1:
            enemySpeed = 0.02;
            break;
        case 2:
            enemySpeed = 0.01;

        default:
            break;
    }

    enemyAttackOccurence = arc4random() % 5;
    [self performSelector:@selector(enemyMovementTimerMethod) withObject:nil afterDelay:enemyAttackOccurence];

}

-(void)enemyMovementTimerMethod {

    enemyMovementTimer = [NSTimer scheduledTimerWithTimeInterval:enemySpeed target:self selector:@selector(enemyMovement) userInfo:nil repeats:YES];


}

-(void)enemyMovement {

    enemyShip.center = CGPointMake(enemyShip.center.x, enemyShip.center.y + 2);

    if (CGRectIntersectsRect(enemyShip.frame, earth.frame)) {

        lives = lives - 1;
        liveString = [NSString stringWithFormat:@"Lives: %i", lives];
        livesLabel.text = liveString;

        // Stop Enemy Moving
        [enemyMovementTimer invalidate];

        if (lives > 0) {
            [self positionEnemy];
        }
        if (lives == 0) {
            [self gameOver];
        }

    }

}

-(void)missileMovement {

    missile.hidden = NO;
    missile.center = CGPointMake(missile.center.x, missile.center.y - 2);

    if (CGRectIntersectsRect(missile.frame, enemyShip.frame)) {
        score = score + 1;
        scoreString = [NSString stringWithFormat:@"Score: %i", score];
        scoreLabel.text = scoreString;
        // Stop missile
        [missileMovementTimer invalidate];
        // Position missile to be at the playerShip's center
        missile.center = CGPointMake(playerShip.center.x, playerShip.center.y);
        missile.hidden = YES;
        // Stop enemy movement
        [enemyMovementTimer invalidate];
        [self positionEnemy];
    }

}

-(void)gameOver {

    [enemyMovementTimer invalidate];
    [missileMovementTimer invalidate];
    [self performSelector:@selector(gameReplay) withObject:nil afterDelay:3];

}

-(void) gameReplay {

    // Images that are to be hidden
    playerShip.hidden = YES;
    enemyShip.hidden = YES;
    missile.hidden = YES;
    earth.hidden = YES;

    // Hidden Labels
    scoreLabel.hidden = YES;
    livesLabel.hidden = YES;

    // Set score and lives remaining
    score = 0;
    lives = 0;

    // Strings
    scoreString = [NSString stringWithFormat:@"Score: 0"];
    liveString = [NSString stringWithFormat:@"Lives: 0"];

    // Initial Label Text
    scoreLabel.text = scoreString;
    livesLabel.text = liveString;

    // Image starting positions
    playerShip.center = CGPointMake(150, 658);
    enemyShip.center = CGPointMake(175, 20);
    missile.center = CGPointMake(playerShip.center.x, playerShip.center.y);

}

@end

ViewController.h(仅供备份)

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

    IBOutlet UIButton *startGame;

}

@end
@interface PlayViewController : UIViewController {

    IBOutlet UIImageView *playerShip;
    IBOutlet UIImageView *enemyShip;
    IBOutlet UIImageView *missile;
    IBOutlet UIImageView *earth;

    IBOutlet UILabel *livesLabel;
    IBOutlet UILabel *scoreLabel;

    IBOutlet UIButton *startButton;
    IBOutlet UIButton *exitButton;

    UITouch *touch;

    NSString *liveString;
    NSString *scoreString;

    NSTimer *enemyMovementTimer;
    NSTimer *missileMovementTimer;

}

-(IBAction)startGame:(id)sender;

@end

我正在观看这个游戏的教程,创建的人没有回复。请帮忙 - 我不能再具体了。它必须是代码中的一个奇怪的差距。感谢。

2 个答案:

答案 0 :(得分:1)

此外,您还为StartGame按钮设置了IBOutlet和IBAction。您似乎永远不会使用的IBOutlet。您可能会因为UIButton的IBOutlet和IBAction具有相同的名称而混淆您的编译器。删除IBOutlet,或正确更改名称,看看是否有任何改变。

答案 1 :(得分:0)

我建议您在设置object.hidden = YES和object.hidden = NO时弄乱您的代码行,看看会发生什么。通常,篡改和测试代码是查看正在发生的事情的好方法。确保为UIImageViews设置了图像。我假设它们已在您的界面构建器中设置,因为我看不到您在代码中设置它们的位置。如果没有为UIImageViews设置图像,除非给定特定颜色,否则它们将是透视的。如果篡改代码不起作用,重新观看教程并确保没有弄乱任何东西也不会有害。通常我们观看的教程都已过时,我们只能自己解决一个小问题,这可能是也可能不是这些实例之一。但是,再次测试你的代码,看看当你按下那个按钮时,事情是否真的被设置为隐藏。