Search code examples
actionscript-3flashbox2dflashdevelop

Where is the impulse coming from?


I am writting a code for character movement with A, D keys (left and right) using Box2D physics in AS3 and I don't know how is the b2body gaining impulse.x in the code you're about to see,

switch (moveState)
    {
    case "RIGHT":
        if (mainChar.GetLinearVelocity().x = 1.3)
        {
            impulse.x = 0.0;
            impulse.y = 0.0;
            mainChar.ApplyImpulse(impulse, mainChar.GetPosition());
        }
        break;
    case "LEFT":
        if (mainChar.GetLinearVelocity().x = -1.3)
            {
                impulse.x = 0.0;
                impulse.y = 0.0;
                mainChar.ApplyImpulse(impulse, mainChar.GetPosition());
            }

    case "STOP":
        if (mainChar.GetLinearVelocity().x > 0) 
        {                       
            impulse.x = -mainChar.GetLinearVelocity().x; 
            impulse.y = 0.0;
            mainChar.ApplyImpulse(impulse, mainChar.GetPosition());
            impulse.x = 0.0;
        }
        if (mainChar.GetLinearVelocity().x < 0)
        {
            impulse.x = +mainChar.GetLinearVelocity().x; 
            impulse.y = 0.0;
            mainChar.ApplyImpulse(impulse, mainChar.GetPosition());
            impulse.x = 0.0;
        }
            if (mainChar.GetLinearVelocity().x = 0)
            {
                impulse.x = 0.0;
                break;  
            }

Nowhere in the code I am giving impulse.x value of 1.3 or -1.3. Anything I don't know about if statements? ^^"

EDIT: Oh ok, I guess "=" assigns the value to it. I didn't know about it - still is a bit weird to me as it is inside an if condition. (What I intended was to test equality, "==")

However, I do not want this, do I? I read I shouldn't set a linear speed rather let the b2body accelerate and stay towards desired speed.


Solution

  • It comes from here: if (mainChar.GetLinearVelocity().x = 1.3). It's an assignment rather than comparison.