Should the first challenge just show 3 colors


#1

I just finished the first challenge and just want to know if the intention is to show 3 different colors basically.

When the ipad is flat it shows black, when its to one side red, and upright blue - was that sort of what we were intended to do:

[code]

  • (void)accelerometer:(UIAccelerometer *)meter didAccelerate:(UIAcceleration *)accel
    {
    NSLog(@"%f %f %f", [accel x], [accel y], [accel z]);

    HypnosisView *hv = (HypnosisView *)[self view];
    float xShift = [hv xShift] * 0.8 + [accel x] * 2.0;
    float yShift = [hv yShift] * 0.8 - [accel y] * 2.0;

    [hv setXShift:xShift];
    [hv setYShift:yShift];

    [hv setColorWithRed:[self getChannelValue:[accel x]] green:[self getChannelValue:[accel y]] blue:[self getChannelValue:[accel z]]];

    //Redraw the view
    [hv setNeedsDisplay];
    }

  • (float)getChannelValue:(double)axisValue
    {
    if (axisValue >= 1)
    {
    return 1.0;
    }
    else
    {
    return 0.0;
    }
    }[/code]


#2

That is one way of doing it, but I had something else in mind. Something along the lines of

[UIColor colorWithRed:fabs([accel x]) green:fabs([accel y]) blue:fabs([accel z])];

Although, the purpose of the challenge was to figure out how to construct a color object based on input from the accelerometer and somehow get it to the view and have the view display differently - so you’ve certainly completed the challenge.

Also, quick note on naming conventions for methods. A method with the word “Get” in it has some what of a special meaning among ObjC developers. A “get” method should be passed a pointer to one or more variables and information is passed back thru those pointers. For example, on the Mac, there is a method on NSColor called getRed:green:blue:alpha: that is declared as so:

- (void)getRed:(CGFloat *)r green:(CGFloat *)g blue:(CGFloat *)b alpha:(CGFloat *)a;

We use methods like that to “return” more than one value from a method.

For methods that perform a calculation given one or more input values, you will see ObjC programmers use the word “For”. So you could replace getChannelValue: with something like:

- (float)channelValueForInput:(double)av;

Or if you really wanted to lock down the name of the method:

- (float)colorChannelValueForAccelerometerInput:(double)av;