How to get b2Body of current CCSprite - cocos2d-iphone

In my code i've always used spriteA = (__bridge CCSprite *) bodyA->GetUserData(); //where spriteA is a CCSprite and bodyA is a b2Body. I use it to get whatever sprite is linked to bodyA. My problem is, how do I do this the other way around? I have a sprite and I want to find out what b2Body is linked to it. How do I do this?
Edit
I don't know wether I set it up right or not, but I'm trying to remove all b2bodies (and sprites) in an array called row4 once there are no more blue objects (objects in row4BlueArray)
Here is part of the code in my tick method:
//Find the sprite for the b2Bodies
else if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
spriteA = (__bridge CCSprite *) bodyA->GetUserData();
spriteB = (__bridge CCSprite *) bodyB->GetUserData();
contactPositionX = spriteA.position.x;
contactPositionY = spriteB.position.y;
//If sprite is a member of row 4 (tag 400)
if (spriteA.tag == 400 && spriteB.tag == 8)
{
[self createExplosionBlue];
[self addTileScore];
[self removeChild:spriteA cleanup:YES];
[self removeChild:spriteB cleanup:YES];
NSLog(#"row 4 count: %d",row4BlueArray.count);
//Remove object from another array
[row4BlueArray removeLastObject];
toDestroy.insert(bodyA);
toDestroy.insert(bodyB);
[self unschedule:#selector(tick:)];
ballCount = 0;
//if that array is empty, then remove all objects from this array (row4)
if (row4BlueArray.count == 0) {
for (b2Body * b = _world->GetBodyList(); b != NULL; b = b->GetNext()) {
Box2DSprite * sprite = (__bridge Box2DSprite*) b->GetUserData();
b2Body * spriteBody = sprite.body;
//not sure how to remove all bodies in an array (row4)`
}
}
}

One simple way to do this is to cache the body as a property of a class.
You could extend the CCSprite class with a class called Box2DSprite. In Box2DSprite, you include a property for the b2Body * you used to create the physics for your sprite. You can also store a reference to the world if you think you may need it.
#interface Box2DSprite : CCSprite {
b2Body * body;
b2World * world;
} // end ivars
#property (readwrite) b2Body * body;
#property (readwrite) b2World * world;
Also remember to rename your .m file to a .mm file
Edit
In your game loop, to obtain the Box2DSprite you simply need to cast it back to a Box2DSprite.
for (b2Body * b = world->GetBodyList(); b != NULL; b = b->GetNext()) {
Box2DSprite * sprite = (__bridge Box2DSprite*) b->GetUserData();
b2Body * spriteBody = sprite.body;
// Do stuff with body here
}
You just need to ensure that whenever you create a sprite with a physics body, you are creating a Box2DSprite.
Hope this helps

Related

Advanced Spritesheeting - using a spritesheet in multiple classes

So after taking a little break after being very frustrated for months on end with this issue, I am now back and attempting to solve my issue once and for all.
I’m not going to post code, because the code I have is messy and patchy and is from countless points of view.
My issue is this:
I have a sprite, and his information is stored in a class called player.m. I have a game level, and it is stored in GameLevelLayer.m
I have player.m store velocity, direction, and all the information about the character, and then I have the GameLevelLayer.m implement, move, use bounds to check collisions, etc.
I now want to make the character move using a spritesheet (I know how to do spritesheeting, I just don’t know how to do it while working with two different classes)
My question is, do I create the batchnodes and spritesheet information (CCBatchnodes, caches, etc) and all of the actions and everything in player.m, and then run them in GameLevelLayer.m? Or do I create all that in GameLevelLayer.m
I really need some help here because I’ve been stuck on this for months
EDIT:
Here is where I am, thanks to a suggestion from a community member.
This is my entire player.m (Long story short, I define everything about the player 'you might just want to pay attention to the init, because that seems to be the problem. The other stuff is my physics engine')
#import "Player.h"
#import "SimpleAudioEngine.h"
#import "GameLevelLayer.h"
#implementation Player
#synthesize velocity = _velocity;
#synthesize desiredPosition = _desiredPosition;
#synthesize onGround = _onGround;
#synthesize forwardMarch = _forwardMarch, mightAsWellJump = _mightAsWellJump, isGoingLeft = _isGoingLeft;
#synthesize WalkAction = _WalkAction;
#synthesize isMoving = _isMoving;
-(id)initWithTexture:(CCTexture2D *)texture{
if (self = [super init]) {
self.velocity = ccp(0.0, 0.0);
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: #"BankerSpriteSheet_default.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"BankerSpriteSheet_default.png"];
[self addChild:spriteSheet];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <=6; ++i) {
[walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:#"banker%d.png", i]]];
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
self = [super initWithSpriteFrameName:#"banker1.png"];
self.WalkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
//[_Banker runAction:_WalkAction];
[spriteSheet addChild:self];
[[CCAnimationCache sharedAnimationCache] addAnimation:walkAnim name:#"walkAnim"];
}
}
return self;
}
-(void)update:(ccTime)dt {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults synchronize];
CGPoint jumpForce = ccp(0.0, 310.0);
float jumpCutoff = 150.0;
if (self.mightAsWellJump && self.onGround) {
self.velocity = ccpAdd(self.velocity, jumpForce);
if (![defaults boolForKey:#"All Muted"]) {
if (![defaults boolForKey:#"SFX Muted"]) {
[[SimpleAudioEngine sharedEngine] playEffect:#"jump.wav"];
}
}
} else if (!self.mightAsWellJump && self.velocity.y > jumpCutoff) {
self.velocity = ccp(self.velocity.x, jumpCutoff);
}
CGPoint gravity = ccp(0.0, -450.0);
CGPoint gravityStep = ccpMult(gravity, dt);
CGPoint forwardMove = ccp(800.0, 0.0);
CGPoint forwardStep = ccpMult(forwardMove, dt);
self.velocity = ccp(self.velocity.x * 0.90, self.velocity.y); //2
if (self.forwardMarch) {
self.velocity = ccpAdd(self.velocity, forwardStep);
if (!self.isMoving) {
//[self runAction: _WalkAction];
//self.isMoving = YES;
}
} //3
CGPoint minMovement = ccp(0.0, -450.0);
CGPoint maxMovement = ccp(120.0, 250.0);
self.velocity = ccpClamp(self.velocity, minMovement, maxMovement);
self.velocity = ccpAdd(self.velocity, gravityStep);
CGPoint stepVelocity = ccpMult(self.velocity, dt);
if (!self.isGoingLeft) {
self.desiredPosition = ccpAdd(self.position, stepVelocity);
}else{
self.desiredPosition = ccp(self.position.x-stepVelocity.x, self.position.y+stepVelocity.y);
}
}
-(CGRect)collisionBoundingBox {
CGRect collisionBox = CGRectInset(self.boundingBox, 3, 0);
//CGRect collisionBox = self.boundingBox;
//collisionBox = CGRectOffset(collisionBox, 0, -2);
CGPoint diff = ccpSub(self.desiredPosition, self.position);
CGRect returnBoundingBox = CGRectOffset(collisionBox, diff.x, diff.y);
return returnBoundingBox;
}
#end
So now the issue is, how do i get the player to appear in the game like a normal sprite, and then how do I run the animations when the player begins to move forward
Your game architecture is very good! Keep player properties on player and use GameLayer only to send player actions is the best approach.
If your Player Class extends from CCSprite, put everithing related to player in your Player Class: Run animations on it, load the cache on player constructor (use the CCSprite initWithTexture:rect:rotated: or a custom autorelease constructor, like a +(id)player ).
Then, in your GameLevelLayer you will need a CCSpriteBatchNode to add your player to it. You can use this batch node to add other objects that use same spritesheet.
EDIT:
Reviewing your code, your initWithTexture is wrong. The correct is to call [super initWithTexture:]:
-(id)initWithTexture:(CCTexture2D *)texture{
if (self = [super initWithTexture:texture]) {
Then, in your GameLevelLayer, create your player and add it to a CCSpriteBatchNode:
// *** In GameLevelLayer.m ***
// Create player
Player *player = [Player spriteWithFile:#"player.png"];
// Create batch node
CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithTexture:player.texture];
// Add batch node as child
[self addChild:batchNode];
// Add player as batch node's child
[batchNode addChild:player];
// Set player position
player.position = ccp(100,100);
Your GameLevelLayer needs to do only this to create your Player. And when it detects user input, call methods like "jump", "moveRight", "moveLeft" in your player. Also detect collisions and send actions to player like "die", "getSpecialItem".
// *** In GameLevelLayer.m ***
[player jump];
[player die];
So, your player needs to handle these methods and performs its own logic to do the actions:
// *** In Player.m ***
- (void)moveRight
{
self.position = ccpAdd(self.position, ccp(10,0));
}

tracking a 'body's' position after another body joint to it has been destroyed

I have created a revolute joint between two bodies. When a another body gets into contact (see Contacter.mm below) with one of them (body with tag ==90), I destroy the body with tag == 90. I put a CCLOG to track the sprite position of the revolute joint body that was not destroyed. The CCLOG works fine till it's joint body is destroyed. Then the CCLOG stops printing to the console. How can I solve this? Below is the relevant code.
Contacter.mm:
#import "Contacter.h"
#import "Box2D.h"
#implementation Contacter
#synthesize arrayOfBodies = _arrayOfBodies;
#synthesize spriteToDestroy = _spriteToDestroy;
-(void)destroyBodies:(b2Body*)body {
_arrayOfBodies = [[NSMutableArray alloc] init];
NSValue *bodyValue = [NSValue valueWithPointer:body];
[_arrayOfBodies addObject:bodyValue];
}
-(void)physicsSpritesContact:(CCPhysicsSprite*)onePhysicsSprite otherSprite (CCPhysicsSprite*)twoPhysicsSprite; {
int firstTag = onePhysicsSprite.tag;
int secondTag = twoPhysicsSprite.tag;
if (((firstTag == 90) && (secondTag == 101 )) || ((firstTag == 101) && (secondTag == 90))) {
if (tag1 == 90) {
[self destroyBodies:onePhysicsSprite.b2Body];// adds body to array to be destroyed
spriteToDestroy = onePhysicsSprite; // taking note of sprite to be destroyed
}
else if (tag2 == 90) {
[self destroyBodies:twoPhysicsSprite.b2Body];
spriteToDestroy = twoPhysicsSprite;
}
}
The following method within HelloWorldLayer.mm is called in the update method to destroy the body and sprite with (tag == 90):
-(void)removeDestroyedBodiesAndSprites {
if ([bodyContact arrayOfBodies]) {
for (NSValue* bodyValue in [bodyContact arrayOfBodies]) {
b2Body *removeBody;
removeBody = (b2Body*)[bodyValue pointerValue];
world->DestroyBody(removeBody);
removeBody = NULL;
[self removeChild:[bodyContact spriteToDestroy]];
}
}
CCLOG(#"y value for Sam sprite %f",[Sam samPhysicsSprite].position.y); // here is the CCLOG
}

Cocos2d Box2d - assign generic userData to bodyDef

Most examples I see of assigning userData go something like this:
CCSprite *sprite = [CCSprite spriteWithFile:#"whatever.png" rect:CGRectMake(0, 0, screenSize.width, screenSize.height)];
sprite.tag = kWallTag;
[self addChild:sprite];
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
groundBodyDef.userData = (__bridge void*)sprite;
That's fine if you're using a sprite. But in my case, I don't want to create a sprite because I just want to test collisions with the screen edges. I could create a sprite the size of the screen with just a border but I don't want to use that much texture memory just for detecting walls. So my question is how to assign the kWallTag to the groundBodyDef, without assigning it a sprite. And how would I retrieve the tag value?
I've answered the first part:
GenericUserData *usrData = (GenericUserData*)malloc(sizeof(GenericUserData));
usrData->tag = kWallTag;
groundBodyDef.userData = usrData;
But I don't know how to test for the generic data:
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
CCSprite *spriteA = (__bridge CCSprite *) bodyA->GetUserData();
CCSprite *spriteB = (__bridge CCSprite *) bodyB->GetUserData();
How do I test for generic user data instead of just assuming that it's a CCSprite?

How to detect all the visible sprites in a layer?

Can I detect all the visible sprites in a layer in order to use them.
Here i assume u r using Box2d with cocos2d
//here create ccsprite and add them to ur scene
while create body with bodydef
bodydef.userData=ccspriteobject;
for (b2Body* b = m_world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
//if the userdata is not null then ur sprite
CCSprite *actor = (CCSprite*)b->GetUserData();
Here is a small function you can use:
-(void)getAllChildren {
CCArray *childrenArray = [self children];
for(CCNode *myNode in childrenArray)
{
}
}

Box2d + CoCos2d: Moving an Object automatically to simulate computer movement in a game

I am working on a hockey game and I am implementing Single Player mode. I am trying to move the "computer's" paddle in offense mode (move towards the ball). I am using CoCos2d and Box2d. I am moving the paddle using MouseJoints. The problem is the Paddle doesnt move at all!
tick is called in init method
[self schedule:#selector(tick:)];
...
- (void)tick:(ccTime) dt
{
_world->Step(dt, 10, 10);
CCSprite *computer_paddle;
CCSprite *ball;
b2Body *computer_paddle_b2Body;
float32 direction;
b2Vec2 velocity;
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *)b->GetUserData();
if (sprite.tag == 1) { //ball
ball = sprite;
static int maxSpeed = 10;
velocity = b->GetLinearVelocity();
float32 speed = velocity.Length();
direction = velocity.y;
if (speed > maxSpeed) {
b->SetLinearDamping(0.5);
} else if (speed < maxSpeed) {
b->SetLinearDamping(0.0);
}
}
if (sprite.tag == 3){ // paddle
computer_paddle = sprite;
computer_paddle_b2Body = b;
}
// update sprite position
sprite.position = ccp(b->GetPosition().x * PTM_RATIO,b->GetPosition().y * PTM_RATIO);
sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
// update the computer paddle in single player moving it towards the ball using MouseJoint
//move towards the ball
b2Vec2 b2Position = b2Vec2(ball.position.x/PTM_RATIO,ball.position.y/PTM_RATIO);
b2MouseJointDef md1;
md1.bodyA = _groundBody;
md1.bodyB = computer_paddle_b2Body;
md1.target = b2Position;
md1.collideConnected = true;
md1.maxForce = 9999.0 * computer_paddle_b2Body->GetMass();
_mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md1);
computer_paddle_b2Body->SetAwake(true);
Check whether:
a) the body is sleeping
b) the body is a static body
If it is sleeping and you have no other bodies, disable sleeping entirely. Otherwise disable sleeping of the body: body->SetSleepingAllowed(NO)
Note: this is according to Box2D 2.2.1 API Reference which isn't the default in cocos2d 1.0 yet, so the function may be different in your Box2D version.
Also check that your body is dynamic by looking up the b2BodyDef's type member and, if necessary, set it to be dynamic (see the b2BodyType enumeration). I'm not sure what the default is, it should be dynamic but this may depend on the Box2D version.