Go Back   CodingForums.com > :: Client side development > Flash & ActionScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-16-2012, 06:39 AM   PM User | #1
TehMadHatter
New Coder

 
Join Date: Jan 2012
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
TehMadHatter is an unknown quantity at this point
[as3] Gravity

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:
Code:
		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

Code:
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;
	}
}

Last edited by TehMadHatter; 02-16-2012 at 06:46 AM..
TehMadHatter is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:51 AM.


Advertisement
Log in to turn off these ads.