dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Angle Question

  1. #1
    Senior Member
    Join Date
    May 2007
    Posts
    102

    Angle Question

    Hi,

    i am following a tutorial from a book, Learning Actionscript 3.0, A Beginners Guide, where it discusses how to move an object in an agle. I have managed to get the app working fine, but i'm tryin to test it out in different angles but everytime i change it seems to go into the same angle. Here is the code listings:

    Code:
    package  
    {
    	
    	/**
    	 * MoveAngle.as is meant to show you can move an object i.e a ball, along an angle
    	 * @author Luong Vuong
    	 * Date created: 26/11/2009
    	 * Last modified: 08/12/2009
    	 */
    	
    	import flash.display.MovieClip;
    	import flash.display.Sprite;
    	import flash.events.Event;
    	
    	public class MoveAngle extends Sprite 
    	{
    		private var ball:MovieClip;
    		
    		private var speed:Number;
    		private var angle:Number;
    		private var radians:Number;
    		private var xVel:Number;
    		private var yVel:Number;
    		
    		public function MoveAngle() :void 
    		{
    			ball = new Ball();
    			ball.x = ball.y = 100;
    			addChild(ball);
    			 
    			speed = 12;
    			angle = 10;
    			radians = deg2rad(angle);
    			xVel = Math.cos(radians) * speed;
    			yVel = Math.cos(radians) * speed;
    			
    			construct();
    		}
    		 
    		// Add listeners and add UI to display list
    		public function construct() :void
    		{
    			addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);	 
    				
    
    		}
    		
    		public function onLoop(evt:Event) :void
    		{
    			ball.x += xVel;
    			ball.y += yVel;
    		}
    		
    		// convert degrees to radians
    		public function deg2rad(deg:Number) :Number
    		{
    			return deg * (Math.PI / 180);
    		}
    		
    		 
    	}
     
    }
    Can somebody tell me why for everytime i changed the value of the degrees, it does not updat the movement of the ball please.

    P.S i'm using FlashDevelop
    Thanks

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You are using cosine for both x and y. The y velocity should be using sine instead. If they are both using the same, then xVel and yVel will have the same value, and the slope of the line will be 45 degrees.

  3. #3
    Senior Member
    Join Date
    May 2007
    Posts
    102
    Quote Originally Posted by 5TonsOfFlax View Post
    You are using cosine for both x and y. The y velocity should be using sine instead. If they are both using the same, then xVel and yVel will have the same value, and the slope of the line will be 45 degrees.
    AAH DOH!!! sorry, i just realised it after you had posted. I looked in the book and yes, it had x value for cos and y value for sin. Apoligise for the minor mistake and thanks for pointing it out.

    Cheers

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center