Я пытаюсь сделать игру для угадайки для iPad, и я использую NSNumber для отслеживания количества попыток, оставленных пользователем в игре, под названием numberOfTries.NSNumber находится в файле на основе представления, и я сначала инициализирую его в методе drawRect, чтобы нарисовать блоки, указывающие, сколько попыток осталось у пользователя.Я инициализирую число значением int, равным 5, и в этот момент оно работает, но когда я пытаюсь использовать его в другом методе класса, например, в том, который указывает, нет ли у пользователя попытки, он показывает, что он уже находится вноль к тому времени, когда я ввожу первое предположение.Вот код:
//Game.h
#import <UIKit/UIKit.h>
@interface Game : UIView {
NSString* equation;
NSNumber* numberOfTries;
}
@property(nonatomic, retain)NSString* equation;
@property(nonatomic, retain)NSNumber* numberOfTries;
@end
//Game.m
#import "Game.h"
@implementation Game
@synthesize equation, numberOfTries;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
-(BOOL)hasLost
{
int tryNumber = [self.numberOfTries intValue];
return tryNumber <= 0;
}
-(BOOL)guessWithEquation:(NSString*)guessEquation
{
int tryNumber = [self.numberOfTries intValue];
if ([self.equation isEqualToString:guessEquation]) {return YES;}
tryNumber--;
self.numberOfTries = [NSNumber numberWithInt:tryNumber];
return NO;
}
-(void)setEquationTo:(NSString*)theEquation {self.equation = [[NSString alloc] initWithString:theEquation];}
- (void)drawRect:(CGRect)rect {
self.numberOfTries = [[NSNumber alloc] initWithInt:5];
int halfwidth = self.bounds.size.width/2;
int halfheight = self.bounds.size.height/2;
int threeqw = self.bounds.size.width-(self.bounds.size.width/4);
int threeqh = self.bounds.size.height-(self.bounds.size.height/4);
int oneqw = self.bounds.size.width/4;
int oneqh = self.bounds.size.height/4;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 0.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
int tryNumber = [self.numberOfTries intValue];
int point = oneqw-100;
while (tryNumber > 0) {
CGContextMoveToPoint(context, point, threeqh);
CGContextAddLineToPoint(context, point+15, threeqh);
CGContextAddLineToPoint(context, point+15, threeqh+15);
CGContextAddLineToPoint(context, point, threeqh+15);
CGContextAddLineToPoint(context, point, threeqh);
tryNumber--;
point += 30;
}
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
}
- (void)dealloc {
[super dealloc];
}
@end
Когда он вызывает guessWithEquation, numberOfTries уже в 0, что действительно сбивает с толку.Что мне здесь не хватает?