When I start to write Apps for iOS, I found a little different difference between Objective-C and Other OO language.
e.g.
-(void) accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration
{
player.position.x += acceleration.x * 10;
player.position = pos;
}
If you try to build the code above, it will get error(Error: lvalue required as left operand of assignment).We write someting like player.position.x += value won't work with Objective-C properties, The problom lies how properties work in Objective-C and also how assignment works in the C language, on which Objective-C based.
The statement player.position.x is actually a calll 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 setPosition] simply will not be called automagically. You can only assign to the player.position property directly, in the case a new CGPoint.
In Objective-C you’ll have to live with this unfortunate issue—and possibly change programming habits if you come from a Java, C++ or C#background.
So we should try the code like this:
-(void) accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration
{
CGPoint pos = player.position;
pos.x += acceleration.x * 10;
player.position = pos;
}
[Notes from" Learn iPhone and iPad Cocos2D Game Development"
分享到:
相关推荐
The Objective-C language defers as many decisions as it can from compile time and link time to runtime. Whenever possible, it does things dynamically. This means that the language requires not just a ...
亲自书签精校,方便阅读。douban 8.2高分教程,设计模式方面的好书
> Curated collection of useful little Java 8 functions that you can understand quickly. Table of Contents Array View contents chunk countOccurrences deepFlatten difference differenceWith ...
Java is a new object-oriented programming language that was developed by Sun Microsystems for programming the Internet and intelligent appliances. In a very short time it has become one of the most ...
JavaTM and C# programming languages. In this paper, we evaluate these extensions with respect to the realm of scientific computing and compare them with C++ templates. At the heart of our comparison ...
A Little Java A Few Patterns
python库,解压后可用。 资源全名:littlefs_python-0.1.2-cp35-cp35m-win_amd64.whl
LittlevGL_Demo-master
LittleVGL移植工程 -- 三个测试程序
Test-Driven Development (TDD) is a methodology that helps you to write as little as code as possible to satisfy software requirements, and ensures that what you've written does what it's supposed to ...
little-bear-dictionary-master,在网上收集到的,发出来分享给大家.
资源来自pypi官网。 资源全名:littlefs_python-0.3.0-cp36-cp36m-win32.whl
C# has its roots in the C family of languages and will be immediately familiar to C, C++, and Java programmers. C# is standardized by ECMA International as the ECMA-334 standard and by ISO/IEC as the...
30 seconds of java8 > 你可以在30秒或更短时间内收集有用的Java8代码片段。 使用 Ctrl F 或者 command F 来查找代码片段。...代码片段基于 Java8,如果你还不熟悉...代码片段翻译自 little-java-functions 目录
LittlevGL_Application-master VS2019 编译
MyBatis couples objects with stored procedures or SQL statements using a XML descriptor. Simplicity is the biggest advantage of the MyBatis data mapper over object relational mapping tools. To use ...
前言作为一个三维地球,在场景中来点雨雪效果,貌似可以增加一点真实感。Cesium 官网 Demo 中有天气系统的实例,用的是 Cesium 中的粒子系统做的。效
LittlevGL_Application-master 综合应用