AS3 Smooth Jumping


I would like to know how to make a smooth jump in my game. Its a 2D game and the code is really simple but I would want to know how to make it better for it to slow down when it gets to the max height and then smooth drop.

This is all I have for jumping:

Player.y -= 50;


Your best bet would be to use a physics engine (Box2d etc). If you don't want the overhead of one though (if the only thing you'd use it for is jumping and not collisions) then you just need to add some friction to your logic.

var friction :Number = .85; //how fast to slow down / speed up - the lower the number the quicker (must be less than 1, and more than 0 to work properly) var velocity :Number = 50; //how much to move every increment, reset every jump to default value var direction :int = -1; //reset this to -1 every time the jump starts function jumpLoop(){ //lets assume this is running every frame while jumping player.y += velocity * direction; //take the current velocity, and apply it in the current direction if(direction < 0){ velocity *= friction; //reduce velocity as player ascends }else{ velocity *= 1 + (1 - friction); //increase velocity now that player is falling } if(velocity < 1) direction = 1; //if player is moving less than 1 pixel now, change direction if(player.y > stage.stageHeight - player.height){ //stage.stageheight being wherever your floor is player.y = stage.stageHeight - player.height; //put player on the floor exactly //jump is over, stop the jumpLoop } }


Copy/paste the following code... jump() can be replaced by jump2() (without bouncing effect). The jumping will be produced by the space bar:

const FA:Number = .99; // air resistance const CR_BM:Number = .8; // bouncing coefficient const µ:Number = .03; // floor friction const LB:int = stage.stageHeight; // floor (bottom limit) const G:int = 2.5; // gravity const R:int = 50; var ball:MovieClip = new MovieClip(); this.addChild(ball); var ba:* = ball.graphics; ba.beginFill(0xFFCC00); ba.lineStyle(0, 0x666666); ba.drawCircle(0, 0, R); ba.endFill(); ball.vx = 2; ball.vy = -30; ball.r = R; ball.x = 100; ball.y = LB - R; stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown); function myKeyDown(e:KeyboardEvent):void { if (e.keyCode == Keyboard.SPACE) { ball.vy = -30; addEventListener(Event.ENTER_FRAME, jump); } } function jump(e:Event):void { ball.vy = ball.vy + G; ball.vx *= FA; ball.vy *= FA; ball.x += ball.vx; ball.y += ball.vy; if (ball.y > LB - ball.r) { ball.y = LB - ball.r; ball.vy = -1 * ball.vy * CR_BM; ball.vx += ball.vx * - µ; } } /* function jump2(e:Event):void { ball.vy = ball.vy + G; ball.vx *= FA; ball.vy *= FA; ball.x += ball.vx; ball.y += ball.vy; if (ball.y > LB - ball.r) { ball.y = LB - ball.r; } } */


  • How can I mask or convert Int32 to Int16 in swift
  • How to play NSData from Core Data in AVAudioPlayer
  • how to get query string value using javascript
  • All bodies have rigidbody 2d and boxcollider2d and there's no collision
  • LibGDX Multiply Vector2 with float value
  • Weird session behaviour in codeigniter
  • Printing out Japanese (Chinese) characters
  • Creating interactive SVG maps for a web portal
  • TCameraComponent and TVideoCaptureDevice do not initialize in Win32
  • How read between delimiters in php DOM of a XML file?
  • How to make a UserDetailsManager available as a bean
  • how to calculate distance between two points in google maps?
  • Setting a relative path to sqlite database with Delphi and Firedac
  • Put percent labels next to legend instead of in the slice
  • HABTM associations in Rails : collecting and counting the categories of a model's children
  • Adding a new element into the DOM with angularjs does not initiate it
  • Localization issue Xcode
  • Parse Framework with Swift
  • Connect Node.js with Oracle on Windows platform
  • Zend framework 2 : Add different authentication adapter for two different modules
  • Guava how to copy all files from one directory to another
  • Lua: Line breaks in strings
  • Is there any way to call saveCurrentTurnWithMatchData without sending a push notification?
  • Angular2 - Template reference inside NgSwitch
  • For loop with if condition on multiple R functions
  • Is there a way to save the selected text and highlight it again once the page is refreshed?
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • Abort upload large uploads after reading headers
  • D3 nodes and links from JSON with nested arrays of children
  • How to rebase a series of branches?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • How to format a variable of double type
  • Data Validation Drop Down Box Arrow Disappearing
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal