Skip to content

Commit e97a207

Browse files
committed
Added in Circle to the Ninja physics system.
1 parent 3e93f24 commit e97a207

4 files changed

Lines changed: 2657 additions & 5 deletions

File tree

examples/wip/ninja2.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
2+
// var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
3+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
4+
5+
function preload() {
6+
7+
game.load.spritesheet('ninja-tiles', 'assets/physics/ninja-tiles.png', 128, 128, 34);
8+
game.load.image('a', 'assets/sprites/firstaid.png');
9+
game.load.image('ball', 'assets/sprites/shinyball.png');
10+
11+
}
12+
13+
var sprite1;
14+
var cursors;
15+
16+
var tile1;
17+
var tile2;
18+
19+
var t;
20+
var running = false;
21+
22+
function create() {
23+
24+
game.stage.smoothed = true;
25+
26+
// Activate the Ninja physics system
27+
game.physics.startSystem(Phaser.Physics.NINJA);
28+
29+
// game.physics.ninja.gravity = 0.1;
30+
31+
sprite1 = game.add.sprite(102, 200, 'ball');
32+
33+
// Enable the physics body for the Ninja physics system
34+
// By default it will create an AABB body for the sprite
35+
game.physics.ninja.enableCircle(sprite1, sprite1.width / 2);
36+
37+
// But you can change it to either a Tile or a Circle
38+
tile1 = game.add.sprite(100, 500, 'ninja-tiles', 14);
39+
tile1.width = 100;
40+
tile1.height = 100;
41+
42+
game.physics.ninja.enableTile(tile1, 14);
43+
44+
cursors = game.input.keyboard.createCursorKeys();
45+
46+
}
47+
48+
function collisionHandler() {
49+
game.stage.backgroundColor = 0xff0000;
50+
}
51+
52+
function update() {
53+
54+
game.physics.ninja.collide(sprite1, tile1, collisionHandler, null, this);
55+
56+
// tile1.body.moveRight(1);
57+
58+
/*
59+
if (cursors.up.isDown && !running)
60+
{
61+
running = true;
62+
t = Date.now();
63+
}
64+
65+
sprite1.body.setZeroVelocity();
66+
67+
if (running)
68+
{
69+
sprite1.body.moveRight(100);
70+
71+
if (sprite1.body.x >= 200)
72+
{
73+
var ms = Date.now() - t;
74+
console.log('100px in ', ms);
75+
running = false;
76+
sprite1.body.setZeroVelocity();
77+
}
78+
}
79+
*/
80+
81+
// sprite1.body.setZeroVelocity();
82+
83+
if (cursors.left.isDown)
84+
{
85+
sprite1.body.moveLeft(20);
86+
}
87+
else if (cursors.right.isDown)
88+
{
89+
sprite1.body.moveRight(20);
90+
}
91+
92+
if (cursors.up.isDown)
93+
{
94+
sprite1.body.moveUp(20);
95+
}
96+
else if (cursors.down.isDown)
97+
{
98+
sprite1.body.moveUp(20);
99+
}
100+
101+
}
102+
103+
function render() {
104+
105+
game.debug.text(sprite1.body.shape.velocity.x, 32, 32);
106+
game.debug.text(sprite1.body.shape.velocity.y, 32, 64);
107+
game.debug.text(game.math.radToDeg(sprite1.body.angle), 32, 96);
108+
109+
// tile1.render(game.context, 'ninja-tiles');
110+
// tile2.render(game.context, 'ninja-tiles');
111+
112+
// game.debug.geom(sprite1.body, 'rgba(0,255,0,0.4)', true, 1);
113+
114+
// game.debug.geom(tile1, 'rgba(0,255,0,0.4)', true, 1);
115+
// game.debug.geom(tile1, 'rgba(0,255,0,0.4)', true, 1);
116+
117+
}

src/physics/ninja/Body.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
* @param {Phaser.Sprite} sprite - The Sprite object this physics body belongs to.
1616
* @param {number} [type=1] - The type of Ninja shape to create. 1 = AABB, 2 = Circle or 3 = Tile.
1717
* @param {number} [id=1] - If this body is using a Tile shape, you can set the Tile id here, i.e. Phaser.Physics.Ninja.Tile.SLOPE_45DEGpn, Phaser.Physics.Ninja.Tile.CONVEXpp, etc.
18+
* @param {number} [radius=16] - If this body is using a Circle shape this controls the radius.
1819
*/
19-
Phaser.Physics.Ninja.Body = function (system, sprite, type, id) {
20+
Phaser.Physics.Ninja.Body = function (system, sprite, type, id, radius) {
2021

2122
if (typeof type === 'undefined') { type = 1; }
2223
if (typeof id === 'undefined') { id = 1; }
24+
if (typeof radius === 'undefined') { radius = 16; }
2325

2426
/**
2527
* @property {Phaser.Sprite} sprite - Reference to the parent Sprite.
@@ -68,7 +70,7 @@ Phaser.Physics.Ninja.Body = function (system, sprite, type, id) {
6870
}
6971
else if (type === 2)
7072
{
71-
this.circle = new Phaser.Physics.Ninja.Circle(system, sprite.x, sprite.y, sprite.width, sprite.height);
73+
this.circle = new Phaser.Physics.Ninja.Circle(system, sprite.x, sprite.y, radius);
7274
this.shape = this.circle;
7375
}
7476
else if (type === 3)

0 commit comments

Comments
 (0)