iPhone кодирование - ошибка компилятора - беспризорный / 342, беспризорный / 200, старый / 223 ... почему? - PullRequest
1 голос
/ 19 декабря 2009

Я пытаюсь реализовать этот код для использования акселерометра, но получаю ошибку компилятора: stray / 342, stray / 200 ... для размера, currentTouch, позиции объекта и т. д. ... почему?

code:

//GravityObj.h

@interface GravityObj : UIView  {
        CGPoint position;
        CGSize size;
    CGPoint velocity;
        NSTimer *objTimer;
        NSString *pngName;
        CGFloat bounce;
    CGFloat gravity;
        CGPoint acceleratedGravity;
        CGPoint lastTouch;
    CGPoint currentTouch;
        BOOL dragging;
}

@property CGPoint position;
@property CGSize size;
@property CGPoint velocity;
@property(nonatomic,retain)NSString *pngName;
@property(nonatomic,retain)NSTimer *objTimer;
@property CGFloat bounce;
@property CGFloat gravity;
@property CGPoint acceleratedGravity;
@property CGPoint lastTouch;
@property CGPoint currentTouch;
@property BOOL dragging;

- (id)initWithPNG:(NSString*)imageName position:(CGPoint)p size:(CGSize)s;
- (void)update;
- (void)onTimer;

@end

//GravityObj.m

#import "GravityObj.h"


@implementation GravityObj

@synthesize position, size;
@synthesize objTimer;
@synthesize velocity;
@synthesize pngName;
@synthesize bounce;
@synthesize gravity, acceleratedGravity;
@synthesize lastTouch, currentTouch;
@synthesize dragging;

- (id)initWithPNG:(NSString*)imageName position:(CGPoint)p size:(CGSize)s {
        if (self = [super initWithFrame:CGRectMake(p.x, p.y, s.width, s.height)]) {
                [self setPngName:imageName];
                [self setPosition:p];
                [self setSize:s];
                [self setBackgroundColor:[UIColor clearColor]];

                // Set default gravity and bounce
                [self setBounce:-0.9f];
        [self setGravity:0.5f];
                [self setAcceleratedGravity:CGPointMake(0.0, gravity)];
                [self setDragging:NO];

                UIImageView *prezzie = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, s.width, s.height)];
                prezzie.image = [UIImage imageNamed:imageName];

                [self addSubview:prezzie];
                [prezzie release];

                [[UIAccelerometer sharedAccelerometer] setDelegate:self];
        }
        return self;
}

- (void)update {
        [self setNeedsDisplay];

        if(dragging) return;

        velocity.x += acceleratedGravity.x;
    velocity.y += acceleratedGravity.y;
    position.x += velocity.x;
    position.y += velocity.y;

    if(position.x + size.width >= 320.0) {
        position.x = 320.0 – size.width;
        velocity.x *= bounce;
    }
    else if(position.x <= 0.0) {
        velocity.x *= bounce;
    }

    if(position.y + size.height >= 416.0) {
        position.y = 416.0 – size.height;
        velocity.y *= bounce;
    }
    else if(position.y <= 0.0) {
        velocity.y *= bounce;
    }
        self.frame = CGRectMake(position.x, position.y, size.width, size.height);
}

- (void)onTimer {
        [self update];
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
}
/* EVENTS */

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    acceleratedGravity.x = acceleration.x * gravity;
    acceleratedGravity.y = -acceleration.y * gravity;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        // First, lets check to make sure the timer has been initiated
        if (objTimer == nil) {
                objTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
        }

    UITouch *touch = [touches anyObject];
        [self setCurrentTouch:[touch locationInView:self]];
    CGFloat dx = currentTouch.x – position.x;
    CGFloat dy = currentTouch.y – position.y;
    CGFloat dist = sqrt(dx * dx + dy * dy);
    if(dist < size.width) {
        [self setVelocity:CGPointMake(0.0, 0.0)];
                [self setDragging:YES];
    }
        [self setLastTouch:currentTouch];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    [self setCurrentTouch:[touch locationInView:self]];
        [self setDragging:YES];
        [self setVelocity:CGPointMake(currentTouch.x – lastTouch.x, currentTouch.y – lastTouch.y)];
        [self setLastTouch:currentTouch];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        [self setDragging:NO];
}

- (void)dealloc {
        [objTimer release], objTimer = nil;
        [pngName release], pngName = nil;
        [super dealloc];
}

@end

Ответы [ 3 ]

4 голосов
/ 19 декабря 2009

Похоже, что знаки минуса в некоторых ваших строках были символами Юникода, которые отображаются как минусы и не отображаются.Я скопировал ваш код в Xcode и получил ту же ошибку.Затем я перепечатал каждую строку, в которой была ошибка, и она работала.

Вот исправленная версия (только файл .m):

@implementation GravityObj

@synthesize position, size;
@synthesize objTimer;
@synthesize velocity;
@synthesize pngName;
@synthesize bounce;
@synthesize gravity, acceleratedGravity;
@synthesize lastTouch, currentTouch;
@synthesize dragging;

- (id)initWithPNG:(NSString*)imageName position:(CGPoint)p size:(CGSize)s {
    if (self = [super initWithFrame:CGRectMake(p.x, p.y, s.width, s.height)]) {
        [self setPngName:imageName];
        [self setPosition:p];
        [self setSize:s];
        [self setBackgroundColor:[UIColor clearColor]];

        // Set default gravity and bounce
        [self setBounce:-0.9f];
        [self setGravity:0.5f];
        [self setAcceleratedGravity:CGPointMake(0.0, gravity)];
        [self setDragging:NO];

        UIImageView *prezzie = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, s.width, s.height)];
        prezzie.image = [UIImage imageNamed:imageName];

        [self addSubview:prezzie];
        [prezzie release];

        [[UIAccelerometer sharedAccelerometer] setDelegate:self];
    }
    return self;
}

- (void)update {
    [self setNeedsDisplay];

    if(dragging) return;

    velocity.x += acceleratedGravity.x;
    velocity.y += acceleratedGravity.y;
    position.x += velocity.x;
    position.y += velocity.y;

    if(position.x + size.width >= 320.0) {
        position.x = 320.0 - size.width;
        velocity.x *= bounce;
    }
    else if(position.x <= 0.0) {
        velocity.x *= bounce;
    }

    if(position.y + size.height >= 416.0) {
        position.y = 416.0 - size.height;
        velocity.y *= bounce;
    }
    else if(position.y <= 0.0) {
        velocity.y *= bounce;
    }
    self.frame = CGRectMake(position.x, position.y, size.width, size.height);
}

- (void)onTimer {
    [self update];
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
}
/* EVENTS */

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    acceleratedGravity.x = acceleration.x * gravity;
    acceleratedGravity.y = -acceleration.y * gravity;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // First, lets check to make sure the timer has been initiated
    if (objTimer == nil) {
        objTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    }

    UITouch *touch = [touches anyObject];
    [self setCurrentTouch:[touch locationInView:self]];
    CGFloat dx = currentTouch.x - position.x;
    CGFloat dy = currentTouch.y - position.y;
    CGFloat dist = sqrt(dx * dx + dy * dy);
    if(dist < size.width) {
        [self setVelocity:CGPointMake(0.0, 0.0)];
        [self setDragging:YES];
    }
    [self setLastTouch:currentTouch];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    [self setCurrentTouch:[touch locationInView:self]];
    [self setDragging:YES];
    [self setVelocity:CGPointMake(currentTouch.x - lastTouch.x, currentTouch.y - lastTouch.y)];
    [self setLastTouch:currentTouch];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self setDragging:NO];
}

- (void)dealloc {
    [objTimer release], objTimer = nil;
    [pngName release], pngName = nil;
    [super dealloc];
}

@end

Если вы снова получите эту ошибку, я обнаружил, что просмотрфайл в FileMerge может помочь;похоже, он не знает Unicode так же хорошо, как Xcode.

0 голосов
/ 22 сентября 2011

да, я также сталкивался с такими типами ошибок, которые можно устранить, просто удалив некоторые ненужные отформатированные строки, такие как серая сфокусированная линия, которые копируются с содержимым кода, который вы скопировали с других сайтов, веб-страниц или из файла PDF. Это определенно удалит такие типы ошибок, удалив лишнюю отформатированную строку или просто скопировав эту часть кода в блокнот, а оригинальную копию текста обратно в ваш код ... все готово. !!!

0 голосов
/ 19 декабря 2009

Вы можете выбрать паразит / 342/200 из копирования / вставки, как правило, из PDF-файла, иногда HTML. Вставьте в терминал и скопируйте обратно для быстрого исправления.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...