我今年 17 岁,对这一切都很陌生,但是我已经学习了一段时间并掌握了基础知识,但是在构建和运行我的应用程序时遇到了没有显示任何内容的问题。
我正在制作一个简单的“类似乒乓球的游戏”,它有一个包含四个选项的菜单,每个选项都有一个不同的“游戏”视图,但是我只是专注于让菜单具有一个“游戏”视图,直到我能找出问题所在在此之前,我为其他 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
我为信息量道歉,但我希望你能意识到我试图尽可能清楚地说明我试图展示/实现的内容,从而让你更容易回答。
如果您需要任何其他信息或发现任何“偏离主题的错误”,请随时告诉我,我将非常感激并张贴任何缺失的信息。我将非常感谢任何帮助,谢谢瑞恩 :)