简体   繁体   English

box2d中的SetUserData

[英]SetUserData in box2d

I am writing a game in box2d / cocos2d where things fall from the sky, when they touch the floor or the player they should be removed from the scene. 我正在用box2d / cocos2d编写游戏,其中物体从天上掉下来,当它们碰到地板或玩家时,应该将它们从场景中移开。 I have implemented a ContactListener as per Ray Wenderlich's tutorial but the issue with it is that anytime ANYTHING touches anything it gets removed. 我已经按照Ray Wenderlich的教程实现了一个ContactListener ,但是问题是任何时候只要碰到任何东西,它都会被删除。

What I need is for some way to know which are objects that can be removed. 我需要以某种方式知道哪些是可以删除的对象。 I see there is a SetUserData function in Box2d where I can set data but it will not accept int s and seems to accept a class only which seems to add unnecessary complications. 我看到SetUserData有一个SetUserData函数,我可以在其中设置数据,但它不接受int ,似乎只接受一个类,这似乎增加了不必要的复杂性。

What is the simplest way to achieve what I want? 实现我想要的最简单的方法是什么?

Try this out: 试试看:

int yourInt = 2012;
body->SetUserData(@(yourInt)); 

It is using the new Objective-C literal syntax for wrapping the primitive type int in an object. 它使用新的Objective-C文字语法将原始类型int包装在对象中。 The object is wrapped in a NSNumber . 该对象包装在NSNumber

@(yourInt) is equivalent with saying [NSNumber numberWithInt:yourInt] @(yourInt)等同于说[NSNumber numberWithInt:yourInt]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM