Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
How to convert Khan academy code to p5.js

I'm using the https://editor.p5js.org/Josef37/sketches/RGDv9lHkK for convert to p5.js and I'm still don't know how to convert 100% correct, the picture won't show up when i convert myCode from the khanAcademy Original code to p5.js and can you also help me put the sound clack.wave when Block fall down in my code as well?

myCode

function preload() {
  blockImg = loadImage('block.png');
}

var Block = function(x, y) {
    this.x = x;
    this.y = y;
    this.img = loadImage("block.png");
    this.sticks = 0;
};


  
  
function setup() {
  createCanvas(windowWidth, windowHeight);

Block.prototype.draw = function() {
    image(this.img, this.x, this.y, 40, 40);
};
  
Block.prototype.hop = function() {
    this.img = getImage("block.png");
    this.y -= -44;
};

Block.prototype.fall = function() {
    this.img = getImage("block.png");
    this.y += 5;
};
}



function draw() {
  background(200);

var block = new Block(10, 300);
block.draw();

}

I'm using P5.js on this website >https://editor.p5js.org/Josef37/sketches/RGDv9lHkK <

https://www.khanacademy.org/computing/computer-programming/programming-games-visualizations/side-scroller/a/beaver-character Original Code from KhanAcademy

var Beaver = function(x, y) {
    this.x = x;
    this.y = y;
    this.img = getImage("creatures/Hopper-Happy");
    this.sticks = 0;
};

Beaver.prototype.draw = function() {
    image(this.img, this.x, this.y, 40, 40);
};

Beaver.prototype.hop = function() {
    this.img = getImage("creatures/Hopper-Jumping");
    this.y -= -44;
};

Beaver.prototype.fall = function() {
    this.img = getImage("creatures/Hopper-Happy");
    this.y += 5;//Down Gavity
};


var beaver = new Beaver(10, 300);
beaver.draw();

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem with your code is that you're trying to call the asynchronous loadImage() function within your constructor, which means the image will not be able to load before you attempt to draw the Block with .draw().

You already do check to make sure it has finished loading by putting an initial loadImage() call in your preload(), so the image will be loaded by the time your setup() and draw() functions run. This means you can just set the image to the preloaded one within your constructor:

var Block = function (x, y) {
  this.x = x;
  this.y = y;
  this.img = blockImg;
  this.sticks = 0;
};

Also, make sure to switch all your getImage()s to preloaded loadImage()s and their returned Objects, as getImage() does not exist within p5.js and using just loadImage() may give you the same problem you're having here.

about 4 years ago · Juan Pablo Isaza Relatório

0

Also, you have to have the file loaded into p5js. you have to run the program with the asset. In Khan academy they have it for you.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda