這次要聽話,從最基本做起。
首先開始一個cocos2D + Box2D的專案。
修改邊界,製造斜度。將方塊改成circle,調整circle的摩擦係數(橘色底就是刪掉的,紅色字是更改的):
-(void) addNewSpriteWithCoords:(CGPoint)p
{
CCLOG(@"Add sprite %0.2f x %02.f",p.x,p.y);
CCSpriteBatchNode *batch = (CCSpriteBatchNode*) [self getChildByTag:kTagBatchNode];
//We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is
//just randomly picking one of the images
int idx = (CCRANDOM_0_1() > .5 ? 0:1);
int idy = (CCRANDOM_0_1() > .5 ? 0:1);
CCSprite *sprite = [CCSprite spriteWithBatchNode:batch rect:CGRectMake(32 * idx,32 * idy,32,32)];
[batch addChild:sprite];
sprite.position = ccp( p.x, p.y);
// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
bodyDef.userData = sprite;
b2Body *body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2CircleShape dynamicCircle;
dynamicCircle.m_radius = 0.5;//These are mid points for our 1m box
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicCircle;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.1f;
body->CreateFixture(&fixtureDef);
}
init方法裡面:
// bottom
groundBox.SetAsEdge(b2Vec2(0,90/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,0));
CCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithFile:@"blocks.png" capacity:150];
[self addChild:batch z:0 tag:kTagBatchNode];
目前為止都成功!
再來將整個邊界,拉長到兩倍!也就是把寬度增加到螢幕兩倍。
init方法裡面:
// bottom
groundBox.SetAsEdge(b2Vec2(0,90/PTM_RATIO), b2Vec2(screenSize.width * 2/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);
// top
groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO),
b2Vec2(screenSize.width * 2/PTM_RATIO,screenSize.height/PTM_RATIO));
groundBody->CreateFixture(&groundBox,0);
// left
groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));
groundBody->CreateFixture(&groundBox,0);
// right
groundBox.SetAsEdge(b2Vec2(screenSize.width * 2/PTM_RATIO,screenSize.height/PTM_RATIO),
b2Vec2(screenSize.width * 2/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);
再來是,畫面可以追著滾動的球跑!
待續....(XD)
首先開始一個cocos2D + Box2D的專案。
修改邊界,製造斜度。將方塊改成circle,調整circle的摩擦係數(橘色底就是刪掉的,紅色字是更改的):
-(void) addNewSpriteWithCoords:(CGPoint)p
{
CCLOG(@"Add sprite %0.2f x %02.f",p.x,p.y);
//We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is
//just randomly picking one of the images
// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
b2Body *body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2CircleShape dynamicCircle;
dynamicCircle.m_radius = 0.5;//These are mid points for our 1m box
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicCircle;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.1f;
body->CreateFixture(&fixtureDef);
}
init方法裡面:
// bottom
groundBox.SetAsEdge(b2Vec2(0,90/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,0));
目前為止都成功!
再來將整個邊界,拉長到兩倍!也就是把寬度增加到螢幕兩倍。
init方法裡面:
// bottom
groundBox.SetAsEdge(b2Vec2(0,90/PTM_RATIO), b2Vec2(screenSize.width * 2/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);
// top
groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO),
b2Vec2(screenSize.width * 2/PTM_RATIO,screenSize.height/PTM_RATIO));
groundBody->CreateFixture(&groundBox,0);
// left
groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));
groundBody->CreateFixture(&groundBox,0);
// right
groundBox.SetAsEdge(b2Vec2(screenSize.width * 2/PTM_RATIO,screenSize.height/PTM_RATIO),
b2Vec2(screenSize.width * 2/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);
再來是,畫面可以追著滾動的球跑!
待續....(XD)
留言
張貼留言