Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!