我正在开发我的第一个基础 iOS 应用程序。我确定我在这里做错了什么,但我似乎无法找出问题所在。
我有一个包含一些 IBActions 的应用程序,它做两件事:
1)通过 NSLog 输出一些简单的文本(让我知道操作有效) -这工作正常(它通过 NSLog 输出我的文本)
2) 调用自定义类中的方法,该方法还应输出 NSLog 语句。-这不起作用(没有通过 NSLog 输出文本)
我还在为在哪里创建我的类的实例而苦苦挣扎,以便在我的代码中的其他地方可以访问它们。
这是代码:
//
// ViewController.h
// OrcAndPie
//
// Created by me on 4/27/13.
// Copyright (c) 2013 me. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Player.h"
#import "Orc.h"
@interface ViewController : UIViewController {
// Variables that you want to access globally go here?
Player *wizard;
Orc *grunty;
}
-(IBAction)takePie:(id)sender;
-(IBAction)castFireball:(id)sender;
-(IBAction)attack:(id)sender;
@end
和
//
// ViewController.m
// OrcAndPie
//
// Created by me on 4/27/13.
// Copyright (c) 2013 me. All rights reserved.
//
#import "ViewController.h"
#import "Player.h"
@interface ViewController ()
@end
@implementation ViewController
-(IBAction)takePie:(id)sender{
// Player attempts to take the pie
// If Orc's health is > 0, don't let the player take the pie
// If the Orc's health is <= zero, let the player take the pie
NSLog(@"IBAction - player attempted to take the pie.");
[wizard takePie];
}
-(IBAction)castFireball:(id)sender{
NSLog(@"IBAction - player cast fireball.");
[wizard castFireball];
}
-(IBAction)attack:(id)sender{
NSLog(@"IBAction - player attacked.");
[wizard attackOrc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Player *wizard = [[Player alloc]init];
Orc *grunty = [[Orc alloc]init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
和
//
// Player.h
// OrcAndPie
//
// Created by me on 4/27/13.
// Copyright (c) 2013 me. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Player : NSObject
@property int maxHealth;
@property int currentHealth;
@property int armorClass;
@property int meleeToHitModifier;
-(void)setHeathBar;
-(void)attackOrc;
-(void)castFireball;
-(void)takePie;
@end
和
//
// Player.m
// OrcAndPie
//
// Created by me on 4/27/13.
// Copyright (c) 2013 me. All rights reserved.
//
#import "Player.h"
@implementation Player
@synthesize maxHealth;
@synthesize currentHealth;
@synthesize armorClass;
@synthesize meleeToHitModifier;
-(void)setHealthBar {
NSLog(@"Player health is 15");
return;
}
-(void)attackOrc {
// roll a d20
// add the "to hit" modifier
// compare the result to the orc's armor class
NSLog(@"Player - Player attached the Orc");
return;
}
-(void)castFireball{
NSLog(@"Player - Player casts Fireball");
return;
}
-(void)takePie{
NSLog(@"Player - Player attempts to take pie");
return;
}
@end
注意:我还定义了一个 Orc 类,但它看起来与显示的播放器类完全相同。
在此先感谢您的帮助!
——埃迪