Gravity only working after a keystoke AS3 - actionscript-3

Here is my problem, I am new to programming and I am following a tutorial just to learn random things. I have gotten everything to work so far except gravity. When I run the program my player is floating, but when I hit the "down" key only then the gravity takes effect.. I can't figure out why.
if(leftBumping){
if(xSpeed < 0){
xSpeed *= -0.5;
}
}
if(rightBumping){
if(xSpeed > 0){
xSpeed *= -0.5;
}
}
if(upBumping){
if(ySpeed < 0){
ySpeed *= -0.5;
}
}
if(downBumping){
if(ySpeed > 0){
ySpeed *= 0.0;
}
}
else {
ySpeed += gravityConstant;
}
I have a trace on the bumping collisions and they all work properly. If i am in open space it detect no collision, and when touching walls the output shows i am. Iv been reworking these lines for hours. PLease help
this may help also
if(aPressed){
xSpeed -= speedConstant;
} else if (dPressed){
xSpeed += speedConstant;
}
if (wPressed){
ySpeed -= speedConstant;
} else if(sPressed){
ySpeed += speedConstant;
}
if(leftBumping){
if(xSpeed < 0){
xSpeed *= -0.5;
}
}

You need to stick this bit:
ySpeed += gravityConstant;
into a function that is called every frame, like this:
this.addEventListener(Event.ENTER_FRAME, onEnterFrame)
private function onEnterFrame(e:Event):void
{
ySpeed += gravityConstant;
}
if this is happening to quickly, you will either need to reduce your gravity constant, reduce the fps, or use a timer rather than enterframe.

Related

ActionScript 3, How to get character to jump for longer

I am making a platformer game in Flash (AS3) and the code I have below works. I want my character to jump high enough to allow it time to reach a platform. The only problem with code below is the speed at which it jumps up and down and the height of the jump. Tthe space bar is what triggers the function to run.
Please help as I would much appreciate it! :)
Player.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function fl_MoveInDirectionOfKey(event:Event)
{
if (spacePressed){
var gravity:Number = 9.8;
var jumping:Boolean = false;
var jumpVar:Number = 0;
if(jumping != true)
{
jumpVar = -70;
jumping = true;
}
if(jumping)
{
spacePressed = false;
Player.y += jumpVar;
jumpVar += gravity;
}
Player.addEventListener(Event.ENTER_FRAME, drop);
function drop(event:Event)
{
Player.y -= jumpVar;
jumpVar -= gravity;
if(Player.y > 350){
Player.y = 350;
}
}
Player.removeEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
Player.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
/*var frameNumb:Number = 0;
Player.addEventListener(Event.ENTER_FRAME, jumpup);
spacePressed = false;
function jumpup(event:Event)
{
while(frameNumb < 30){
spacePressed = false;
Player.y -= 1;
frameNumb += 0.5;
}
Player.removeEventListener(Event.ENTER_FRAME, jumpup);
Player.addEventListener(Event.ENTER_FRAME, jumpdown);
function jumpdown(){
while(frameNumb > 0){
spacePressed = false;
Player.y += 1;
frameNumb -= 0.5;
}
}
}*/
}
if (leftPressed)
{
Player.x -= speed;
Player.gotoAndStop("left");
}
if (rightPressed)
{
Player.x += speed;
Player.gotoAndStop("right");
}
}
Thanks
Your use of 9.8 for gravity is meters-per-second-per-second. Since drop() is executed every frame, you're going to get some serious fast-forward gravity, unless the program is doing only 1 FPS. So, assuming you want more fluidity than 1 FPS, consider doing
jumpVar += gravity/fps;
However, to get the exact velocity required to lift you to a height, I think the calculation is...
initialVelocityInMetersPerSecond = Math.sqrt( 2 * gravity * metersToJump )
So instead of jumpVar = -70, you would do something like...
// get posititive distance since we'll use to get a square root
var metersToJump:Number = Player.y - platform.y;
jumpVar = -Math.sqrt( 2 * gravity * metersToJump );
...and then in the ENTER_FRAME handler...
Player.y += jumpVar / fps;
jumpVar += gravity / fps;
From your example it's not the case, but if you place the platform below the Player, it won't work as you can't get the root of a negative number!
In my example code I am not fixing the height of the platform, so how you decide on the target platform is a completely separate matter.

Action Script 3.0 timer doesn't stop

I'm trying to make flappy birds in actionscript (just for practicing and fun). This is my first programming language and I'm still new to this.
So the problem starts here, I want to make the bird rotates (as the real flappy bird does) every 2 seconds when no button is pressed. But it turned out that the timer still activates after I pressed spacebar again which I think it is supposed to stop the last timer first before activating the new one.
If I press spacebar 2 times, the timer will activate twice. Without stopping the timer first.
Code :
stage.addEventListener (KeyboardEvent.KEY_DOWN, jump);
function jump(event: KeyboardEvent):void
{
var myTimer4:Timer = new Timer (2000)
if(event.keyCode == 32)
{bird.y=bird.y-40;
bird.rotation=0;
myTimer4.stop();
myTimer4.start();
}
myTimer4.addEventListener(TimerEvent.TIMER, fall);
function fall (e:TimerEvent):void{
bird.rotation=40;
myTimer4.stop();
}
I think the problem might be you are creating a new instance of Timer every time the key is pressed and myTimer4 takes in a new reference. Try removing it outside the function scope like this:
var myTimer4:Timer = new Timer (2000);
function jump(event: KeyboardEvent):void
{
if(event.keyCode == 32)
{bird.y=bird.y-40;
bird.rotation=0;
myTimer4.stop();
myTimer4.start();
}
onClipEvent (load) {
power = 0.3;
yspeed = 0;
xspeed = 0;
friction = 0.95;
gravity = 0.5;
thrust = 3.75;
wind = 0.18;
_root.level1_text.text = 0+collected_coin19;
reverse = new Sound();
reverse.attachSound("hit2");
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
xspeed -= power;
}
if (Key.isDown(Key.RIGHT)) {
xspeed += power;
}
if (Key.isDown(1)) {
yspeed -= power*thrust;
}
if (Key.isDown(Key.SPACE)) {
yspeed -= power*thrust;
}
xspeed += wind;
xspeed *= friction;
yspeed += gravity;
_y += yspeed;
_x += xspeed;
Try This one Might Help you

character is bouncing when hits the ground. it does not stay stable

The question is clear. Character is stable on the stage and background and ground is moving. But when character hits the ground, it bounces again and again. Here is the code. how can i solve it? When i move the character its working perfect but when the moving object is ground bouncing problem happens.
here is the problem : http://www.swfcabin.com/open/1391814250
public function controller():void
{
if (rightKey || leftKey || jumpKey)
{
if (rightKey)
{
if (jumpWalk || canJump)
{
hero.gotoAndStop(2);
hero.scaleX = 1;
xSpeed -= speed;
}
}
if (leftKey)
{
if (jumpWalk || canJump)
{
hero.gotoAndStop(2);
hero.scaleX = -1;
xSpeed += speed;
}
}
if (jumpKey && canJump)
{
ySpeed += 15;
canJump = false;
}
}
else
{
hero.gotoAndStop(1);
}
if(!canJump){
hero.gotoAndStop(3);
}
ySpeed -= gravity;
if(ySpeed >10){
ySpeed = 10;
}
else if(ySpeed < -10){
ySpeed = -10;
}
if(xSpeed>10){
xSpeed = 10
}
else if(xSpeed < -10){
xSpeed = -10;
}
xSpeed *= 0.8;
level.x += xSpeed;
level.y += ySpeed;
}// controller function
public function loop(event:Event):void
{
controller();
while(level.hitTestPoint(hero.x , hero.y + hero.height/2 -1 - ySpeed , true)){
trace(ySpeed +"dasd");
ySpeed ++;
canJump = true;
} }
Here is what is happening now: when you are embedded in terrain, you are increasing the ySpeed -- the upward momentum -- until one 'tick' pulls the character out of the ground. But then the character is flying upward, because you increased their upward momentum. They still have that upward momentum until gravity pulls them back down, so they will keep 'bouncing'. To fix this, do the following:
Instead of ySpeed ++; in the main loop, try level.y++; This would represent pulling the hero out of the terrain when he is embedded (or actually pulling the terrain down, since your character is stationary), instead of changing his momentum to get him out.
You should also add ySpeed = 0; in there. That would represent losing all of your y-momentum when you hit the ground.
So your main loop would look like this:
public function loop(event:Event):void
{
controller();
while(level.hitTestPoint(hero.x , hero.y + hero.height/2 -1 - ySpeed , true)){
trace(ySpeed +"dasd");
level.y++;
ySpeed = 0;
canJump = true;
} }

Player velocity will never go to zero after collision?

My collision code is supposed to inverse the x velocity and reduce it to a third, then when the player is not pressing either "A" or "D" keys, the velocity should slowly reduce to zero, however, once the player collides the velocity only reduces to a small decimal between .25 and .70 (either positive or negative based on previous directions.)
//Function to determine what to do with keys
function KeyHandler():void{
//A Key Handlers
if(aKeyPressed == true){
if(Math.abs(_vx) < MaxSpeed){
_vx += -6;
}
}
//Player _vx somehow won't hit zero!!!
else if(aKeyPressed == false){
if(_vx < 0){
_vx += 1;
}
}
//D Key Handlers
if(dKeyPressed == true){
if(Math.abs(_vx) < MaxSpeed){
_vx += 6;
}
}
else if(dKeyPressed == false){
if( _vx > 0){
_vx += -1;
}
}
//W Key Handlers
if(wKeyPressed == true){
if(Jumped == false){
_vy = -15;
Jumped = true;
}
}
else if(wKeyPressed == false){
}
}
//Code for Right Collision
if(RightCollision){
while(RightCollision){
Player.x -= 0.1;
RightCollision = false;
if(_boundaries.hitTestPoint((Player.x + (Player.width / 2)), (Player.y - (Player.height / 2)), true)){RightCollision = true;}
}
_vx *= -.33
}
//Code for Left Collision
if(LeftCollision){
while(LeftCollision){
Player.x += 0.1;
LeftCollision = false;
if(_boundaries.hitTestPoint((Player.x - (Player.width / 2)), (Player.y - (Player.height / 2)), true)){LeftCollision = true;}
}
_vx *= -.33
}
Note that abs(-.25) + abs(.7) ~ 1.0
The collision sets the velocity to something that is not an integer (e.g. 2 * .33 ~ .7), so +/- 1 will skip past 0 without landing on it.
The simple fix is to keep the velocity an integer which can be done with Math.floor, for instance. (Account for the difference in +/- velocities: floor only moves numbers one direction.)
Happy coding.
Also, I am not sure how the int type works in AS3 work, which might be worth exploring.

Constrain movement of HSlider Thumb by Accelerometer

I'm moving an HSlider thumb via the accelerometer. The following code works fine. The problem, though, is that as I keep tilting the device, xSpeed continues to increment. This means that when I now tilt it the other way, the thumb doesn't move for a while -- since, depending on how long I was holding tilted in that intial direction, xSpeed has been going up and up.
So this works, but with the abovementioned flaw:
private function readAcc(e:AccelerometerEvent):void
{
xSpeed -= e.accelerationX * 4;
myHSlider.dispatchEvent(new FlexEvent("valueCommit"));
myHSlider.value += xSpeed;
}
But what I want to do is to stop incrementing xSpeed once the hSlider.value == either the minimum or the maximum. Sounds simple, but when I put in if statements, they prevent the thumb from moving at all:
private function readA(e:AccelerometerEvent):void
{
if(h.minimum < h.value && h.maximum > h.value)
{
xSpeed -= e.accelerationX * 4;
h.dispatchEvent(new FlexEvent("valueCommit"));
h.value += xSpeed;
var lastSpeed:Number = xSpeed;
}
else if (h.value == h.minimum || h.value == h.maximum)
{
xSpeed = lastSpeed;
h.dispatchEvent(new FlexEvent("valueCommit"));
h.value += xSpeed;
}
}
What should the logic be to make this work?
Thanks.
Perhaps your slider value is exceeding the min or max limits when you add (or subtract) xSpeed. Check for this, and subtract if the value is too high, or add if the value is too low.
// EDIT: Code modified to reflect most recent comment
if(h.minimum < h.value && h.maximum > h.value)
{
xSpeed -= e.accelerationX * 4;
var lastSpeed:Number = xSpeed;
}
else if (h.value <= h.minimum )
{
xSpeed = 2;
}
else if ( h.value >= h.maximum )
{
xSpeed = -2
}
h.value += xSpeed;
h.dispatchEvent(new FlexEvent("valueCommit"));
OK, have figured this out and wanted to share it. The key is not to mess with setting the hslider's h.value. Rather, just set the x of the thumb, and let the value take care of itself. The accelerometer event calls a handler; in that handler create a moveThumb() function which will adjust that x. Then, from within the moveThumb() function, dispatch the valueCommit event so that the hslider will respond.
private var xSpeed:Number;
private function accUpdateHandler(e:AccelerometerEvent):void
{
xSpeed -= e.accelerationX;
moveThumb();
}
private function moveThumb():void
{
var newX:Number = h.thumb.x + xSpeed;
var newY:Number = h.thumb.y + ySpeed;
if (newX < 0)
{
h.thumb.x = 0;
xSpeed = 0;
}
else if (newX > h.width - h.thumb.width)
{
h.thumb.x = h.width - h.thumb.width;
xSpeed = 0;
}
else
{
h.value += xSpeed;
}
h.dispatchEvent(new FlexEvent("valueCommit"));
}