-
import com.actionsnippet.qbox.*;
-
import Box2D.Common.Math.*;
-
import Box2D.Collision.Shapes.*;
-
-
[SWF(width = 800, height = 600, backgroundColor = 0x000000)]
-
-
var sim:QuickBox2D = new QuickBox2D(this);
-
-
sim.createStageWalls();
-
-
var boxNum:int = 8;
-
var boxes:Array = [];
-
var xp:Number, yp:Number, w:Number;
-
for (var i:int = 0; i<boxNum; i++){
-
xp = 3 + Math.random() * 10;
-
yp = 4 + i * 2;
-
w = 2 + Math.random() * 3;
-
boxes.push(sim.addBox({x: xp, y: yp, width: w, height: 0.5, density:0, groupIndex:-1, fillColor:0xFF6532}));
-
}
-
-
var char:QuickObject = sim.addBox({x:2, y:18, width:1, height:2, allowSleep:false, groupIndex:-1, fillColor:0x0099CC});
-
-
sim.start();
-
-
var charVel:b2Vec2;
-
var charVelAng:Number;
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
-
charVel = char.body.GetLinearVelocity();
-
charVelAng = char.body.GetAngularVelocity();
-
var filter:b2FilterData;
-
-
if (key[Keyboard.RIGHT]){
-
charVel.x += 1
-
char.body.SetLinearVelocity(charVel);
-
charVelAng += 1;
-
char.body.SetAngularVelocity(charVelAng);
-
}
-
if (key[Keyboard.LEFT]){
-
charVel.x -=1;
-
char.body.SetLinearVelocity(charVel);
-
charVelAng -= 1;
-
char.body.SetAngularVelocity(charVelAng);
-
}
-
if (key[Keyboard.UP] && sim.w.GetContactCount()> 0){
-
charVel.y = -10;
-
trace(charVel.y);
-
char.body.SetLinearVelocity(charVel);
-
charVelAng *= 0.8;
-
char.body.SetAngularVelocity(charVelAng);
-
}
-
for (var i:int = 0; i<boxes.length; i++){
-
var rect:Rectangle = char.userData.getRect(this);
-
if (rect.bottom / 30 <boxes[i].y){
-
filter = boxes[i].shape.GetFilterData();
-
filter.groupIndex = 1;
-
boxes[i].shape.SetFilterData(filter);
-
}else{
-
filter = boxes[i].shape.GetFilterData();
-
if (filter.groupIndex != -1){
-
filter.groupIndex = -1;
-
boxes[i].shape.SetFilterData(filter);
-
}
-
}
-
}
-
}
-
-
var key:Object = new Object();
-
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
-
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
-
function onKeyPressed(evt:KeyboardEvent):void{ key[evt.keyCode] = true; }
-
function onKeyReleased(evt:KeyboardEvent):void{ key[evt.keyCode] = false; }
NOTE: This requires the QuickBox2D mini-library
This snippet shows how to use groupIndex to create simple platforms for a character to jump on. A few people have asked me about this so I figured I'd post about it. It works by altering the groupIndex of each platform based on the characters position - if the character is below a platform, both the character and the platform have groupIndex -1 (meaning they will not collide). As soon as the character is above a platform, the platforms groupIndex is set to 1 (allowing the character to walk on the platform despite having just passed through it.
It's also worth noting that I'm using b2World.GetContactCount() to make sure it's ok to jump... if there were more going on in the demo you'd need to check a specific contact point(s), but since the only collision that could happen involves the character this works nicely...