...

View Full Version : [as3] Gravity



TehMadHatter
02-16-2012, 06:39 AM
Alright well, I am trying to learn about gravity and NO I am not going to just leach someones gravity so please don't just give me someone elses...I tried this on my own and it does work...But I don't know if the way I did it will work in the future when I add other things

At the part:
if (jumpSpeed < 0) {
jumpSpeed *= -1.3;
trace(ball.y);
ball.y += jumpSpeed + 2.7;
}


I added the +2.7 because it was 2.7 high on the y axis (negative)
But I don't know the reason why


import flash.events.KeyboardEvent;
import flash.events.Event;





//Detect Mouse Location
var mouseLocText:TextField = new TextField();
addEventListener(Event.ENTER_FRAME, showMouseLoc);
function showMouseLoc(X1:Event) {
mouseLocText.text = "X="+mouseX+" Y="+mouseY;
mouseLocText.x = 300;
mouseLocText.y = 300;
addChild(mouseLocText);
}





ball.addEventListener(Event.ENTER_FRAME, animate);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPush);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPull);

var leftArrow:Boolean = false;
var rightArrow:Boolean = false;
var spacebar:Boolean = false;
var isJumping:Boolean = false;
var speed:Number = 5;
var jumpSpeed:Number = 0;
var maxjumpSpeed:Number = 15;
var count:Number = 0;
function jump():void
{
if (!isJumping) {
isJumping = true;
jumpSpeed -= maxjumpSpeed;
ball.y += jumpSpeed;
}
if (jumpSpeed < -maxjumpSpeed / 5) {
jumpSpeed /= 1.3;
ball.y += jumpSpeed;
} else {
if (jumpSpeed < 0) {
jumpSpeed *= -1.3;
trace(ball.y);
ball.y += jumpSpeed + 2.7;
}
if (jumpSpeed <= maxjumpSpeed) {
jumpSpeed *= 1.3;
ball.y += jumpSpeed;
}
else {
jumpSpeed = 0;
isJumping = false;
}
}
}

function animate(event:Event):void
{
if (leftArrow) {
ball.x -= speed;
}
if (rightArrow) {
ball.x += speed;
}
if (spacebar || isJumping) {
jump();
}
}

function keyPush(event:KeyboardEvent):void
{
if (event.keyCode == 37) {
leftArrow = true;
}
if (event.keyCode == 39) {
rightArrow = true;
}
if (event.keyCode == 32) {
spacebar = true;
}
}
function keyPull(event:KeyboardEvent):void
{
if (event.keyCode == 37) {
leftArrow = false;
}
if (event.keyCode == 39) {
rightArrow = false;
}
if (event.keyCode == 32) {
spacebar = false;
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum