Basic Filler engine with Box2D – part 2
2009-10-18 00:00:00 来源:WEB开发网Some time ago I published Basic Filler engine with Box2D – part 1, now it’s time to see the next step.
I am going to add enemies, the evil bouncing balls.
Obviously, enemies aren’t affected by gravity, and this can be made following the instructions published at Managing multiple gravities with Box2D.
The next, big, problem is enemies must have a constant speed, or at least a minumum speed, otherwise it will be easy to use your generated balls to collide with enemies and change their momentum slowing them down.
This is the part I fully commented on the script, at the moment I don’t manage player deaths, so you can create your red balls wherever you want.
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 colorballz extends Sprite {
public var m_dbgSprite;
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;
public var initX:Number=0.0;
public var initY:Number=0.0;
public var drawing:Boolean=false;
public var b:b2Body;
public var the_ball:ball;
public var the_enemy:enemy;
public function colorballz() {
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
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);
//Add enemies
for(var x:int=1;x<=3;x++){
// the first parameter is enemy's radius
addEnemy(10);
}
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) {
initX=e.localX;
initY=e.localY;
the_ball = new ball();
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) {
if (drawing) {
the_ball.x=mouseX;
the_ball.y=mouseY;
}
}
public function mouseReleased(e:MouseEvent) {
drawing=false;
addCircle( mouseX, mouseY, the_ball.width/2,the_ball);
}
public function addCircle(_x:Number, _y:Number, _radius:Number,ballclip:ball) {
var bd:b2BodyDef = new b2BodyDef();
var cd:b2CircleDef = new b2CircleDef();
var area:Number=Math.floor(_radius*_radius*Math.PI/25)/100;
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 addEnemy(_radius:Number) {
var x_speed:Number = Math.random()*10;
var bd:b2BodyDef = new b2BodyDef();
var cd:b2CircleDef = new b2CircleDef();
the_enemy = new(enemy);
cd.radius=Math.abs(_radius)/m_phys_scale;
cd.density=20;
// setting restitution to 1 should grant the enemy won't lose speed when bouncing
// on STATIC objects
cd.restitution=1;
cd.friction=2;
// randomly placing the enemy
bd.position.Set((Math.random()*300+50)/m_phys_scale, (Math.random()*300+50)/ m_phys_scale);
bd.userData=the_enemy;
bd.userData.name="enemy";
bd.userData.width=_radius*2;
bd.userData.height=_radius*2;
addChild(the_enemy);
b=m_world.CreateBody(bd);
b.CreateShape(cd);
b.SetMassFromShapes();
// assigning random speed to enemy
b.m_linearVelocity.x=x_speed;
b.m_linearVelocity.y=10-x_speed;
}
public function AddStaticBox(_x:Number,_y:Number,_halfwidth:Number,_halfheight:Number) {
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) {
// variable to handle enemy speed
var enemy_speed:Number;
// variable to handle the speed offset:it's the ratio between
// the real enemy speed and the minimum allowed speed
var speed_offset:Number;
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) {
if (bb.m_userData.name=="enemy") {
// determining enemy speed
enemy_speed=Math.abs(bb.GetLinearVelocity().x)+Math.abs(bb.GetLinearVelocity().y);
// if the speed is too slow... (lower than 10)
if (enemy_speed<10) {
// calculating the offset
speed_offset=10/enemy_speed;
// multiplying the horizontal and vertical components of the speed
// by the offset
bb.m_linearVelocity.x=bb.GetLinearVelocity().x*speed_offset;
bb.m_linearVelocity.y=bb.GetLinearVelocity().y*speed_offset;
}
}
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;
}
}
}
}
}
更多精彩
赞助商链接