Saltearse al contenido

Ayuda con el espaciado de visualización de números

💡 ¿Tienes problemas ensamblando bloques de Scratch? ¿No sabes cómo implementar la lógica del código? 🚀 Obtener Ayuda Ahora

IS

Isopod7

Publicado el 5 de agosto de 2025 • Intermedio

🔢 Ayuda con una visualización de números

Estoy creando una visualización de números que se hace más pequeña cuando se agregan números para encajar en un cuadrado y mantenerse centrada. El problema es que los espacios del eje x entre los números no están coordinados.

Quiero que los números estén espaciados uniformemente y se ajusten automáticamente al ancho total. ¿Cómo puedo implementar esto en Scratch?

SC

ScratchCoder_Pro

Respondió 2 horas después • ⭐ Mejor Respuesta

¡Excelente pregunta @Isopod7! Las visualizaciones dinámicas de números con espaciado adecuado requieren un enfoque reflexivo. Aquí tienes una solución completa:

🔧 Paso 1: Configurar Variables

Primero, crea estas variables para tu sistema de visualización:

    when flag clicked
set [Number to Display v] to [12345]
set [Display Width v] to [200]
set [Display Height v] to [50]
set [Digit Count v] to [0]
set [Digit Width v] to [0]
set [Digit Spacing v] to [0]
set [Start X v] to [0]
  

📏 Paso 2: Calcular Tamaño Dinámico

Calcula el tamaño óptimo basado en el número de dígitos:

    // Contar dígitos y calcular tamaño
set [Digit Count v] to (length of (Number to Display))
set [Digit Width v] to ((Display Width) / (Digit Count))
if <(Digit Width) > [30]> then
set [Digit Width v] to [30]
end
set [Digit Spacing v] to ((Display Width) / (Digit Count))
  

🎯 Paso 3: Calcular Posición Inicial

Determina la posición inicial para el centrado:

    // Calcular posición inicial para centrado
set [Total Width v] to ((Digit Count) * (Digit Spacing))
set [Start X v] to (0 - ((Total Width) / [2]))
  

🔢 Paso 4: Crear Clones de Dígitos

Crea clones para cada dígito con espaciado adecuado:

    delete all clones of [Digit Sprite v]
set [Current Position v] to (Start X)
repeat (Digit Count)
set [Current Digit v] to (letter (Current Position + 1) of (Number to Display))
create clone of [Digit Sprite v]
change [Current Position v] by (Digit Spacing)
end
  

➖ Paso 5: Manejar Números Negativos

Para números negativos, agrega manejo especial:

    // Verificar números negativos
if <(letter [1] of (Number to Display)) = [-]> then
set [Has Negative v] to [true]
set [Number to Display v] to (letter [2] to (length of (Number to Display)) of (Number to Display))
else
set [Has Negative v] to [false]
end

// Crear clon de signo menos si es necesario
if <(Has Negative) = [true]> then
set [Current Digit v] to [-]
set [Current Position v] to ((Start X) - (Digit Spacing))
create clone of [Digit Sprite v]
end
  

🎨 Paso 6: Comportamiento del Clon

En el sprite de dígito, agrega este comportamiento de clon:

    when I start as a clone
go to x: (Current Position) y: [0]
set size to ((Digit Width) / [20] * [100]) %
switch costume to (Current Digit)
show
  

🔄 Paso 7: Sistema de Visualización Completo

Combina todo en un script principal:

    define Update Number Display (number)
set [Number to Display v] to (number)
delete all clones of [Digit Sprite v]

// Manejar números negativos
if <(letter [1] of (number)) = [-]> then
set [Has Negative v] to [true]
set [Number to Display v] to (letter [2] to (length of (number)) of (number))
else
set [Has Negative v] to [false]
end

// Calcular tamaño y espaciado
set [Digit Count v] to (length of (Number to Display))
if <(Has Negative) = [true]> then
change [Digit Count v] by [1]
end

set [Digit Spacing v] to ((Display Width) / (Digit Count))
set [Digit Width v] to ((Digit Spacing) * [0.8])
if <(Digit Width) > [30]> then
set [Digit Width v] to [30]
end

set [Total Width v] to ((Digit Count) * (Digit Spacing))
set [Start X v] to (0 - ((Total Width) / [2]))

// Crear signo menos
if <(Has Negative) = [true]> then
set [Current Digit v] to [-]
set [Current Position v] to (Start X)
create clone of [Digit Sprite v]
change [Start X v] by (Digit Spacing)
end

// Crear dígitos
set [Digit Index v] to [1]
repeat (length of (Number to Display))
set [Current Digit v] to (letter (Digit Index) of (Number to Display))
set [Current Position v] to ((Start X) + ((Digit Index - 1) * (Digit Spacing)))
create clone of [Digit Sprite v]
change [Digit Index v] by [1]
end
  

💡 Consejos Profesionales

  • Dimensionamiento Responsivo: Usa tamaños mínimos y máximos para los dígitos
  • Transiciones Suaves: Agrega animaciones de deslizamiento al cambiar tamaños
  • Números Decimales: Extiende el sistema para puntos decimales
  • Diferentes Fuentes: Crea diferentes disfraces para varios estilos

¡Este sistema ajusta automáticamente el tamaño y espaciado basado en el número de dígitos mientras mantiene un centrado adecuado! 🎯

IS

Isopod7

Respondió 30 minutos después

@ScratchCoder_Pro ¡Eso es fantástico! ¡Muchas gracias! 🎉

Logré que el sistema básico funcionara. Una pregunta rápida: ¿cómo puedo hacer que la visualización también sea compatible con números negativos? ¿Y por qué el espaciado entre números de dos dígitos es tan grande en tu solución?



¿Necesitas más ayuda con la programación en Scratch? ¡Únete a nuestra comunidad y obtén orientación experta para tus proyectos! ✨🚀