我今年17岁,对这一切还很陌生,但是我已经学习了一段时间,并且掌握了基本知识,但是在构建和运行应用程序时却遇到了什么都不显示的问题。
我正在制作一个简单的“类似乒乓球的游戏”,它具有一个带有四个选项的菜单,每个菜单具有不同的“游戏”视图,但是,我只是专注于具有一个“游戏”视图的菜单,直到我弄清楚出了什么问题。 beofre我为其他3种类型的“游戏场景”(网球,经典,溜冰场,沙滩球-使用网球atm)添加了其他3个视图。这是我的AppDelegate.h文件的代码;
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ViewController *viewController;
@end
这是AppDelegate.m文件;
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
@synthesize window;
@synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
@end
接下来是ViewContoller.h文件;
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
IBOutlet UIImageView *menuBG;
IBOutlet UIButton *playClassic;
IBOutlet UIButton *playTennis;
IBOutlet UIButton *playIceHockey;
IBOutlet UIButton *playBeachball;
IBOutlet UIImageView *court;
IBOutlet UIImageView *ball;
IBOutlet UIImageView *racquet_blue;
IBOutlet UIImageView *racquet_red;
IBOutlet UILabel *tapToBegin;
IBOutlet UILabel *player_score;
IBOutlet UILabel *computer_score;
CGPoint ballVelocity;
NSInteger gameState;
NSInteger previousState;
NSInteger player_score_value;
NSInteger computer_score_value;
IBOutlet UIButton *back;
}
@property(nonatomic,retain) IBOutlet UIImageView *menuBG;
@property(nonatomic,retain) IBOutlet UIButton *playClassic;
@property(nonatomic,retain) IBOutlet UIButton *playTennis;
@property(nonatomic,retain) IBOutlet UIButton *playIceHockey;
@property(nonatomic,retain) IBOutlet UIButton *playBeachball;
@property(nonatomic,retain) IBOutlet UIImageView *court;
@property(nonatomic,retain) IBOutlet UIImageView *ball;
@property(nonatomic,retain) IBOutlet UIImageView *racquet_red;
@property(nonatomic,retain) IBOutlet UIImageView *racquet_blue;
@property(nonatomic,retain) IBOutlet UILabel *tapToBegin;
@property(nonatomic,retain) IBOutlet UILabel *player_score;
@property(nonatomic,retain) IBOutlet UILabel *computer_score;
@property(nonatomic) CGPoint ballVelocity;
@property(nonatomic) NSInteger gameState;
@property(nonatomic) NSInteger previousState;
@property(nonatomic,retain) IBOutlet UIButton *back;
-(void)reset:(BOOL) newGame;
-(IBAction)playClassicButtonClicked;
-(IBAction)playTennisButtonClicked;
-(IBAction)playIceHockeyButtonClicked;
-(IBAction)playBeachballButtonClicked;
-(IBAction)BackButtonClicked;
@end
这是ViewContoller.m文件;
#import "ViewController.h"
#define kGameStateRunning 1
#define kGameStatePaused 2
#define kStateMenu 1
#define kBallSpeedX 4
#define kBallSpeedY 6
#define kCompMoveSpeed 3
#define kScoreToWin 5
@implementation ViewController
@synthesize ball,racquet_blue,racquet_red,player_score,computer_score,gameState,ballVelocity,tapToBegin,back,menuBG,playBeachball,playClassic,playIceHockey,playTennis,court,previousState;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(gameState == kGameStatePaused) {
tapToBegin.hidden = YES;
gameState = kGameStateRunning;
} else if(gameState == kGameStateRunning) {
[self touchesMoved:touches withEvent:event];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
CGPoint xLocation = CGPointMake(location.x,racquet_blue.center.y);
racquet_blue.center = xLocation;
}
-(void) gameLoop {
// Running
if(gameState == kGameStateRunning) {
if (previousState != kGameStateRunning)
{
menuBG.hidden = 1;
playClassic.hidden = 1;
playTennis.hidden = 1;
playIceHockey.hidden= 1;
playBeachball.hidden = 1;
ball.hidden = 0;
court.hidden = 0;
racquet_blue.hidden = 0;
racquet_red.hidden = 0;
player_score.hidden = 0;
computer_score.hidden = 0;
tapToBegin.hidden = 0;
back.hidden = 0;
}
previousState = kGameStateRunning;
}
else if (gameState == kStateMenu)
{
if(previousState != kStateMenu) {
menuBG.hidden = 0;
playClassic.hidden = 0;
playTennis.hidden = 0;
playIceHockey.hidden = 0;
playBeachball.hidden = 0;
ball.hidden = 1;
court.hidden = 1;
racquet_blue.hidden = 1;
racquet_red.hidden = 1;
player_score.hidden = 1;
computer_score.hidden = 1;
tapToBegin.hidden = 1;
back.hidden = 1;
}
previousState = kStateMenu;
}
ball.center = CGPointMake(ball.center.x + ballVelocity.x , ball.center.y + ballVelocity.y);
if(ball.center.x > self.view.bounds.size.width || ball.center.x < 0) {
ballVelocity.x = -ballVelocity.x;
}
if(ball.center.y > self.view.bounds.size.height || ball.center.y < 0) {
ballVelocity.y = -ballVelocity.y;
}
if(CGRectIntersectsRect(ball.frame,racquet_blue.frame)) {
if(ball.center.y < racquet_blue.center.y) {
ballVelocity.y = -ballVelocity.y;
NSLog(@"%f %f",ball.center,racquet_red.center);
}
}
if(CGRectIntersectsRect(ball.frame,racquet_red.frame)) {
if(ball.center.y > racquet_red.center.y) {
ballVelocity.y = -ballVelocity.y;
}
}
// Begin Simple AI
if(ball.center.y <= self.view.center.y) {
if(ball.center.x < racquet_red.center.x) {
CGPoint compLocation = CGPointMake(racquet_red.center.x - kCompMoveSpeed, racquet_red.center.y);
racquet_red.center = compLocation;
}
if(ball.center.x > racquet_red.center.x) {
CGPoint compLocation = CGPointMake(racquet_red.center.x + kCompMoveSpeed, racquet_red.center.y);
racquet_red.center = compLocation;
}
}
// Begin Scoring Game Logic
if(ball.center.y <= 0) {
player_score_value++;
[self reset:(player_score_value >= kScoreToWin)];
}
if(ball.center.y > self.view.bounds.size.height) {
computer_score_value++;
[self reset:(computer_score_value >= kScoreToWin)];
}
else {
if(tapToBegin.hidden) {
tapToBegin.hidden = NO;
}
}
}
-(void)reset:(BOOL) newGame {
self.gameState = kGameStatePaused;
ball.center = self.view.center;
if(newGame) {
if(computer_score_value > player_score_value) {
tapToBegin.text = @"Computer Wins!";
} else {
tapToBegin.text = @"Player Wins!";
}
computer_score_value = 0;
player_score_value = 0;
} else {
tapToBegin.text = @"Tap To Begin";
}
player_score.text = [NSString stringWithFormat:@"%d",player_score_value];
computer_score.text = [NSString stringWithFormat:@"%d",computer_score_value];
}
- (void)viewDidLoad {
[super viewDidLoad];
gameState = kStateMenu;
self.gameState = kGameStatePaused;
ballVelocity = CGPointMake(kBallSpeedX,kBallSpeedY);
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
}
-(IBAction)playClassicButtonClicked {
gameState = kGameStateRunning;
}
-(IBAction)playTennisButtonClicked {
gameState = kGameStateRunning;
}
-(IBAction)playIceHockeyButtonClicked {
gameState = kGameStateRunning;
}
-(IBAction)playBeachballButtonClicked {
gameState = kGameStateRunning;
}
- (IBAction)BackButtonClicked {
gameState = kStateMenu;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
@end
对于大量信息,我深表歉意,但我希望您能意识到,我正在尽力使我要显示/实现的内容尽可能清晰,从而使您更容易回答。
如果您需要任何其他信息或发现任何“主题错误”,请随时告诉我,我将非常感谢并将其张贴在任何缺少的信息上。我非常感谢您的帮助,谢谢Ryan :)
最佳答案
[window addSubview:viewController.view];
[window makeKeyAndVisible];
而不是上面
做这个
window.rootViewController = viewController;
[window makeKeyAndVisible];
关于iphone - 构建并运行应用程序后,iPhone Simulator屏幕为黑色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13921044/