Free ActionScript

Flash AS2 & AS3 Tutorials, Game Code, Effects, Source Files & Sample Downloads

Simple Physics

3 Comments »

Very simple flash physics in AS3. This script creates a ball and makes it bounce around in a container, changing direction when a wall is hit.

View Code ACTIONSCRIPT
/**
 * Simple Physics
 * ---------------------
 * VERSION: 1.1
 * DATE: 11/21/2010
 * AS3
 * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com
 **/
package com.freeactionscript
{
	import flash.display.DisplayObjectContainer;
	import com.freeactionscript.Ball;
	import flash.events.Event;
 
	public class SimplePhysics
	{
		// reference to container (stage, movieclip or sprite)
		private var _canvas:DisplayObjectContainer;
 
		// ball object
		private var _ball:Ball;
 
		// boundries
		private var _minX:int;
		private var _minY:int;
		private var _maxX:int;
		private var _maxY:int;
 
		/**
		 * Constructor
		 * @param	$canvas	Takes DisplayObjectContainer (MovieClip, Sprite, Stage) as argument
		 */
		public function SimplePhysics($canvas:DisplayObjectContainer)
		{
			trace("SimplePhysics");
 
			_canvas = $canvas;
 
			setBoundries(_canvas);
			createBall(_canvas);
			enable();
		}
 
		/**
		 * Creates ball
		 */
		private function createBall($container:DisplayObjectContainer):void
		{
			// get random number between -10 and 10
			var newRandomX:Number = Math.random() * 10 - 10;
			var newRandomY:Number = Math.random() * 10 - 10;
 
			//Ball usage: new Ball(x, y, velocity X, velocity Y);
			_ball = new Ball(150, 200, newRandomX, newRandomY);
			$container.addChild(_ball);
		}
 
		/**
		 * Enables physics engine
		 */
		private function enable():void
		{
			_canvas.addEventListener(Event.ENTER_FRAME, update);
		}
 
		/**
		 * Disables physics engine
		 */
		public function disable():void
		{
			_canvas.removeEventListener(Event.ENTER_FRAME, update);
		}
 
		/**
		 * Sets container boundries
		 */
		public function setBoundries($container:DisplayObjectContainer):void
		{
			_minX = 0;
			_minY = 0;
			_maxX = $container.width;
			_maxY = $container.height;
		}
 
		/**
		 * Update function that updates ball
		 * @param	$event
		 */
		private function update($event:Event):void 
		{
			// Check X
			// Check if we hit top
			if (((_ball.x - _ball.width / 2) < _minX) && (_ball.velocityX < 0))
			{
			  _ball.velocityX = -_ball.velocityX;
			}
			// Check if we hit bottom
			if ((_ball.x + _ball.width / 2) > _maxX && (_ball.velocityX > 0))
			{
			  _ball.velocityX = -_ball.velocityX;
			}
 
			// Check Y
			// Check if we hit left side
			if (((_ball.y - _ball.height / 2) < _minY) && (_ball.velocityY < 0))
			{
			  _ball.velocityY = -_ball.velocityY
			}
			// Check if we hit right side
			if (((_ball.y + _ball.height / 2) > _maxY) && (_ball.velocityY > 0))
			{
			  _ball.velocityY = -_ball.velocityY;
			}
 
			// update ball position
			_ball.x += _ball.velocityX;
			_ball.y += _ball.velocityY;
		}
	}
}
Download Fla Sample

Download Fla Sample

Find out how to add friction in the next example.

3 Responses

When I try to build this it gives me an error saying “col:33 Error: Access of undefined property container. For some reason it does not know there is a movieclip in the .fla called container. Do you know how I might be able to fix this?

  • Hi Zeek,

    I just downloaded the file, published, and it works. Are you using your own modified version of this example? What version of Flash are you using?

    PR

  • How would I replace the ball with an image? I am working on making a screen saver that will bounce a image just like the ball in this code.

    Any help would be greatly appreciated.

    Regards,

    Joe

  • Leave a Reply

    You must be logged in to post a comment.