Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // some variables that we gonna use in this demo
- var initId = 0;
- var player = function(){
- this.object = null;
- this.canJump = false;
- };
- var world;
- var ctx;
- var canvasWidth;
- var canvasHeight;
- var keys = [];
- // HTML5 onLoad event
- //Event.observe(window, 'load', function() {
- $(document).ready(function() { //jquery doc ready
- world = createWorld(); // box2DWorld
- var canvasElm = document.getElementById('game');
- ctx = canvasElm.getContext('2d'); // 2
- canvasWidth = parseInt(canvasElm.width);
- canvasHeight = parseInt(canvasElm.height);
- initGame(); // 3
- step(); // 4
- // 5
- //window.addEventListener('keydown',handleKeyDown,true);
- //window.addEventListener('keyup',handleKeyUp,true);
- });
- function initGame(){
- // create 2 big platforms
- createBox(world, 60, 230, 60, 180, true, 'ground'); //biggest
- /* createBox(world, 560, 360, 50, 50, true, 'ground'); /*
- // create small platforms
- for (var i = 0; i < 5; i++){
- createBox(world, 150+(80*i), 360, 5, 40+(i*15), true, 'ground');
- }
- */
- // create player ball
- /*
- var ballSd = new b2CircleDef();
- ballSd.density = 0.1;
- ballSd.radius = 12;
- ballSd.restitution = 0.5;
- ballSd.friction = 1;
- ballSd.userData = 'player';
- var ballBd = new b2BodyDef();
- ballBd.linearDamping = .03;
- ballBd.allowSleep = false;
- ballBd.AddShape(ballSd);
- ballBd.position.Set(20,0);
- player.object = world.CreateBody(ballBd);
- */
- var bodyDef = new b2BodyDef;
- var fixDef = new b2FixtureDef;
- bodyDef.type = b2Body.b2_dynamicBody;
- fixDef.shape = new b2CircleShape(
- 12 //radius
- );
- bodyDef.position.x = Math.random() * 10;
- bodyDef.position.y = Math.random() * 10;
- /*
- console.log('fixDef');
- console.log(fixDef);
- */
- player.object = world.CreateBody(bodyDef).CreateFixture(fixDef);
- var debugDraw = new b2DebugDraw();
- debugDraw.SetSprite(document.getElementById("game").getContext("2d"));
- debugDraw.SetDrawScale(30.0);
- debugDraw.SetFillAlpha(0.3);
- debugDraw.SetLineThickness(1.0);
- debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
- world.SetDebugDraw(debugDraw);
- }
- function step() {
- console.log('stepping');
- var stepping = false;
- var timeStep = 1.0/60;
- var iteration = 1;
- // 1
- world.Step(timeStep, iteration);
- // 2
- ctx.clearRect(0, 0, canvasWidth, canvasHeight);
- drawWorld(world, ctx);
- // 3
- //world.DrawDebugData();
- setTimeout('step()', 2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement