Skip to content

Commit a65f21c

Browse files
committed
created platform and player, camera follows player.
1 parent 7b3ada7 commit a65f21c

File tree

7 files changed

+139
-42
lines changed

7 files changed

+139
-42
lines changed

game/gameScene.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const Phaser = require("phaser");
2+
3+
class GameScene extends Phaser.Scene {
4+
constructor(){
5+
super("GameScene");
6+
}
7+
preload(){
8+
this.canvas = this.sys.game.canvas; // Sets the canvas property for ease of acess
9+
}
10+
create(){
11+
12+
13+
// Player
14+
this.player = this.add.rectangle(300, 200,50,70, 0xff0000);
15+
this.physics.add.existing(this.player);
16+
this.player.body.setCollideWorldBounds(true);
17+
18+
// Platforms
19+
let platformsArr = [];
20+
platformsArr.push(this.add.rectangle(300, 400, 100, 30, 0x0000ff));
21+
platformsArr.push(this.add.rectangle(500, 550, 200, 30, 0x0000ff));
22+
// this.add.rectangle(300, 100, 100, 30, 0x0000ff);
23+
// this.add.rectangle(300, 100, 100, 30, 0x0000ff);
24+
// this.add.rectangle(300, 100, 100, 30, 0x0000ff);
25+
let platforms = this.physics.add.staticGroup();
26+
platforms.addMultiple(platformsArr);
27+
28+
this.physics.add.collider(this.player, platforms);
29+
30+
this.cursors = this.input.keyboard.createCursorKeys();
31+
32+
33+
// Camera
34+
this.cameras.main.setBounds(0,0, 3000, this.canvas.height);
35+
this.cameras.main.startFollow(this.player);
36+
this.cameras.main.setBackgroundColor('#ccccff');
37+
}
38+
update(){
39+
if(this.cursors.left.isDown){
40+
this.player.body.x = this.player.body.x-10;
41+
} else if(this.cursors.right.isDown){
42+
this.player.body.x = this.player.body.x+10;
43+
} if((this.cursors.space.isDown || this.cursors.up.isDown) && this.player.body.onFloor()){
44+
this.player.body.setVelocityY(-900);
45+
}
46+
}
47+
}
48+
49+
module.exports = GameScene;

game/index.js

+8-36
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,22 @@
1+
const Phaser = require("phaser");
2+
const GameScene = require("./gameScene");
3+
14
let width = window.innerWidth;
25
let height = window.innerHeight;
36

7+
48
var config = {
59
type: Phaser.AUTO,
610
width,
711
height,
812
physics: {
913
default: 'arcade',
1014
arcade: {
11-
gravity: { y: 200 }
15+
gravity: { y: 1500 },
16+
debug: true
1217
}
1318
},
14-
scene: {
15-
preload: preload,
16-
create: create
17-
}
19+
scene: GameScene
1820
};
1921

20-
var game = new Phaser.Game(config);
21-
22-
function preload ()
23-
{
24-
this.load.setBaseURL('http://labs.phaser.io');
25-
26-
this.load.image('sky', 'assets/skies/space3.png');
27-
this.load.image('logo', 'assets/sprites/phaser3-logo.png');
28-
this.load.image('red', 'assets/particles/red.png');
29-
}
30-
31-
function create ()
32-
{
33-
this.add.image(400, 300, 'sky');
34-
35-
var particles = this.add.particles('red');
36-
37-
var emitter = particles.createEmitter({
38-
speed: 100,
39-
scale: { start: 1, end: 0 },
40-
blendMode: 'NORMAL'
41-
});
42-
43-
var logo = this.physics.add.image(400, 100, 'logo');
44-
45-
logo.setVelocity(100, 200);
46-
logo.setBounce(1, 1);
47-
logo.setCollideWorldBounds(true);
48-
49-
emitter.startFollow(logo);
50-
}
22+
var game = new Phaser.Game(config);

package-lock.json

+76
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"scripts": {
77
"dev": "concurrently \"npm:dev:server\" \"npm:dev:javascript\"",
88
"dev:server": "node server/index.js",
9-
"dev:javascript":"webpack --config webpack.dev.config.js --watch",
10-
9+
"dev:javascript": "webpack --config webpack.dev.config.js --watch",
1110
"production": "TODO: Will do the production compile commands later",
1211
"build": "webpack --config webpack.config.js"
1312
},
@@ -25,6 +24,7 @@
2524
"dependencies": {
2625
"concurrently": "^6.3.0",
2726
"express": "^4.17.1",
27+
"phaser": "^3.55.2",
2828
"webpack": "^5.58.2",
2929
"webpack-cli": "^4.9.0"
3030
}

server/index.htm

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
<body>
2020
<noscript>Sadly :( your browser doesn't support the game</noscript>
2121
<!-- <canvas id="canvas">Sadly :( your browser doesn't support the game</canvas> -->
22-
<script src="phaser.min.js"></script>
23-
<script src="main-game.js"></script>
22+
<script src="static/main-game.js"></script>
2423
</body>
2524
</html>

server/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ const app = express();
33
const path = require("path");
44
const port = 3000;
55

6-
app.use(express.static("static"));
6+
app.use("/static", express.static("static"));
7+
app.use("/assets", express.static("assets"))
8+
79
app.get('/', (req, res) => {
810
res.sendFile(path.join(__dirname, "index.htm"));
911
});

static/phaser.min.js

-1
This file was deleted.

0 commit comments

Comments
 (0)