Game Development Reference
pos.x + = acceleration.x * 10;
player.position = pos;
Notice something odd? I wrote three lines where one might seem to suffice:
// ERROR: lvalue required as left operand of assignment
= acceleration.x * 10;
Unlike other programming languages such as Java, C++, and C#, writing something
like player.position.x + = value won't work with Objective-C properties.
The position property is a CGPoint , which is a regular C struct data type.
Objective-C properties simply can't assign a value to a field in a struct directly. The
problem lies in how properties work in Objective-C and also how assignment works in
the C language, on which Objective-C is based.
The statement player.position.x is actually a call to the position getter method
[player position] , which means you're actually retrieving a temporary position
and then trying to change the x member of the temporary CGPoint . But the temporary
CGPoint would then get thrown away. The position setter [player setPosi-
tion] simply will not be called automagically. You can only assign to the play-
er.position property directly—in this case, a new CGPoint . In Objective-C you
have to live with this unfortunate issue—and possibly change programming habits if
you come from a Java, C++, or C# background.
This is why the previous code has to create a temporary CGPoint object, change the
point's x field, and then assign the temporary CGPoint to player.position . Un-
fortunately, this is how you have to do it in Objective-C.
First Test Run
Your project should now be at the same level as the one in the DoodleDrop02 folder of
the code provided with this chapter. Give it a try now. Make sure you choose to run the
app on the device, because you won't get accelerometer input from the simulator. Test
how the accelerometer input behaves in this version.