Hello,
I am creating my first game with the library Box2D Flash AS3 and I have some problems ...
The game is a kind of tetris. I generate rectangles of different sizes falling one by one on a platform and I can control their x direction during their fall with the mouse or increase the speed of their fall by pressing the mouse.
Currently I can generate the rectangles and when I 'presses the mouse it falls on the platform. I'm stuck for the rest: (
look at my code :
So I have three questions:
- how to detect when a rectangle has completed its falling touching the platform or another rectangle in order to launch next rectangle?
- how to move in x the rectangle during his fall with the movement of the mouse?
- How accelerated the fall of a rectangle by pressing the mouse?
Thank you for your answers.
I am creating my first game with the library Box2D Flash AS3 and I have some problems ...
The game is a kind of tetris. I generate rectangles of different sizes falling one by one on a platform and I can control their x direction during their fall with the mouse or increase the speed of their fall by pressing the mouse.
Currently I can generate the rectangles and when I 'presses the mouse it falls on the platform. I'm stuck for the rest: (
look at my code :
Code:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class Main extends Sprite
{
public var m_world:b2World;
public var m_iterations:int = 10;
public var m_timeStep:Number = 1.0 / 30.0;
public var body:b2Body;
public var bodyDef:b2BodyDef;
public var boxDef:b2PolygonDef;
public var ran_cadeau:uint;
public var num_box:int;
public function Main()
{
// constructor code
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100, -100);
worldAABB.upperBound.Set(100, 100);
var gravity:b2Vec2 = new b2Vec2(0.0,10.0);
var doSleep:Boolean = true;
m_world = new b2World(worldAABB,gravity,doSleep);
//debug
var debug_draw:b2DebugDraw = new b2DebugDraw();
var debug_sprite:Sprite = new Sprite();
addChild(debug_sprite);
debug_draw.m_sprite = debug_sprite;
debug_draw.m_drawScale = 30;
debug_draw.m_fillAlpha = 0.5;
debug_draw.m_lineThickness = 1;
debug_draw.m_drawFlags = b2DebugDraw.e_shapeBit;
m_world.SetDebugDraw(debug_draw);
//end debug;
add_sol();
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mousePressed);
stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoved);
}
public function add_sol()
{
bodyDef = new b2BodyDef();
bodyDef.position.Set(8.3,16.5);
boxDef = new b2PolygonDef();
boxDef.SetAsBox(6,0.5);
boxDef.friction = 0.3;
boxDef.density = 0;
body = m_world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
var the_sol:Sol=new Sol();
addChild(the_sol);
the_sol.y = 510;
the_sol.x = 250;
//add_cadeau();
}
public function add_cadeau()
{
num_box++;
bodyDef = new b2BodyDef();
bodyDef.position.x = mouseX / 30;
bodyDef.position.y = -2;
boxDef = new b2PolygonDef();
ran_cadeau = Math.random() * 4 + 1;
switch (ran_cadeau)
{
case 1 :
boxDef.SetAsBox(0.8,0.8);
break;
case 2 :
boxDef.SetAsBox(1.6,0.8);
break;
case 3 :
boxDef.SetAsBox(1,1.3);
break;
case 4 :
boxDef.SetAsBox(1.3,0.65);
break;
case 5 :
boxDef.SetAsBox(1.3,1.3);
break;
}
boxDef.density = 1.0;
boxDef.friction = 0.5;
boxDef.restitution = 0.2;
bodyDef.userData = new Cadeau();
bodyDef.userData.gotoAndStop(ran_cadeau);
bodyDef.userData.num = num_box;
body = m_world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
addChild(bodyDef.userData);
bodyDef.userData.x = bodyDef.position.x * 30;
bodyDef.userData.y = bodyDef.position.y * 30;
}
public function mousePressed(e:MouseEvent)
{
//i want increase the fall of the current box
}
public function mouseMoved(e:MouseEvent)
{
//i want change x direction of the current box
}
public function Update(e:Event):void
{
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);
}
}
}
}
}
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class Main extends Sprite
{
public var m_world:b2World;
public var m_iterations:int = 10;
public var m_timeStep:Number = 1.0 / 30.0;
public var body:b2Body;
public var bodyDef:b2BodyDef;
public var boxDef:b2PolygonDef;
public var ran_cadeau:uint;
public var num_box:int;
public function Main()
{
// constructor code
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100, -100);
worldAABB.upperBound.Set(100, 100);
var gravity:b2Vec2 = new b2Vec2(0.0,10.0);
var doSleep:Boolean = true;
m_world = new b2World(worldAABB,gravity,doSleep);
//debug
var debug_draw:b2DebugDraw = new b2DebugDraw();
var debug_sprite:Sprite = new Sprite();
addChild(debug_sprite);
debug_draw.m_sprite = debug_sprite;
debug_draw.m_drawScale = 30;
debug_draw.m_fillAlpha = 0.5;
debug_draw.m_lineThickness = 1;
debug_draw.m_drawFlags = b2DebugDraw.e_shapeBit;
m_world.SetDebugDraw(debug_draw);
//end debug;
add_sol();
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mousePressed);
stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoved);
}
public function add_sol()
{
bodyDef = new b2BodyDef();
bodyDef.position.Set(8.3,16.5);
boxDef = new b2PolygonDef();
boxDef.SetAsBox(6,0.5);
boxDef.friction = 0.3;
boxDef.density = 0;
body = m_world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
var the_sol:Sol=new Sol();
addChild(the_sol);
the_sol.y = 510;
the_sol.x = 250;
//add_cadeau();
}
public function add_cadeau()
{
num_box++;
bodyDef = new b2BodyDef();
bodyDef.position.x = mouseX / 30;
bodyDef.position.y = -2;
boxDef = new b2PolygonDef();
ran_cadeau = Math.random() * 4 + 1;
switch (ran_cadeau)
{
case 1 :
boxDef.SetAsBox(0.8,0.8);
break;
case 2 :
boxDef.SetAsBox(1.6,0.8);
break;
case 3 :
boxDef.SetAsBox(1,1.3);
break;
case 4 :
boxDef.SetAsBox(1.3,0.65);
break;
case 5 :
boxDef.SetAsBox(1.3,1.3);
break;
}
boxDef.density = 1.0;
boxDef.friction = 0.5;
boxDef.restitution = 0.2;
bodyDef.userData = new Cadeau();
bodyDef.userData.gotoAndStop(ran_cadeau);
bodyDef.userData.num = num_box;
body = m_world.CreateBody(bodyDef);
body.CreateShape(boxDef);
body.SetMassFromShapes();
addChild(bodyDef.userData);
bodyDef.userData.x = bodyDef.position.x * 30;
bodyDef.userData.y = bodyDef.position.y * 30;
}
public function mousePressed(e:MouseEvent)
{
//i want increase the fall of the current box
}
public function mouseMoved(e:MouseEvent)
{
//i want change x direction of the current box
}
public function Update(e:Event):void
{
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);
}
}
}
}
}
So I have three questions:
- how to detect when a rectangle has completed its falling touching the platform or another rectangle in order to launch next rectangle?
- how to move in x the rectangle during his fall with the movement of the mouse?
- How accelerated the fall of a rectangle by pressing the mouse?
Thank you for your answers.