【问题标题】:How to show image when a specific key is pressed in p5js?在p5js中按下特定键时如何显示图像?
【发布时间】:2021-08-25 06:29:57
【问题描述】:

我是 p5js 的新手 :)

当用户在键盘上按1-0之间的某个键时,会播放某种声音并显示随机图像。我试过这个...

function keyPressed() {
   if (key === '2') {
        songA.play()
        image(imgB1, random(windowWidth), random(windowHeight), img.width/2, img.height/2); 
   } 

但是 p5js 只在绘图函数中读取“图像”。然后我在 draw 函数中尝试了 keyisdown 并尝试为每个数字输入 keyCodes,例如我相信 1 是 49,但它没有用。

  if (keyIsDown(49)) {
     songA.play()
     image(imgB1,random(windowWidth), random(windowHeight), img.width/2, img.height/2); 
}}

我基本上是从类似于 p5js 的 openprocessing 中复制 https://openprocessing.org/sketch/441971,但实际上并非如此。帮忙?

谢谢!

【问题讨论】:

    标签: keyboard keyboard-events


    【解决方案1】:
    1. 确保您的图片已在 setup() 函数中预加载
    2. 当你显示图像时,你引用的是“img”而不是 imgB1,这意味着 p5js 不知道显示图像的大小,所以我假设它默认为 0。要解决这个问题,只需将 img 替换为imgB1.

    【讨论】:

      猜你喜欢
      • 2021-05-25
      • 1970-01-01
      • 2020-10-01
      • 2018-06-20
      • 1970-01-01
      • 2013-10-28
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多