Free ActionScript

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

Physics – Multiple objects (balls) colliding

After months and months of trying to figure this one out, I finally managed to build a fully working example!

So, here is the first version of my Simple Physics script – multiple balls colliding and bouncing off each other.

Preview

Download Fla Sample

Download Fla Sample

I have to give credit to a book called HTML5 Canvas by Steve Fulton & Jeff Fulton (great book!). It was very hard to grasp the law of conservation of momentum without it. Thanks guys!

Drag, Drop and Snap Inventory System

My latest AS3 Game Inventory System.

Features

  • Drag, drop and snap
  • Create/destroy inventory containers
  • Create/destroy level containers
  • Move items between different movieclip containers
  • Create/destroy items in level
  • Create/destory items in inventory – new
  • Find empty inventory slot – new
  • Added item selected/diselected state – new
  • Added slot full/empty state – new
  • Code optimization and more comments – new

Preview – v1.1.2

Download Fla Sample

Download Fla Sample


Wishlist

  • Stackable Items – items of the same type can be placed in the same slot
  • Auto-pickup item – items placed in bag automatically, if bag has space
  • Multiple Level containers – different game levels
  • Multiple Inventory containers – bag, bank, store
  • Destroy items
  • Sell items
  • Show Item Tooltips

Bug Fixes
v1.1.2

  • Fixed bug when releasing mouse over item, while holding nothing – Thanks 19greg96!

v1.1.1

  • Fixed “slot full” bug after removing item from slot – Thanks 19greg96!
  • Fixed publishing issue for Flash Player 10

AS2
Older AS2 version is available here:
http://www.freeactionscript.com/2008/11/drag-drop-snap-inventory-system-v2/

 

 

Scroller – Dynamic Scrollbar v2

A newer, better version of my scrollbar class. This one does not have any classes attached to movieclips. Instead you pass it a content clip, mask clip, track clip and slider clip like so:

_scrollbar = new Scrollbar();
_scrollbar.init(content, contentMask, track, slider);

This version also supports mouse wheel scrolling.

Download Fla Sample

Download Fla Sample

Simple Physics with Friction & Gravity

Adding gravity to a ball from my previous Simple Physics with Friction AS3 example.

This script creates a ball and makes it bounce around in a container, as a real ball would. The ball slows down over time and comes to a stop due to friction and gravity.

Click ball to reset speed

View Code ACTIONSCRIPT
/**
 * Simple Physics with Friction & Gravity
 * ---------------------
 * VERSION: 1.2
 * DATE: 11/28/2010
 * AS3
 * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com
 **/
package com.freeactionscript
{
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip
	import com.freeactionscript.Ball;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
 
	public class SimplePhysics
	{
		// reference to container (stage, movieclip or sprite)
		private var _canvas:DisplayObjectContainer;
 
		// ball object
		private var _ball:Ball;
 
		// boundries
		private var _boundries:Rectangle;
		private var _minX:int;
		private var _minY:int;
		private var _maxX:int;
		private var _maxY:int;
 
		// settings
		private var _friction:Number = .98;
		private var _gravity:Number = .68;
 
		/**
		 * 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() * 20 - 20;
			var newRandomY:Number = Math.random() * 20 - 30;
 
			// Create new ball. Usage: new Ball(x, y, velocity X, velocity Y);
			_ball = new Ball(150, 200, newRandomX, newRandomY);
 
			// add mouse listener to ball
			_ball.addEventListener(MouseEvent.CLICK, onBallClick);
			_ball.buttonMode = true;
 
			//
			$container.addChild(_ball);
		}
 
		/**
		 * On ball CLICK handler
		 * @param	$even	Takes MouseEvent
		 */
		private function onBallClick($event:MouseEvent):void
		{
			resetBall(Ball($event.target));
		}
 
		/**
		 * 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);
		}
 
		/**
		 * Resets ball's volocity
		 * @param	$ball	Takes Ball object
		 */
		public function resetBall($ball:Ball):void
		{
			// get random number between -10 and 10
			var newRandomX:Number = Math.random() * 20 - 20;
			var newRandomY:Number = Math.random() * 20 - 30;
 
			// update ball velocity
			$ball.velocityX = newRandomX;
			$ball.velocityY = newRandomY;
		}
 
		/**
		 * Sets container boundries
		 */
		public function setBoundries($container:DisplayObjectContainer):void
		{
			_boundries = new Rectangle(0, 0, $container.width, $container.height);
			_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;
			}
 
			// apply friction to ball velocity
			_ball.velocityX *= _friction;
			_ball.velocityY *= _friction;
 
			// apply gravity (only to Y axis)
			_ball.velocityY += _gravity;
 
			// Update X
			// Update position
			_ball.x += _ball.velocityX;
 
			// Update Y
			// Make sure we dont fall thru the bottom
			if ((_ball.y + _ball.velocityY + (_ball.height / 2)) > _maxY)
			{
				// if we're falling thru, set ball y at maxY, minus ball size
				_ball.y = _maxY - (_ball.height / 2);
			}
			else
			{
				// update position
				_ball.y += _ball.velocityY;
			}
 
		}
	}
}

Only a couple of small changes were made to the original script.

Added a gravity variable:

private var _gravity:Number = .68;

Add gravity to Y velocity:

_ball.velocityY += _gravity;

And make sure the ball doesn’t fall through the floor:

if ((_ball.y + _ball.velocityY + (_ball.height / 2)) > _maxY)
{
_ball.y = _maxY - (_ball.height / 2);
}
else
{
_ball.y += _ball.velocityY;
}

Download Fla Sample

Download Fla Sample

See how to make multiple balls collide and bounce off each other.