views:

154

answers:

1

The code below is from the book Beginning iPhone 3 Development, chapter 15, using the accelerometer to control a ball. I want to use a label instead of the ball, and only move in the x direction. However, I donno how to revise the code to achieve this goal. Can anyone please help me? THX so much!!!!

- (id)initWithCoder:(NSCoder *)coder {

    if (self = [super initWithCoder:coder]) {
        self.image = [UIImage imageNamed:@"ball.png"];
        self.currentPoint = CGPointMake((self.bounds.size.width / 2.0f) +
                                        (image.size.width / 2.0f), 
                                        (self.bounds.size.height / 2.0f) + (image.size.height / 2.0f));

        ballXVelocity = 0.0f;
        ballYVelocity = 0.0f;
    }
    return self;
}


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
    // Drawing code
    [image drawAtPoint:currentPoint];
}


- (void)dealloc {
    [image release];
    [acceleration release];

    [super dealloc];
}

//#pragma mark -
- (CGPoint)currentPoint {

    return currentPoint;
}
- (void)setCurrentPoint:(CGPoint)newPoint {
    previousPoint = currentPoint;
    currentPoint = newPoint;

    if (currentPoint.x < 0) {
        currentPoint.x = 0;
        //ballXVelocity = 0;
        ballXVelocity = - (ballXVelocity / 2.0);
    }
    if (currentPoint.y < 0){
        currentPoint.y = 0;
        //ballYVelocity = 0;
        ballYVelocity = - (ballYVelocity / 2.0);
    } 
    if (currentPoint.x > self.bounds.size.width - image.size.width) {
        currentPoint.x = self.bounds.size.width  - image.size.width; 
         //ballXVelocity = 0;
        ballXVelocity = - (ballXVelocity / 2.0);
    }
    if (currentPoint.y > self.bounds.size.height - image.size.height) {
        currentPoint.y = self.bounds.size.height - image.size.height;
        //ballYVelocity = 0;
        ballYVelocity = - (ballYVelocity / 2.0);
    }

    CGRect currentImageRect = CGRectMake(currentPoint.x, currentPoint.y, 
                                         currentPoint.x + image.size.width, 
                                         currentPoint.y + image.size.height);
    CGRect previousImageRect = CGRectMake(previousPoint.x, previousPoint.y,
                                          previousPoint.x + image.size.width,  
                                          currentPoint.y + image.size.width);
    [self setNeedsDisplayInRect:CGRectUnion(currentImageRect,
                                            previousImageRect)];

}

- (void)draw {
    static NSDate *lastDrawTime;

    if (lastDrawTime != nil) {
        NSTimeInterval secondsSinceLastDraw =
        -([lastDrawTime timeIntervalSinceNow]);

        ballYVelocity = ballYVelocity + -(acceleration.y *
                                          secondsSinceLastDraw);
        ballXVelocity = ballXVelocity + acceleration.x *
        secondsSinceLastDraw;

        CGFloat xAcceleration = secondsSinceLastDraw * ballXVelocity * 500;
        CGFloat yAcceleration = secondsSinceLastDraw * ballYVelocity * 500;

        self.currentPoint = CGPointMake(self.currentPoint.x + xAcceleration,
                                        self.currentPoint.y +yAcceleration);
    }
    // Update last time with current time
    [lastDrawTime release];
    lastDrawTime = [[NSDate alloc] init];

}
+1  A: 

To start, you create the label as a property of the controller. If you use IB then make it an IBOutlet and hook it up. Then in the code above, where it uses the ball image, substitute the label. Of course, you don't have to draw the label it will handle that itself.

The movement logic is exactly the same except you move the center or origin of the label instead of drawing the ball in a rect.

Edit

thank u! But I am not quite understand how to move the center of the label?

Like this:

self.myLabel.center=CGPointMake(newX,newY);

You can also animate the change so that the label appears to move smoothly. See the UIView class docs for the methods.

TechZen
thank u! But I am not quite understand how to move the center of the label?
Didi