Skip to content

Commit ba626dd

Browse files
committed
Improved parallax background
1 parent d182764 commit ba626dd

3 files changed

Lines changed: 35 additions & 17 deletions

File tree

package-lock.json

Lines changed: 30 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/background.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Player from './player/player'
2-
31
export default class Background extends Phaser.GameObjects.TileSprite {
42
constructor(scene: Phaser.Scene) {
53
super(scene, 0, 0, 0, 0, 'background')
@@ -9,13 +7,14 @@ export default class Background extends Phaser.GameObjects.TileSprite {
97
}
108

119
adjustPosition() {
10+
const imgHeight = 648
11+
this.setScale(this.scene.cameras.main.height / imgHeight)
1212
this.x = this.scene.cameras.main.centerX
1313
this.y = this.scene.cameras.main.centerY
1414
this.width = this.scene.cameras.main.width
15-
this.height = this.scene.cameras.main.height
1615
}
1716

18-
parallax(player: Player) {
19-
this.tilePositionX += player.body.velocity.x / 1200
17+
parallax() {
18+
this.tilePositionX = this.scene.cameras.main.worldView.x * 0.2
2019
}
2120
}

src/scenes/mainScene.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export default class MainScene extends Phaser.Scene {
126126
}
127127

128128
update() {
129-
this.background.parallax(this.player)
129+
this.background.parallax()
130130
this.controls.update()
131131
this.enemiesGroup.update()
132132
this.player.update(this.cursors, this.controls)

0 commit comments

Comments
 (0)