Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1

Thread: [as3] Gravity

  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    [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.


 

Posting Permissions

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