Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 516057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:41:33+00:00 2026-05-13T07:41:33+00:00

I’m trying to implement this code for accelerometer use, but I’m getting a compiler

  • 0

I’m trying to implement this code for accelerometer use, but I’m getting a compiler error:

stray /342, stray/200, stray/223 … for size,currentTouch, position object’s and so on …

Why?

Code

File 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

File 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, let's 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:41:33+00:00Added an answer on May 13, 2026 at 7:41 am

    It looks like the minus signs in some of your lines were Unicode characters that display as minuses and aren’t. I copied your code into Xcode and got the same error. I then retyped each line that had the error and it worked.

    Here’s the fixed version (.m file only):

    @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
    

    If you get that error again, I found that viewing the file in FileMerge can help; it doesn’t seem to know Unicode as well as Xcode.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 291k
  • Answers 291k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer use return false; at the end of the function that… May 13, 2026 at 5:56 pm
  • Editorial Team
    Editorial Team added an answer Put an invisible button over the text area by making… May 13, 2026 at 5:56 pm
  • Editorial Team
    Editorial Team added an answer You could try something like (Full example) DECLARE @Categories TABLE(… May 13, 2026 at 5:56 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.