1
我正在研究曲棍球比賽,並且正在實施單人模式。我正試圖將「電腦」的槳在進攻模式下移動(朝着球移動)。我正在使用CoCos2d和Box2d。我正在使用MouseJoints移動槳。問題是槳沒有任何動作!Box2d + CoCos2d:自動移動對象以模擬遊戲中的計算機運動
滴答被調用init方法
[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);
我會嘗試一個答案,但你的代碼真的很難閱讀。你應該遵循你使用的任何語言的方法和變量命名約定。此外,還有一種方法可以將代碼粘貼到問題中,以使其格式正確。 – 2010-04-30 00:26:43