Actionscript 3 恒定字符移动-AS3
如何使用Box2D进行角色移动?我需要球员与地板和其他人发生碰撞,但是当他左右移动时,冲动不是很方便。至少不是我现在拥有的Actionscript 3 恒定字符移动-AS3,actionscript-3,flash,box2d,flashdevelop,Actionscript 3,Flash,Box2d,Flashdevelop,如何使用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()); } e
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更新
编辑:
<> P>这里面有这个代码,但它是C++的,我想我会喜欢翻译,因为它太小了,到了AS^ ^ ^,因为我不知道浮标在做什么,语法对我来说有点混淆。
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());
(我应该删除帖子的开头部分并留下答案吗?你应该使用武力。这里有很好的描述:您必须检查当前速度,并调整应用的脉冲。这可能会有帮助:我会尽量绕开它,因为我能理解这里和那里的一些信息,但我在AS3中写下了这个