Skip to content

Commit c9d07c7

Browse files
committed
Testing out new Body structure
1 parent 128c714 commit c9d07c7

3 files changed

Lines changed: 1026 additions & 281 deletions

File tree

examples/wip/gravity.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
2+
3+
function preload() {
4+
game.load.image('arrow', 'assets/sprites/arrow.png');
5+
6+
}
7+
8+
var sprite;
9+
10+
var setGravityToPointXY = function(game,sprite,power, radiusLow,radiusHigh,pointX,pointY){
11+
var pointXY = new Phaser.Point(pointX,pointY);
12+
var tempDistance = Phaser.Point.distance(sprite,pointXY);
13+
if(tempDistance>radiusLow&&tempDistance<radiusHigh){
14+
var rotation = game.physics.angleBetween(sprite,pointXY);
15+
//console.log(" ROTATION " + rotation);
16+
sprite.body.gravity.y = Math.round(Math.sin(rotation)*1000)/1000 * (power);
17+
sprite.body.gravity.x = Math.round(Math.cos(rotation)*1000)/1000 * (power);
18+
}
19+
20+
};
21+
22+
function create() {
23+
24+
game.stage.backgroundColor = '#0072bc';
25+
26+
sprite = game.add.sprite(100, 100, 'arrow');
27+
sprite.anchor.setTo(0.5, 0.5);
28+
sprite.body.canSleep = false;
29+
30+
}
31+
32+
function update() {
33+
setGravityToPointXY(game,sprite,20,-0,8000,100,300);
34+
35+
}
36+
37+
function render() {
38+
game.debug.renderBodyInfo(sprite,30,32);
39+
}

0 commit comments

Comments
 (0)