The command image() do not works

hello developers

I had a problem with the image() command

One answer is very important to me : _ (

My image does not load on the screen, I check the console but I have no errors, my image is prompted and live according to the console, I don't know if this problem is because my program has the P5.Play library or because the drawSprites() command, I use scenes in my program and always put the command image() in front of the background

Half of my code is below

var player
var target
var scene = 1
let img

function preload() {
  img = loadImage('assets/Pixel Studio/ButtonUnpressed.gif')
  
}

function setup(){
  createCanvas(960, 430)
  cursor('none')
  
  target = createSprite(850, 100)
  targetImg = loadImage('assets/128/dagger.png')
  target.addImage(targetImg)
  player = createSprite(150, 150)
  playerImg = loadImage('assets/128/dagger.png')
  player.addImage(playerImg)
  }

function draw(){

if (scene === 1) {
   background(230)
     image(img, 200, 200)

I am using the P5.play library, if you have experience in this library, please talk to me.

*No console errors.

*Image requested and maintained in console.

*I use scenes.

*I am available in the private chat.

*I really need answers.

  • Have a nice day.

Hi @Kevincroos ,

First simple question :

if (scene === 1) {

is this condition true?

Can you print the img variable in the setup() function? Are you sure you are not overriding it or hiding it with something else?

Try to make another simple sketch where you only load and display your image and see if that works. :wink:

1 Like

In front of is not clear…

This will give you a black background on top of your image and result in a black screen:

image(img);
background(0);

Read the reference:
https://p5js.org/reference/#/p5/background

:)

Yes, it’s a true condition…

I tryed your options :arrow_down:

I put img() in the Setup (but nothing happened), kept only one scene (no modification too), so I tried calling my image only, no sprites, element or other objects (but it dont has displayed), I think of my problem is about the program don’t search for my img variable from the preLoad() function, am I correct? It’s really a mess because when I called preLoad() for the first time, my image was displayed and it worked correctly.

Well, i always use

background(255);
image(img, 0, 0);

I guess you are confunded about my post “In front of the background”, sorry me😀.

But you got it help me too, i readed your reference (reference | p5.js) and i got it another ways to display my image(how sprite), thanks by your help too😁