Yes, the player :-)
The player is on the same layer that the platforms are on and hence the body definitions. The layer is scrolled horizontally and vertically thus I need to use SetTransform to keep the player body in the center of the screen.
I can't understand why this seems so difficult? If I continue to hold down the button that moves the player right for instance(ccTouchBegan), he will walk through the box's on the floor, but, if I keep 'clicking/touching' the button so to speak, he will stop when hit a box (I set a flag when player hits a box and do a check in the scrolling code if this flag is set, it doesn't scroll) - it is as though Box2D isn't stepping...
I'm kind of coming to the conclusion I'm doing it all the wrong way, but don't know any alternative way. I'm convinced if the screen wasn't scrolling and I moved the sprite around, collisions would work fine.
This is some code I also use to detect which bodies have collided, may give some more clues??
std::vector<b2Body *>toDestroy;
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos)
{
MyContact contact = *pos;
// See if player is on the ground (_platformFixture is actually the floor, lol )
if ((contact.fixtureA == _platformFixture && contact.fixtureB == _playerFixture) ||
(contact.fixtureA == _playerFixture && contact.fixtureB == _platformFixture))
{
bOnGround = true;
_bOnPlatform = false;
}
else // must be on a platform as no other bodies in the world yet
{
_bOnPlatform = true;
bOnGround = false;
}
} // end for
This is the code I use to update the player sprite and body:
for(b2Body *b = world->GetBodyList(); b; b=b->GetNext())
{
if(b->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite*)b->GetUserData();
switch(sprite.tag)
{
case kPlayerTag: // Player
{
CGPoint screenPos = [self position];
CGPoint temp = ccp(240 - screenPos.x, 160 - screenPos.y);
sprite.position = ccp(temp.x,temp.y);//* PTM_RATIO, temp.y * PTM_RATIO);
b2Vec2 b2Position = b2Vec2(temp.x/PTM_RATIO,
(/*-25+*/temp.y)/PTM_RATIO);
b->SetTransform(b2Position, 0); // Need to move body so always in centre of screen on x-coordinate
// NSLog(@"Body pos x:%f y:%f",b2Position.x*32, b2Position.y*32);
}
break;
}
}
else {
}
}
and this to do the scrolling:
CGPoint screenposition = [self position];
if(Dir == Up)
{
if(_topJump <= _toJumpToLoc)
{
_topJump += 4;
screenposition.y -= 4;
}
else { // jumped done
Dir = Down;
}
}
else {
if(Dir == Down)
{
if(!bOnGround && !_bOnPlatform)
{
screenposition.y += 4;
}
}
}
CGPoint backLayerPos = [_backGroundLayer position];
backLayerPos.x += xVel / 6;
CGPoint newbackLayerPos = ccp(backLayerPos.x, backLayerPos.y);
[_backGroundLayer setPosition:newbackLayerPos];
screenposition.x += xVel;
[self setPosition:screenposition];
Regards,
Steve