Thankyou for the reply.
The problem is the I have the following code.
Main.as
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Stage;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
[SWF(width='500', height='500', backgroundColor='#02091D', frameRate='30')]
public class Main extends Sprite {
public var m_dbgSprite:Sprite;
public var m_world:b2World;
public var m_phys_scale:Number = 30;
public var m_timestep:Number = 1.0/30.0;
public var m_iterations:Number = 10.0;
//initial box coordinates when we first press mouse down
public var initX:Number = 0.0;
public var initY:Number = 0.0;
public var drawing:Boolean = false;
public var b:b2Body;
// the visual that the user see's
public var the_ball:Ball;
private var colorArray:Array = new Array(0xFFFF33, 0xFFFFFF, 0x79DCF4, 0xFF3333, 0xFFCC33, 0x99CC33);
public function Main() {
/*
A Box2D world needs three parameters: a b2AABB, gravity
and a Booleand deciding whether or not to let bodies sleep
when they are not being simulated.
This saves CPU so should always be left on :)
*/
var gravity:b2Vec2 = new b2Vec2(0,9.8);
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-1000,-1000);
worldAABB.upperBound.Set(1000,1000);
m_world = new b2World(worldAABB,gravity,true);
//Add our ground and walls
addStaticBox(250/m_phys_scale,510/m_phys_scale,250/m_phys_scale,10/m_phys_scale);
addStaticBox(250/m_phys_scale,-10/m_phys_scale,250/m_phys_scale,10/m_phys_scale);
addStaticBox(-10/m_phys_scale,250/m_phys_scale,10/m_phys_scale,250/m_phys_scale);
addStaticBox(510/m_phys_scale,250/m_phys_scale,10/m_phys_scale,250/m_phys_scale);
addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mousePressed);
stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoved);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseReleased);
}
public function mousePressed(e:MouseEvent) : void {
//Store initial X and Y position
initX = e.localX;
initY = e.localY;
var randomColorId:Number = Math.floor(Math.random()*colorArray.length);
the_ball = new Ball(50, colorArray[randomColorId]);
the_ball.x = mouseX;
the_ball.y = mouseY;
the_ball.width = 1;
the_ball.height = 1;
addChild(the_ball);
drawing = true;
}
public function mouseMoved(e:MouseEvent) : void {
if (drawing) {
the_ball.x = mouseX;
the_ball.y = mouseY;
}
}
public function mouseReleased(e:MouseEvent) : void {
drawing = false;
addCircle( mouseX, mouseY, the_ball.width/2,the_ball);
}
public function addCircle(_x:Number, _y:Number, _radius:Number, ballclip:Ball) : void {
var bd:b2BodyDef = new b2BodyDef();
var cd:b2CircleDef = new b2CircleDef();
var area:Number = Math.floor(_radius*_radius*Math.PI/25)/100;
// area is the % of the entire stage filled by the circle
// the entire stage area is 500*500 = 250000 pixels
// circle area is radius*radius*PI
// so the % is radius*radius*PI*100/250000
cd.radius = Math.abs(_radius)/m_phys_scale;
cd.density = 2;
cd.restitution = 0.7;
cd.friction = 2;
bd.position.Set(_x/m_phys_scale, _y/ m_phys_scale);
bd.userData = ballclip;
b = m_world.CreateBody(bd);
b.CreateShape(cd);
b.SetMassFromShapes();
}
public function addStaticBox(_x:Number,_y:Number,_halfwidth:Number,_halfheight:Number) : void {
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(_x,_y);
var boxDef:b2PolygonDef = new b2PolygonDef();
boxDef.SetAsBox(_halfwidth,_halfheight);
boxDef.density = 0.0;
var body:b2Body = m_world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
}
public function update(e:Event) : void {
//We need to do this to simulate physics
if (drawing) {
the_ball.width+=2;
the_ball.height+= 2;
}
m_world.Step(m_timestep,m_iterations);
for (var bb:b2Body=m_world.m_bodyList; bb; bb=bb.m_next) {
if (bb.m_userData is Sprite) {
bb.m_userData.x=bb.GetPosition().x * 30;
bb.m_userData.y=bb.GetPosition().y * 30;
bb.m_userData.rotation=bb.GetAngle() * 180 / Math.PI;
}
}
}
}
}
In Ball.as file.
package
{
import flash.display.Sprite;
import flash.geom.Matrix;
public class Ball extends Sprite
{
private var mc:Sprite;
public function Ball(radius:Number, color:Number = 0x660033)
{
super();
mc = new Sprite();
setRegistrationPoint( mc, mc.width >> 1, mc.height >> 1, true);
mc.graphics.lineStyle(1, 0x000000);
mc.graphics.beginFill(color);
mc.graphics.drawCircle(0, 0, radius);
mc.graphics.endFill();
addChild(mc);
}
public function setRegistrationPoint(s:Sprite, regx:Number, regy:Number, showRegistration:Boolean) : void
{
//translate movieclip
s.transform.matrix = new Matrix(1, 0, 0, 1, -regx, -regy);
//registration point.
if (showRegistration)
{
var mark:Sprite = new Sprite();
mark.graphics.lineStyle(1, 0x000000);
mark.graphics.moveTo(-5, -5);
mark.graphics.lineTo(5, 5);
mark.graphics.moveTo(-5, 5);
mark.graphics.lineTo(5, -5);
s.addChild(mark);
}
}
}
}
And in the Air.mxml file I have this.
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="init()">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
public var main:Main;
public function init():void{
//Alert.show();
main = new Main();
}
]]>
</fx:Script>
<fx:Declarations>
</fx:Declarations>
</s:WindowedApplication>
But nothing is coming. And I am getting some errors also. I have imported the library. So there is no problem with the library. I have just started using Flash Builder and Actionscript.Please correct me if I am doing something wrong.