Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

560
Vistas
How do you create global variables in Microsoft MakeCode for micro:bit?

I'm currently trying to create snake inside a micro:bit. But one problem is when I use an if statement to detect if the snake is touching an apple it'll work once but then I call the function which says

function createApple() {
    let apple = game.createSprite(randint(0, 5), randint(0, 5));
}

Creating variables with a let statement makes it a local variable only working with other statements inside of the function, but when I try and make a variable using var it says I must define variables using a let statement. Is there any way to create global variables inside functions and other code blocks that work? (Please answer it in a way that won't be subjective to my situation and will reach across many similar scenarios for people including myself)

(Here's my if statement if it's somehow required by somebody answering)

loops.everyInterval(1, function () {
    if (snakeHead.isTouching(apple)) {
        apple.delete();
        createApple();
    }
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can declare the variable outside of the function to make it global. If you start with an empty global variable, you have to add the type of the variable behind :

let apple : game.LedSprite

function createApple() {
    apple = game.createSprite(randint(0, 5), randint(0, 5));
}

If you don't know the type of the variable (in this case game.LedSprite) you can find it by hovering the mouse over the function createSprite, see screenshot:

enter image description here

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda