Skip to content

Commit

Permalink
fix(gameobjects): fix score width when text becomes long
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Jan 29, 2025
1 parent a1ddce7 commit 22c310b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/gameobjects/score.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import { addReward } from '.'

let score: Score

const CHARACTER_WIDTH = 40

export function addScore() {
const textbox = add([
rect(70, 70),
const box = add([
rect(CHARACTER_WIDTH, 60),
pos(50, 50),
anchor('center'),
color(255, 255, 255),
fixed(),
z(Layer.Foreground),
])

const score = textbox.add([
const score = box.add([
text('0', { size: 48 }),
anchor('center'),
color(0, 0, 0),
Expand All @@ -38,6 +40,8 @@ export function incrementScore(value = 1) {
const newScore = parseInt(score.text, 10) + value
score.text = newScore.toString()

score.parent!.width = CHARACTER_WIDTH * score.text.length

if (newScore === gameState.reward.score) {
gameState.reward.increment += 1
gameState.reward.score += gameState.reward.increment
Expand Down

0 comments on commit 22c310b

Please sign in to comment.