簡體   English   中英

恆定字符移動-AS3

[英]Constant Character Movement - AS3

我應該如何(我?)使用Box2D進行角色移動? 我需要玩家與地板和其他物體碰撞,但是當要左右移動他時,沖動並不是很方便。 至少不是我現在所擁有的。

private function update(e:Event):void {
if (keys[Keyboard.RIGHT]) {
  impulse_x = 3;
  impulse_y = 0;
  mainChar.ApplyImpulse(new b2Vec2(impulse_x, impulse_y), mainChar.GetPosition());
} else {
  impulse_x = 0;
  impulse_y = 0;
  mainChar.ApplyImpulse(new b2Vec2(impulse_x, impulse_y), mainChar.GetPosition());
  }
}

我如何穩定地移動角色,與上面的代碼相反,當按下右鍵時,每次更新都會加速mainChar。

編輯:

因此,這里有這段代碼,但它在c ++上,我想-我很感謝對AS3 ^^的翻譯,因為它是如此之小,因為我不知道float在做什么,語法對我來說有點混亂。

b2Vec2 vel = body->GetLinearVelocity();
float desiredVel = 0;
switch ( moveState )
{
  case MS_LEFT:  desiredVel = -5; break;
  case MS_STOP:  desiredVel =  0; break;
  case MS_RIGHT: desiredVel =  5; break;
}
float velChange = desiredVel - vel.x;
float impulse = body->GetMass() * velChange; //disregard time factor
body->ApplyLinearImpulse( b2Vec2(impulse,0), body->GetWorldCenter() );

在AS3中

switch (moveState)
    {
        case "RIGHT": desiredVel = 1.4; break;
        case "LEFT": desiredVel = -1.4; break;
        case "STOP": desiredVel = 0; break;
    }

velChange = desiredVel - mainChar.GetLinearVelocity().x;
impulse.x = velChange;
impulse.y = 0.0;
mainChar.ApplyImpulse(impulse, mainChar.GetWorldCenter());

(我應該刪除帖子的開頭部分並留下答案嗎?)

你應該用力。 在這里有很好的描述: http : //www.iforce2d.net/b2dtut/forces

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM