2016-06-12 26 views
1

忽略通過在適當的時間提供適當的力量來實際平衡極點的任務(該部分沒有問題),沒有人有關於如何設置任務的基本指導Phaser中的極點平衡?如果這使得它更容易,我也有基於Phaser的Box2D插件。Phaser(Box2D)中的基本2D極點平衡設置

基本上我正在尋找要創建的對象類型(例如,主體,關節),創建/初始化過程以及在任一方向施加力的過程。對我而言,這些力量最初是不正確的,我只是不確定如何在Phaser中構建我想要的場景。

我覺得這樣的事情在Phaser中應該很簡單,但目前對我來說並不是那樣。

回答

1

使用迴轉關節和我的出發點簡易力量 Box2D的插件的例子,事實證明它是不是太困難可言:

,從他們的例子基本的移相器/ Box2D的設置(例如,800x600 ...),這裏有一些代碼放入createupdate方法。基本上我只是創建一個靜態地面體,兩個動態物體:手推車和立杆,然後使用revoluteJoint方法將兩個動態物體連接在一起。

function create() { 

    // Enable Box2D physics 
    game.physics.startSystem(Phaser.Physics.BOX2D); 
    game.physics.box2d.debugDraw.joints = true; 
    game.physics.box2d.setBoundsToWorld(); 
    game.physics.box2d.gravity.y = 500; 

    ground = new Phaser.Physics.Box2D.Body(this.game, null, game.world.centerX, 575, 0); // game, sprite, cx, cy, density [static/kinematic/dynamic], world 
    ground.setRectangle(800, 50, 0, 0, 0); // width, height, offsetx, offsety, angle (rads) 

    cart = new Phaser.Physics.Box2D.Body(this.game, null, game.world.centerX, 543); 
    cart.setRectangle(60, 10, 0, 0, 0); 
    cart.mass = 1; 

    pole = new Phaser.Physics.Box2D.Body(this.game, null, game.world.centerX, 495); 
    pole.setRectangle(4, 100, 0, 0, 0); 
    pole.mass = 0.1; 
    pole.linearDamping = 4; // makes the pole fall slower (heavy air resistance) 

    //bodyA, bodyB, ax, ay, bx, by, motorSpeed, motorTorque, motorEnabled, lowerLimit, upperLimit, limitEnabled 
    game.physics.box2d.revoluteJoint(cart, pole, 0, -5, 0, 50); 

    pole.angle = 5; // so it starts out falling to the right 

    //Set up arrow keys for input 
    cursors = game.input.keyboard.createCursorKeys(); 

    // track the angle of the pole 

    game.add.text(5, 5, 'Pole balancing.', { fill: '#ffffff', font: '14pt Arial' }); 
    caption1 = game.add.text(5, 30, 'Angle: ' + pole.angle, { fill: '#dddddd', font: '10pt Arial' }); 
} 

function update() { 

    caption1.text = 'Angle: ' + pole.angle; 

    if (cursors.left.isDown) { 
     cart.applyForce(-5,0); 
    } 

    if (cursors.right.isDown) { 
     cart.applyForce(5,0); 
    } 
}