Flex 3 – Rounded Corner Utility

This is just a little utility for adding a rounded corner mask on UIComponents.

There is an available method for doing a similar effect on Canvas elements – you have to set “cornerRadius=”8″ ” or whatever radius and then set the border to thickness 0 for it to work. This is an alternative method which might be appropriate in some cases.

package Actionscript
{
	import mx.core.UIComponent;

	public class RoundedMaskGenerator
	{
		//some consts
		private static const ROUNDEDCORNER_TOP:String = "top";
		private static const ROUNDEDCORNER_BOTTOM:String = "bottom";
		private static const ROUNDEDCORNER_LEFT:String = "left";
		private static const ROUNDEDCORNER_RIGHT:String = "right";
		private static const ROUNDEDCORNER_TOPLEFT:String = "topLeft";
		private static const ROUNDEDCORNER_BOTTOMLEFT:String = "bottomLeft";
		private static const ROUNDEDCORNER_TOPRIGHT:String = "topRight";
		private static const ROUNDEDCORNER_BOTTOMRIGHT:String = "bottomRight";

		//apply a basic rounded corner effect to all sides
		public static function applyRoundedCornerMask(_target:UIComponent,_rad:int=8, _width:Number=0,_height:Number=0, _x:int = 0, _y:int = 0):void
		{
			var roundedMask:UIComponent= new UIComponent();
			_target.addChild(roundedMask);
			roundedMask.graphics.clear();
			roundedMask.graphics.beginFill(0x000000);
			roundedMask.graphics.drawRoundRectComplex(_x, _y, (_width==0)?_target.width:_width, (_height==0)? _target.height:_height, _rad,_rad, _rad,_rad);
			roundedMask.graphics.endFill();
			roundedMask.mouseEnabled= false;
			_target.mask = roundedMask;
		}

		//applies rounded corners only to the specified side of a component (left, right, top,bottom)
		public static function applyRoundedCornersToSide(_target:UIComponent,_side:String = ROUNDEDCORNER_TOP,_rad:int=8, _width:Number=0,_height:Number=0):void
		{
			var roundedMask:UIComponent= new UIComponent();
			_target.addChild(roundedMask);
			roundedMask.graphics.clear();
			roundedMask.graphics.beginFill(0x000000);
			roundedMask.mouseEnabled= false;

			switch(_side)
			{
				case ROUNDEDCORNER_BOTTOM:
					roundedMask.graphics.drawRoundRectComplex(0, 0, (_width==0)?_target.width:_width, (_height==0)? _target.height:_height, 0,0, _rad,_rad);
					break;
				case ROUNDEDCORNER_LEFT:
					roundedMask.graphics.drawRoundRectComplex(0, 0, (_width==0)?_target.width:_width, (_height==0)? _target.height:_height, _rad,0,_rad,0);
					break;
				case ROUNDEDCORNER_RIGHT:
					roundedMask.graphics.drawRoundRectComplex(0, 0, (_width==0)?_target.width:_width, (_height==0)? _target.height:_height, 0,_rad, 0,_rad);
					break;
				default:
					//defaults to top
					roundedMask.graphics.drawRoundRectComplex(0, 0, (_width==0)?_target.width:_width, (_height==0)? _target.height:_height, _rad,_rad, 0,0);
					break;
			}

			roundedMask.graphics.endFill();
			_target.mask = roundedMask;
		}

		//apply rounded corner effect to only one corner
		public static function applyRoundedCornersToCorner(_target:UIComponent,_corner:String = ROUNDEDCORNER_TOPLEFT,_rad:int=8, _width:Number=0,_height:Number=0):void
		{
			var roundedMask:UIComponent= new UIComponent();
			_target.addChild(roundedMask);
			roundedMask.graphics.clear();
			roundedMask.mouseEnabled= false;
			roundedMask.graphics.beginFill(0x000000);

			switch(_corner)
			{
				case ROUNDEDCORNER_TOPRIGHT:
					roundedMask.graphics.drawRoundRectComplex(0, 0, (_width==0)?_target.width:_width, (_height==0)? _target.height:_height, 0,_rad, 0,0);
					break;
				case ROUNDEDCORNER_BOTTOMLEFT:
					roundedMask.graphics.drawRoundRectComplex(0, 0, (_width==0)?_target.width:_width, (_height==0)? _target.height:_height, 0,0,_rad,0);
					break;
				case ROUNDEDCORNER_BOTTOMRIGHT:
					roundedMask.graphics.drawRoundRectComplex(0, 0, (_width==0)?_target.width:_width, (_height==0)? _target.height:_height, 0,0, 0,_rad);
					break;
				default:
					//defaults to upper left
					roundedMask.graphics.drawRoundRectComplex(0, 0, (_width==0)?_target.width:_width, (_height==0)? _target.height:_height,_rad,0, 0,0);
					break;
			}

			roundedMask.graphics.endFill();
			_target.mask = roundedMask;
		}
	}
}
Posted in AS3, Flex 3 | Leave a comment

Wolfram Rule #30 In HTML 5

Ok, HTML 5 is really easy and almost exactly like AS3. This did not take long at all.

Here is the link to the full example

My Wolfram Rule 30 Class in HTML 5 / JavaScript:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="language" content="en" />
	<meta name="description" content="" />
	<meta name="keywords" content="" />
	<script type="text/javascript" src="com/wolfram/WolframRule30.js"></script>
</head>
<body>
<canvas id="Wolfram30" width="1000" height="1500"></canvas>
<script>

var contextID = "Wolfram30";
var w = new Rule30(contextID);
w.generate();
</script>
</body>
</html>

/*
	a tile changes if the 3x3 region centered on it contains at least 5 walls.

	e.g., if the grid contains a 1 it goes to 0 or vice versa
 */
function Rule30(contextID)
{
	//set important member vars
	this.x_size = 250;
	this.y_size = 250;
	this.xCutoff = 5; //after the number is higher than this, we switch to walls
	this.yCutoff = 2; //under this, its a blank space
	this.generationCount = 140; //how many times we cycle the automata before we are happy

	//our canvas and context we will need for drawing on
	this.canvas  = document.getElementById(contextID)
    this.context = this.canvas.getContext("2d");

	//details on the squares
	this.SquareSize = 4; //how big are we drawing these squares?

	//generate the grid
	this.generate = function()
	{
		//step 1, create a random map
		var newGrid = this.CreateBlankGrid();

		//set the seed
		newGrid[Math.round(this.x_size / 2)][0] = 1;

		var ticks = this.generationCount; //how many times we will run this simulation
		var rowIndex = 0; //the current row we are drawing on in our pyramid of love

		//run the simulator
		for(var i = this.generationCount; i > 0; i--)
		{
			this.context.clearRect ( 0 , 0 , 800, 600 );//blank the context
			this.UpdateGrid(newGrid, rowIndex+1); //update our grid with the new automata
			this.Draw(newGrid); //draw our new automata
			rowIndex++;
		}
	}

	 //initialize the map grid with a bunch of blank squares
	 this.CreateBlankGrid =  function()
	 {
		 //x and y indices for the grid
		 var grid = new Array(this.x_size); //array of 20 columns	

		for (var i = 0; i < this.x_size; i++)
		{
			grid[i] = new Array(this.y_size); 

			//fill the width with random objects
			for (var j = 0; j < this.y_size; j++)
			{
				//fill with randoms
				grid[i][j] = 0;
			}
		}
		return grid;
	 }

	 //adds a new row of cellular automate to the next row
	 this.UpdateGrid = function(grid, rowIndex)
	 {
		 var i = 0;
		 var j = 0;
		 var state = new Array(3); //captures the current three squares we are examining

		 //we run this one row at a time
		 for (i  = 0; i < this.x_size; i++)
		 {
			 if ((i < (this.x_size - 1)) && (i > 0))
			 {
				 state[0] = grid[i - 1][rowIndex-1];
				 state[1] = grid[i][rowIndex-1];
				 state[2] = grid[i + 1][rowIndex-1];
				 this.HandleState(grid,state, i, rowIndex);
			 }
		 }
	 } 

	 //performs an action based on the state
	 //we apply the rules for the wolfram rule 30 right here
	 this.HandleState = function(grid, state, xi, yi)
	 {
		 // 111 & 000
		 if ((state[0] == state[1]) && (state[1] == state[2])&&(state[0]==state[2]) )
		 grid[xi][yi] = 0;
		 else if ((state[0] == 1) && (state[1] ==1) && (state[2]==0))
		 grid[xi][yi] = 0;
		 else if ((state[0] == 1) && (state[1] ==0) && (state[2]==1))
		 grid[xi][yi] = 0;
		 else
		 grid[xi][yi] = 1;
	 }

	 //draw all the squares in the grid
	 this.Draw = function(grid)
	 {
		 for (var i = 0; i < this.x_size; i++)
		 {
			 for (var j = 0; j < this.y_size; j++)
			 {

				//document.write("Now Drawing..." + grid[i][j]);
				 if (grid[i][j] == 1)
				 {
					// document.write("Now Drawing..." + grid[i][j]);
					// document.write("x : "+ (this.SquareSize * i) + " y: "+(this.SquareSize * j) + " size: "+  this.SquareSize);
					 this.context.fillRect(((this.SquareSize) * i), ((this.SquareSize) * j), this.SquareSize, this.SquareSize);
				 }
			}
		 }
	 }
}
Posted in Cellular Automata, HTML 5, JavaScript | Leave a comment

Dynamic Shadows In AS3






I saw someone wrote a demo that showed off an effect like this so I decided to try to write one of my own.

Posted in 3D Engines, AS3, Dynamic Lighting | Leave a comment

Introducing The Photo Smasher






View the full version of the photo smasher here: Photo Smasher

Posted in AS3, Box2D, Photo Effects, Pixel Art | Leave a comment

Pixelate Yourself In Box2D






Posted in AS3, Box2D, Photo Effects, Pixel Art | Leave a comment

Simple Box2D Ragdoll Class






There is a ragdoll class that comes standard with the AS3 Box2D library…but it is a bit messy and incomprehensible for me so I modified it to be more workable:

package com.physics.primitives
{
import Box2D.Collision.b2Bound;
import Box2D.Collision.Shapes.b2CircleShape;
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.Joints.b2JointDef;
import Box2D.Dynamics.Joints.b2RevoluteJointDef;
import com.physics.World;
/**
* ...
* @author ScanPlayGames
*/
public class Ragdoll extends PhysicsObject
{
private var circleShape:b2CircleShape;

//head
public var head:b2Body;
protected var torso:b2Body;
protected var torso2:b2Body;
protected var torso3:b2Body;

//arms
protected var upperArmL:b2Body;
protected var upperArmR:b2Body;
protected var lowerArmL:b2Body;
protected var lowerArmR:b2Body;

//legs
protected var upperLegL:b2Body;
protected var upperLegR:b2Body;
protected var lowerLegL:b2Body;
protected var lowerLegR:b2Body;

//joints
protected var jointDef:b2RevoluteJointDef;

//fixture details
protected var density:Number = .1;

public function Ragdoll(_x:Number, _y:Number)
{
super();
var startX:Number = _x;
var startY:Number =_y ;
bodyDef.type = b2Body.b2_dynamicBody;

// CREATE THE HEAD
circleShape = new b2CircleShape( 12.5 / World.scale );

//fixture def
fixtureDef.shape = circleShape;
fixtureDef.density =  density;
fixtureDef.friction = 0.4;
fixtureDef.restitution = 0.3;

//create the body def
bodyDef.position.Set(startX / World.scale, startY / World.scale);

//create the head
head = World.CreateBody(bodyDef);
head.CreateFixture(fixtureDef);

//CREATE THE TORSO
boxDef.SetAsBox(15 / World.scale, 10 / World.scale);

//create the fixture
fixtureDef.shape = boxDef;
fixtureDef.density =  density;
fixtureDef.friction = 0.4;
fixtureDef.restitution = 0.1;

//set position
bodyDef.position.Set(startX / World.scale, (startY + 28) / World.scale);
torso = World.CreateBody(bodyDef);
torso.CreateFixture(fixtureDef);

// THE SECOND TORSO SEGMENT
boxDef = new b2PolygonShape();
boxDef.SetAsBox(15 / World.scale, 10 / World.scale);

//create the fixture
fixtureDef.shape = boxDef;
bodyDef.position.Set(startX / World.scale, (startY + 43) / World.scale);

torso2 = World.CreateBody(bodyDef);
torso2.CreateFixture(fixtureDef);

//CREAT THE THIRD TORSO SEGMENT
boxDef.SetAsBox(15 / World.scale, 10 / World.scale);
fixtureDef.shape = boxDef;
bodyDef.position.Set(startX / World.scale, (startY + 58) / World.scale);
torso3 = World.CreateBody(bodyDef);
torso3.CreateFixture(fixtureDef);

// CREATE THE UPPER ARM
fixtureDef.density =  density;
fixtureDef.friction = 0.4;
fixtureDef.restitution = 0.1;

// L
boxDef = new b2PolygonShape();
boxDef.SetAsBox(18 / World.scale, 6.5 / World.scale);
fixtureDef.shape =boxDef;
bodyDef.position.Set((startX - 30) / World.scale, (startY + 20) / World.scale);
upperArmL = World.CreateBody(bodyDef);
upperArmL.CreateFixture(fixtureDef);

// R
boxDef = new b2PolygonShape();
boxDef.SetAsBox(18 / World.scale, 6.5 / World.scale);
fixtureDef.shape = boxDef;
bodyDef.position.Set((startX + 30) / World.scale, (startY + 20) / World.scale);
upperArmR = World.CreateBody(bodyDef);
upperArmR.CreateFixture(fixtureDef);

// LowerArm
fixtureDef.density =  density;
fixtureDef.friction = 0.4;
fixtureDef.restitution = 0.1;

// L
boxDef = new b2PolygonShape();
boxDef.SetAsBox(17 / World.scale, 6 / World.scale);
fixtureDef.shape = boxDef;
bodyDef.position.Set((startX - 57) / World.scale, (startY + 20) / World.scale);
lowerArmL = World.CreateBody(bodyDef);
lowerArmL.CreateFixture(fixtureDef);

// R
boxDef = new b2PolygonShape();
boxDef.SetAsBox(17 / World.scale, 6 / World.scale);
fixtureDef.shape = boxDef;
bodyDef.position.Set((startX + 57) / World.scale, (startY + 20) / World.scale);
lowerArmR = World.CreateBody(bodyDef);
lowerArmR.CreateFixture(fixtureDef);

// UpperLeg
fixtureDef.density =  density;
fixtureDef.friction = 0.4;
fixtureDef.restitution = 0.1;

// L
boxDef = new b2PolygonShape();
boxDef.SetAsBox(7.5 / World.scale, 22 / World.scale);
fixtureDef.shape = boxDef;
bodyDef.position.Set((startX - <img src='http://scanplaygames.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> / World.scale, (startY + 85) / World.scale);
upperLegL = World.CreateBody(bodyDef);
upperLegL.CreateFixture(fixtureDef);
// R
boxDef = new b2PolygonShape();
boxDef.SetAsBox(7.5 / World.scale, 22 / World.scale);
fixtureDef.shape =boxDef;
bodyDef.position.Set((startX + <img src='http://scanplaygames.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> / World.scale, (startY + 85) / World.scale);
upperLegR = World.CreateBody(bodyDef);
upperLegR.CreateFixture(fixtureDef);

// LowerLeg
fixtureDef.density =  density;
fixtureDef.friction = 0.4;
fixtureDef.restitution = 0.1;

// L
boxDef = new b2PolygonShape();
boxDef.SetAsBox(6 / World.scale, 20 / World.scale);
fixtureDef.shape = boxDef;
bodyDef.position.Set((startX - <img src='http://scanplaygames.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> / World.scale, (startY + 120) / World.scale);
lowerLegL = World.CreateBody(bodyDef);
lowerLegL.CreateFixture(fixtureDef);

// R
boxDef = new b2PolygonShape();
boxDef.SetAsBox(6 / World.scale, 20 / World.scale);
fixtureDef.shape = boxDef;
bodyDef.position.Set((startX + <img src='http://scanplaygames.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> / World.scale, (startY + 120) / World.scale);
lowerLegR = World.CreateBody(bodyDef);
lowerLegR.CreateFixture(fixtureDef);

// JOINTS
jointDef = new b2RevoluteJointDef();
jointDef.enableLimit = true;

// Head to shoulders
jointDef.lowerAngle = -40 / (180/Math.PI);
jointDef.upperAngle = 40 / (180/Math.PI);
jointDef.Initialize(torso, head, new b2Vec2(startX / World.scale, (startY + 15) / World.scale));
World.world.CreateJoint(jointDef);

// Upper arm to shoulders
// L
jointDef.lowerAngle = -85 / (180/Math.PI);
jointDef.upperAngle = 130 / (180/Math.PI);
jointDef.Initialize(torso, upperArmL, new b2Vec2((startX - 18) / World.scale, (startY + 20) / World.scale));
World.world.CreateJoint(jointDef);
// R
jointDef.lowerAngle = -130 / (180/Math.PI);
jointDef.upperAngle = 85 / (180/Math.PI);
jointDef.Initialize(torso, upperArmR, new b2Vec2((startX + 18) / World.scale, (startY + 20) / World.scale));
World.world.CreateJoint(jointDef);

// Lower arm to upper arm
// L
jointDef.lowerAngle = -130 / (180/Math.PI);
jointDef.upperAngle = 10 / (180/Math.PI);
jointDef.Initialize(upperArmL, lowerArmL, new b2Vec2((startX - 45) / World.scale, (startY + 20) / World.scale));
World.world.CreateJoint(jointDef);
// R
jointDef.lowerAngle = -10 / (180/Math.PI);
jointDef.upperAngle = 130 / (180/Math.PI);
jointDef.Initialize(upperArmR, lowerArmR, new b2Vec2((startX + 45) / World.scale, (startY + 20) / World.scale));
World.world.CreateJoint(jointDef);

// Shoulders/stomach
jointDef.lowerAngle = -15 / (180/Math.PI);
jointDef.upperAngle = 15 / (180/Math.PI);
jointDef.Initialize(torso, torso2, new b2Vec2(startX / World.scale, (startY + 35) / World.scale));
World.world.CreateJoint(jointDef);

// Stomach/hips
jointDef.Initialize(torso2, torso3, new b2Vec2(startX / World.scale, (startY + 50) / World.scale));
World.world.CreateJoint(jointDef);

// Torso to upper leg
// L
jointDef.lowerAngle = -25 / (180/Math.PI);
jointDef.upperAngle = 45 / (180/Math.PI);
jointDef.Initialize(torso3, upperLegL, new b2Vec2((startX - <img src='http://scanplaygames.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> / World.scale, (startY + 72) / World.scale));
World.world.CreateJoint(jointDef);
// R
jointDef.lowerAngle = -45 / (180/Math.PI);
jointDef.upperAngle = 25 / (180/Math.PI);
jointDef.Initialize(torso3, upperLegR, new b2Vec2((startX + <img src='http://scanplaygames.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> / World.scale, (startY + 72) / World.scale));
World.world.CreateJoint(jointDef);

// Upper leg to lower leg
// L
jointDef.lowerAngle = -25 / (180/Math.PI);
jointDef.upperAngle = 115 / (180/Math.PI);
jointDef.Initialize(upperLegL, lowerLegL, new b2Vec2((startX - <img src='http://scanplaygames.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> / World.scale, (startY + 105) / World.scale));
World.world.CreateJoint(jointDef);
// R
jointDef.lowerAngle = -115 / (180/Math.PI);
jointDef.upperAngle = 25 / (180/Math.PI);
jointDef.Initialize(upperLegR, lowerLegR, new b2Vec2((startX + <img src='http://scanplaygames.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> / World.scale, (startY + 105) / World.scale));
World.world.CreateJoint(jointDef);
}

public override function Update():void{}

public override function Destroy():void
{
Main.physicsObjects.splice(Main.physicsObjects.indexOf(this), 1);

World.DestroyBody(head);
World.DestroyBody(torso);
World.DestroyBody(torso2);
World.DestroyBody(torso3);

World.DestroyBody(upperArmL);
World.DestroyBody(upperArmR);
World.DestroyBody(lowerArmL);
World.DestroyBody(lowerArmR);

World.DestroyBody(upperLegL);
World.DestroyBody(upperLegR);
World.DestroyBody(lowerLegL);
World.DestroyBody(lowerLegR);
}
}

}

Here is my code for the physics object class:

package com.physics.primitives
{
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2FixtureDef;
import flash.display.Bitmap;
import flash.display.MovieClip;
import flash.display.Sprite;
import com.physics.World;
/**
* ...
* @author ScanPlayGames
*/
public class PhysicsObject extends Sprite
{
protected var body:b2Body;
protected var bodyDef:b2BodyDef;
protected var fixtureDef:b2FixtureDef;
protected var boxDef:b2PolygonShape;

public function PhysicsObject()
{
//create the box definition
boxDef                 = new b2PolygonShape();
bodyDef                = new b2BodyDef();
fixtureDef             = new b2FixtureDef();
}

public function GetBody():b2Body
{
return body;
}

public function Destroy():void
{
World.DestroyBody(body);

Main.physicsObjects.splice(Main.physicsObjects.indexOf(this), 1);

if (Main.sprite.contains(this))
Main.sprite.removeChild(this);
}

public function Update():void
{
x = body.GetPosition().x * World.scale;
y = body.GetPosition().y * World.scale;
rotation  = body.GetAngle() * (180 / Math.PI);
}
}
}
Posted in AS3, Box2D | Leave a comment

Box2D Hangman






Posted in Uncategorized | 1 Comment

How Much Damage Does An Angry Bird Do?

credit: IGN

This is how you determine how much damage a projectile causes an object in AS3 / Box2D.

  • Create A Custom Contact Listener
  • Apply it to your world
  • Update the Post Solve method to find the collission impulse between the two objects
  • Have each object handle the result of the collission

Here is how you apply a custom contact listener to your world:

m_world.SetContactListener(new CustomContactListener (this));

Here is source for the custom contact listener:CustomListener

package
{
	import Box2D.Collision.b2Collision;
	import Box2D.Collision.b2Manifold;
	import Box2D.Collision.Shapes.b2CircleShape;
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Collision.Shapes.b2Shape;
	import Box2D.Common.Math.b2Transform;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2ContactImpulse;
	import Box2D.Dynamics.b2ContactListener;
	import Box2D.Dynamics.b2Fixture;
	import Box2D.Dynamics.Contacts.b2Contact;
	import com.physics.blocks.PhysicsObject;
	import com.core.*;
	import Box2D.Common.Math.b2Math;
	import com.game.GameObject;

	/**
	 * ...
	 * @author ScanPlay Games
	 *
	 * This is a modified contact listener that we mainly use to determine the strength of weapon / projectile / bird hits on
	 * other objects and then decide how much damage was done.
	 */
	public class CustomListener extends b2ContactListener
	{
		private var gameLevel:GameLevel;
		private var objectA:PhysicsObject; //a custom class which has both a physics body and the art for that body
		private var objectB:PhysicsObject;

		public function CustomListener(g:GameLevel)
		{
			gameLevel = g;
		}

		private var i:int;

		override public function PostSolve(contact:b2Contact, impulse:b2ContactImpulse):void
		{
			// get how many contact points were detected
			var count:int = contact.GetManifold().m_pointCount;

			var collissionImpulse:Number = 0.0;
			for (i = 0; i < count; i++)
			{
				collissionImpulse = b2Math.Max(collissionImpulse, impulse.normalImpulses[i]);
			}

			//dispatch a message indicating the collission if the force is over a certain threshold
			if (collissionImpulse > .1)
			{
                                //since we can't figure out what the objects are, we have to have a function that
                                //queries the list of game objects and checks the body against each body in the game object list
				objectA = GameLevel.GetObjectByBody(contact.GetFixtureA().GetBody());
				objectB = GameLevel.GetObjectByBody(contact.GetFixtureB().GetBody());	

                                //now that we have figured out which objects have collided, we have custom collission handling
				if (objectA && objectB)
				{
                                        //now that we have both bodies, have a custom function that handles the collission
                                        //pass in the collission impulse
					objectA.handleCollission(objectB, collissionImpulse);
					objectB.handleCollission(objectA, collissionImpulse);
				}
			}
		}
	}
}
Posted in AS3, Box2D | Leave a comment

Simple Loading Screen In AS3 / FlashDevelop

This is how to do a simple loading screen in flash using FlashDevelop.

  • First, create a project using Flash Develop and the “AS3 Project with Preloader” setting
  • Next, embed your artwork as below into the Preloader.as
  • Next, create a loading bar out of a rect and scale it against the progress of the bytes loaded.
  • Thats it!
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.utils.getDefinitionByName;

/**
* ...
* @author ScanPlayGames
*/
public class Preloader extends MovieClip
{
[Embed(source = 'com/core/factory/assets/library.swf', symbol = 'Splash')]
private const _library:Class;
private var loader:Sprite;
private var rect:Sprite;
private var w:Number = 265;

public function Preloader()
{
addEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
// show loader
loader = Sprite(new _library());
addChild(loader);

//create a loading bar
rect = new Sprite();
rect.graphics.beginFill(0x760101, 1);
rect.graphics.drawRect(0, 0, 200, 30);
rect.graphics.endFill();
addChild(rect);
rect.x = 284;
rect.y = 379;
loader.x -= 40;
loader.y += 30;
}

private function progress(e:ProgressEvent):void
{
// update loader
rect.width = (e.bytesLoaded  / e.bytesTotal) * w;
}

private function checkFrame(e:Event):void
{
if (currentFrame == totalFrames)
{
removeEventListener(Event.ENTER_FRAME, checkFrame);
startup();
}
}

private function startup():void
{
// hide loader

stop();
removeChild(loader);
removeChild(rect);
loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
var mainClass:Class = getDefinitionByName("Main") as Class;
addChild(new mainClass() as DisplayObject);
}
}
}
Posted in AS3 | Leave a comment

Android Sprite Animation With A Thread, Blitting And SurfaceView

Source Code Source Code

This is the worlds simplest example on how to combine a Thread, Sprite Blitting and SurfaceView in good working order. I had this post up before, but WordPress decided to wipe out my database so here we go again.

Posted in Android | Leave a comment