Skip to content

Commit 10bc22b

Browse files
committed
Motion helper functions added to the ArcadePhysics class.
1 parent 128bdcc commit 10bc22b

4 files changed

Lines changed: 518 additions & 49 deletions

File tree

examples/motion1.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
(function () {
14+
15+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
16+
17+
function preload() {
18+
game.load.image('car', 'assets/sprites/car90.png');
19+
game.load.image('ship', 'assets/sprites/xenon2_ship.png');
20+
game.load.image('baddie', 'assets/sprites/space-baddie.png');
21+
}
22+
23+
var car;
24+
var ship;
25+
var total;
26+
var aliens;
27+
28+
function create() {
29+
30+
aliens = [];
31+
32+
for (var i = 0; i < 50; i++)
33+
{
34+
var s = game.add.sprite(game.world.randomX, game.world.randomY, 'baddie');
35+
s.name = 'alien' + s;
36+
s.body.collideWorldBounds = true;
37+
s.body.bounce.setTo(1, 1);
38+
s.body.velocity.setTo(10 + Math.random() * 40, 10 + Math.random() * 40);
39+
aliens.push(s);
40+
}
41+
42+
car = game.add.sprite(400, 300, 'car');
43+
car.anchor.setTo(0.5, 0.5);
44+
car.body.collideWorldBounds = true;
45+
car.body.bounce.setTo(0.5, 0.5);
46+
car.body.allowRotation = true;
47+
48+
ship = game.add.sprite(400, 400, 'ship');
49+
ship.body.collideWorldBounds = true;
50+
ship.body.bounce.setTo(0.5, 0.5);
51+
52+
}
53+
54+
function update() {
55+
56+
car.body.velocity.x = 0;
57+
car.body.velocity.y = 0;
58+
car.body.angularVelocity = 0;
59+
60+
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
61+
{
62+
car.body.angularVelocity = -200;
63+
}
64+
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
65+
{
66+
car.body.angularVelocity = 200;
67+
}
68+
69+
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
70+
{
71+
car.body.velocity.copyFrom(game.physics.velocityFromAngle(car.angle, 300));
72+
}
73+
74+
game.physics.collideGroup(aliens);
75+
// total = game.physics.overlap(ship);
76+
77+
}
78+
79+
function render() {
80+
81+
// game.debug.renderQuadTree(game.physics.quadTree);
82+
// game.debug.renderRectangle(ship.body.bounds);
83+
84+
// game.debug.renderText('total: ' + total.length, 32, 50);
85+
86+
// game.debug.renderText('up: ' + ship.body.touching.up, 32, 70);
87+
// game.debug.renderText('down: ' + ship.body.touching.down, 32, 90);
88+
// game.debug.renderText('left: ' + ship.body.touching.left, 32, 110);
89+
// game.debug.renderText('right: ' + ship.body.touching.right, 32, 130);
90+
91+
}
92+
93+
})();
94+
95+
</script>
96+
97+
</body>
98+
</html>

examples/quadtree2.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@ function preload() {
2525

2626
function create() {
2727

28-
// game.world.setSize(2000, 2000);
29-
3028
aliens = [];
3129

32-
for (var i = 0; i < 10; i++)
30+
for (var i = 0; i < 50; i++)
3331
{
3432
var s = game.add.sprite(game.world.randomX, game.world.randomY, 'baddie');
3533
s.name = 'alien' + s;
3634
s.body.collideWorldBounds = true;
3735
s.body.bounce.setTo(1, 1);
38-
s.body.velocity.setTo(10 + Math.random() * 10, 10 + Math.random() * 10);
36+
s.body.velocity.setTo(10 + Math.random() * 40, 10 + Math.random() * 40);
3937
aliens.push(s);
4038
}
4139

@@ -47,13 +45,6 @@ function create() {
4745

4846
function update() {
4947

50-
// total = game.physics.overlap(ship);
51-
52-
for (var i = 0; i < aliens.length; i++)
53-
{
54-
game.physics.separate(ship.body, aliens[i].body);
55-
}
56-
5748
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
5849
{
5950
ship.body.velocity.x -= 2;
@@ -72,13 +63,18 @@ function update() {
7263
ship.body.velocity.y += 2;
7364
}
7465

66+
game.physics.collideGroup(aliens);
67+
// total = game.physics.overlap(ship);
68+
7569
}
7670

7771
function render() {
7872

7973
game.debug.renderQuadTree(game.physics.quadTree);
8074
game.debug.renderRectangle(ship.body.bounds);
8175

76+
// game.debug.renderText('total: ' + total.length, 32, 50);
77+
8278
game.debug.renderText('up: ' + ship.body.touching.up, 32, 70);
8379
game.debug.renderText('down: ' + ship.body.touching.down, 32, 90);
8480
game.debug.renderText('left: ' + ship.body.touching.left, 32, 110);

0 commit comments

Comments
 (0)