A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: [F8] friction / decceleration help?

  1. #1
    Member
    Join Date
    Jan 2006
    Posts
    30

    [F8] friction / decceleration help?

    I was wondering if someone could help to write a deccelaration code to this (tutorial example) ship?

    I'm kind of stuck myself. I provided both the .fla (flash8) and the full code here.

    Thanks in advance!

    Code:
    Spaceship.total = 0;
    function Spaceship(base_mc:MovieClip, linkageId_str:String, xPos:Number, yPos:Number, accel:Number, rotateAmount:Number, startRotation:Number)
    {
      Spaceship.total++;
      var instanceName_str:String = "spaceship" + Spaceship.total;
      this._mc = base_mc.attachMovie(linkageId_str, instanceName_str, base_mc.getNextHighestDepth(), {_x:xPos, _y:yPos, _rotation:startRotation});
      this.speedX = 0;
      this.speedY = 0;
      this.acceleration = accel;
      this.accelX = 0;
      this.accelY = 0;
      this.rotateAmount = rotateAmount;
    }
    
    
    Spaceship.prototype.getComponents = function():Void
    {
      var tSpaceship:Number = this._mc._rotation * Math.PI / 180;
      this.accelX = this.acceleration * Math.cos(tSpaceship);
      this.accelY = this.acceleration * Math.sin(tSpaceship);
    };
    
    
    
    Spaceship.prototype.move = function():Void
    {
      this.getComponents();
      this._mc._x += this.speedX;
      this._mc._y += this.speedY;
    };
    
    
    Spaceship.prototype.steer = function(steerDir_str:String):Void
    {
      if(steerDir_str.toLowerCase() == "left")
        this._mc._rotation -= this.rotateAmount;
      else if(steerDir_str.toLowerCase() == "right")
        this._mc._rotation += this.rotateAmount;
    
    };
    
    Spaceship.prototype.thrust = function():Void
    {	
      this.getComponents();
      this.speedX += this.accelX;
      this.speedY += this.accelY;
    };
    
    
    
    
    _root.createEmptyMovieClip("base_mc", _root.getNextHighestDepth());
    var mySpaceship:Object = new Spaceship(base_mc, "Spaceship", (Stage.width / 2), (Stage.height / 2), 0.2, 5, -90);
    onEnterFrame = function():Void
    {
      if(Key.isDown(Key.LEFT))
        mySpaceship.steer("left");
      else if(Key.isDown(Key.RIGHT))
        mySpaceship.steer("right");
    
      if(Key.isDown(Key.UP))
        mySpaceship.thrust();
      mySpaceship.move();
      
    };
    Attached Files Attached Files

  2. #2
    FK founder & general loiterer Flashkit's Avatar
    Join Date
    Feb 2000
    Location
    Sydney
    Posts
    1,149
    just multiply the ships x and y speed by say 0.9 on an enterframe to simulate friction.

    Using prototypes is pretty old school (read actionscript 1!!) you should look to movign it up to actionscript 2

    to deccelerate you would make acceleration negative
    Regards Mark Fennell - Flash Kit Founder, general loiterer
    -------------------------------
    I Hate Zombies - iPhone Game | markfennell.com

  3. #3
    Member
    Join Date
    Jan 2006
    Posts
    30
    thanks!.. that was really easy.. just multiplied the xy-speeds by 0.95 at the NOT Key.UP . any ideas on how to limit the max speed?

  4. #4
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    Code:
    Spaceship.prototype.thrust = function():Void
    {	
      this.getComponents();
      if((this.speedX + this.accelX) < this.maxSpeedX) {
          this.speedX += this.accelX;
      }
      if((this.speedY + this.accelY) < this.maxSpeedY) {
          this.speedY += this.accelY;
      }
    };

  5. #5
    Member
    Join Date
    Jan 2006
    Posts
    30
    hmm that did work but.. only when flying to the right and down. when flying up and left it has no limit as before..

    a little confused why it does that.. :S

  6. #6
    Senior Member rachil0's Avatar
    Join Date
    Jul 2007
    Location
    Columbus, OH.
    Posts
    465
    Use absolute values. When moving up/left your velocity and acceleration components are both negative (so they are less than maxSpeedX/Y, because those are positive constants - and the clamping test never triggers).

    A robust way to constrain both x/y axes and both +/- directions is to use:

    Code:
    var newspeedX = this.speedX + this.accelX;
    var newspeedY = this.speedY + this.accelY;
    if ( sqrt(newspeedX*newspeedX + newspeedY*newspeedY) < this.maxSpeed)
      {
      this.speedX = newspeedX;
      this.speedY = newspeedY;
      }
    (This treats speedX/Y like a vector whose magnitude is clamped to this.maxSpeed)

  7. #7
    Member
    Join Date
    Jan 2006
    Posts
    30
    Trying to accomplish your idea but it doesn't work for me.. may I ask how my thrust function should look like with these absolute values.. ?

  8. #8
    Senior Member rachil0's Avatar
    Join Date
    Jul 2007
    Location
    Columbus, OH.
    Posts
    465
    Try this.

    Code:
    Spaceship.prototype.thrust = function():Void
    {	
      this.getComponents();
      if(Math.abs(this.speedX + this.accelX) < this.maxSpeedX) {
          this.speedX += this.accelX;
      }
      if(Math.abs(this.speedY + this.accelY) < this.maxSpeedY) {
          this.speedY += this.accelY;
      }
    };
    I have no idea what version of AS you're doing, so Math.abs might be found under some other name in your platform. It just means absolute value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center