Chris
Chris

Reputation: 27384

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. 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.

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 ints and seems to accept a class only which seems to add unnecessary complications.

What is the simplest way to achieve what I want?

Upvotes: 0

Views: 391

Answers (1)

brynbodayle
brynbodayle

Reputation: 6626

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. The object is wrapped in a NSNumber.

@(yourInt) is equivalent with saying [NSNumber numberWithInt:yourInt]

Upvotes: 3

Related Questions