Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有一个精灵,我想在上面绑定一些 var。从文档中我了解了一些东西,使用函数“setUserData”和“getUserData”。但是有一点不对。
float nums = 2.3f; pSprite->setUserData((void*)nums); // wrong?? // how to get the var from pSprite. float some_float = *(pSprite->getUserData());??
用户数据存储一个指针,因此您需要动态分配内存。尝试这样的事情:
float* nums = new float(2.3f); pSprite->setUserData((void*)nums); ... float* data = (float*)pSprite->getUserData(); printf("%f", *data); delete data;
通常,您创建一个结构,其中包含要存储在 sprite 中的所有数据。如果您只想存储一个数字,这将起作用。在任何一种情况下,请确保删除内存!