Hey all, I'll explain my situation and post some relevant code which will lead to my question.
I am working on a space shooter iphone game, think Galaga to get a mental picture. I am using Cocos2d and Box2D (mostly just for collision detection). What I want is to have enemies that are a little more complex then just a single sprite that will be destroyed on contact. What I want is a sprite that has other sprites as destroyable parts. My plan was to have a parent sprite as the "body" of the enemy ship and add "ship components" as children.
The following is my init ship method:
In my GameScene I create my ship and add it to the layer (I know using sprite sheets is the better way to do things but I am kind of just in proof of concept stage trying to get the enemies to work)
The last bit of code is the addBoxBodyForAllSprites method:
Basically, the idea of the above method is to loop through the all the children sprites (ship components) of the parent sprite (the "body" of the ship).
The problem I am having is with the line spriteBodyDef.userData = newSprite;
If I have that line commented out, the bounding boxes will be in the correct position (around the sprite component) but I will not be able to get any of the information about the sprite when I later execute the line
This makes sense since I never set it.
If I leave the line and have it get executed, the position of the bounding boxes is incorrect. It will be positioned at the position of the child sprite. For example, the position of the first component is at (-2,-13) and you can see the bounding box just barely visible in the bottom left hand corner (I have debug code enabled that will display the bounding box in pink).
I tried changing the position of the sprite to the world coordinates, setting the user data, and then changing the position of the sprite back to its original position but that did not work. I ended up with the same results. My understanding of userData is that it is just a pointer so I guess that makes sense.
So my question is how can I set the the position of a bounding box at the position of a sprite child and be able to set the user data? Is what I am looking for possible or do I / should I go about this problem a different way? I will appreciated any and all suggestions. If I was unclear or anyone needs more information let me know and I will try to clarify or elaborate.
I am working on a space shooter iphone game, think Galaga to get a mental picture. I am using Cocos2d and Box2D (mostly just for collision detection). What I want is to have enemies that are a little more complex then just a single sprite that will be destroyed on contact. What I want is a sprite that has other sprites as destroyable parts. My plan was to have a parent sprite as the "body" of the enemy ship and add "ship components" as children.
The following is my init ship method:
Code:
-(id) initWithType:(ShipTypes)shipType
{
type = shipType;
NSString *frameName;
// location of component based on ship type, number of components
CGPoint locations[3][3] = { {ccp(-2,-13), ccp(36, -13), ccp(64, 0)},
{ccp(0,0), ccp(32, 0), ccp(64, 0)},
{ccp(0,0), ccp(32, 0), ccp(64, 0)}
};
switch (type)
{
case ShipTypeStandard:
frameName = @"bodyportion.png";
numberOfComponents = [self randomComponentNumber];
break;
case ShipTypeOtherKind:
frameName = @"otherKindShipBody.png";
default:
break;
}
//if ((self = [super initWithSpriteFrameName:frameName]))
if ((self = [super initWithFile:frameName]))
{
for (int i = 1; i <= numberOfComponents; i++)
{
ShipComponent *shipComponent = [ShipComponent shipComponentInit];
shipComponent.position = locations[type][i-1];
[self addChild:shipComponent];
}
return self;
}
{
type = shipType;
NSString *frameName;
// location of component based on ship type, number of components
CGPoint locations[3][3] = { {ccp(-2,-13), ccp(36, -13), ccp(64, 0)},
{ccp(0,0), ccp(32, 0), ccp(64, 0)},
{ccp(0,0), ccp(32, 0), ccp(64, 0)}
};
switch (type)
{
case ShipTypeStandard:
frameName = @"bodyportion.png";
numberOfComponents = [self randomComponentNumber];
break;
case ShipTypeOtherKind:
frameName = @"otherKindShipBody.png";
default:
break;
}
//if ((self = [super initWithSpriteFrameName:frameName]))
if ((self = [super initWithFile:frameName]))
{
for (int i = 1; i <= numberOfComponents; i++)
{
ShipComponent *shipComponent = [ShipComponent shipComponentInit];
shipComponent.position = locations[type][i-1];
[self addChild:shipComponent];
}
return self;
}
In my GameScene I create my ship and add it to the layer (I know using sprite sheets is the better way to do things but I am kind of just in proof of concept stage trying to get the enemies to work)
Code:
SpaceShip *ship = [SpaceShip shipWithType:(ShipTypes)0];
ship.position = [ship randomPosition];
[self addChild:ship];
[self addBoxBodyForAllSprites:ship];
ship.position = [ship randomPosition];
[self addChild:ship];
[self addBoxBodyForAllSprites:ship];
The last bit of code is the addBoxBodyForAllSprites method:
Code:
-(void)addBoxBodyForAllSprites:(CCSprite *)sprite
{
CGPoint temp;
CGPoint originalPosition;
for (CCSprite *newSprite in [sprite children])
{
originalPosition = newSprite.position;
temp = [sprite convertToWorldSpace:newSprite.position];
//newSprite.position = temp;
b2BodyDef spriteBodyDef;
spriteBodyDef.type = b2_dynamicBody;
spriteBodyDef.position.Set(temp.x/PTM_RATIO, temp.y/PTM_RATIO);
spriteBodyDef.userData = newSprite;
b2Body *spriteBody = _world->CreateBody(&spriteBodyDef);
b2PolygonShape spriteShape;
spriteShape.SetAsBox(newSprite.contentSize.width/PTM_RATIO/2, newSprite.contentSize.height/PTM_RATIO/2);
b2FixtureDef spriteShapeDef;
spriteShapeDef.shape = &spriteShape;
spriteShapeDef.density = 10.0;
spriteShapeDef.isSensor = true;
if (newSprite.tag == enemyComponentTag)
{
spriteShapeDef.filter.groupIndex = componentIndex;
}
spriteBody->CreateFixture(&spriteShapeDef);
}
}
{
CGPoint temp;
CGPoint originalPosition;
for (CCSprite *newSprite in [sprite children])
{
originalPosition = newSprite.position;
temp = [sprite convertToWorldSpace:newSprite.position];
//newSprite.position = temp;
b2BodyDef spriteBodyDef;
spriteBodyDef.type = b2_dynamicBody;
spriteBodyDef.position.Set(temp.x/PTM_RATIO, temp.y/PTM_RATIO);
spriteBodyDef.userData = newSprite;
b2Body *spriteBody = _world->CreateBody(&spriteBodyDef);
b2PolygonShape spriteShape;
spriteShape.SetAsBox(newSprite.contentSize.width/PTM_RATIO/2, newSprite.contentSize.height/PTM_RATIO/2);
b2FixtureDef spriteShapeDef;
spriteShapeDef.shape = &spriteShape;
spriteShapeDef.density = 10.0;
spriteShapeDef.isSensor = true;
if (newSprite.tag == enemyComponentTag)
{
spriteShapeDef.filter.groupIndex = componentIndex;
}
spriteBody->CreateFixture(&spriteShapeDef);
}
}
Basically, the idea of the above method is to loop through the all the children sprites (ship components) of the parent sprite (the "body" of the ship).
The problem I am having is with the line spriteBodyDef.userData = newSprite;
If I have that line commented out, the bounding boxes will be in the correct position (around the sprite component) but I will not be able to get any of the information about the sprite when I later execute the line
Code:
ShipComponent *spriteA = (ShipComponent *) bodyA->GetUserData();
This makes sense since I never set it.
If I leave the line and have it get executed, the position of the bounding boxes is incorrect. It will be positioned at the position of the child sprite. For example, the position of the first component is at (-2,-13) and you can see the bounding box just barely visible in the bottom left hand corner (I have debug code enabled that will display the bounding box in pink).
I tried changing the position of the sprite to the world coordinates, setting the user data, and then changing the position of the sprite back to its original position but that did not work. I ended up with the same results. My understanding of userData is that it is just a pointer so I guess that makes sense.
So my question is how can I set the the position of a bounding box at the position of a sprite child and be able to set the user data? Is what I am looking for possible or do I / should I go about this problem a different way? I will appreciated any and all suggestions. If I was unclear or anyone needs more information let me know and I will try to clarify or elaborate.