在不影响他人的情况下处理、镜像或翻转图像

Processing, Mirroring or Flipping an Image without affecting others

我正在使用 Javascript 开发一个带有 Processing 的迷你平台游戏,比如马里奥。

我正在用箭头或 WASD 移动我的角色,我想知道在镜像角色/图像时是否有 scale(-1,1); 的替代方法。 (它默认向右看,或者当你按 D 时,当你按 A 时它向左翻转)。

如果这是最好或最简单的方法,我也想知道如何使缩放方法不影响所有其他图像,因为我想把一些平台,但随着角色的移动,它们会不断翻转......

我也在收听任何与 Processing 和 Javascript 使用精灵/声音相关的信息。我尝试了一些库,但它们只有在我切换到 Java 模式时才有效。

提前致谢, 马拉.

由于之前的 post,我使用了比例镜像。 Processing mirror image over x axis?

"dreta" 是右手,"esquerre" 是左手,两者都由 WASD 或按键控制,将每个布尔值按下时更改为真,释放时更改为假。 我没有 post 所有代码,但这是我想要修复的基本动作。

   void draw() {
       pushMatrix();
       if (iniciar == true) {
       inici();
    }
    if (dreta == true && esquerre != true) {
      movDret(backgroundimg[2]);
    }
    if (esquerre == true && dreta != true) {
      movEsquerre(backgroundimg[2]);
    }
  }

   void platformndBackground(PImage b){
      background(b);
      popMatrix();
      image(imgGrass[0], 50, 50);
      if (esquerre == true) {
        scale(-1,1);
      }
   }

   void inici() {
      movDret(backgroundimg[2]);
      iniciar = false;
   }

   void movDret(PImage b) {
      //Colisió extrem Esquerre.
     if (posicio > 1024-imgJugador[5].width) {
       posicio = posicio - imgJugador[tipusMoviment].width/2;
       movEsquerre(backgroundimg[2]);
     } else {
       bothMoviments(b);
       image(imgJugador[tipusMoviment], posicio, posicioSalt);
       posicio = posicio + 3;
     }
   }

    void movEsquerre(PImage b) {
      //Colisió extrem Dret.
      if (posicio < 0) {
        posicio = posicio + imgJugador[tipusMoviment].width/2;
        popMatrix();
        movDret(backgroundimg[2]);
      } else {
        bothMoviments(b);
        image(imgJugador[tipusMoviment], ((-imgJugador[tipusMoviment].width)-posicio), posicioSalt);
        posicio = posicio - 3;
      }
    }

    void bothMoviments(PImage b) {
      if (esquerre == true) {
        scale(-1, 1);
      }else{
        popMatrix();
      }
        if (tipusMoviment < imgJugador.length-1) {
          tipusMoviment++;
        } else {
          tipusMoviment = 5;
        }
        platformndBackground(b);
    }

如果您不想镜像您的 sprite,您可以只使用两组 sprite:一组用于向右行驶,一组用于向左行驶。

但是,如果您使用 scale() 函数来镜像您的精灵,则需要使用 pushMatrix()popMatrix(),这样比例就不会影响所有其他精灵。像这样:

public void draw(){

   background(0);

   pushMatrix(); //save current "default" matrix
      scale(-1,1); //scale the matrix
      image(img,-img.width,img.height); //draw the image using the scaled matrix
   popMatrix(); //go back to the saved "default" matrix

   //draw non-mirrored sprites
   image(img2,img2.width,img2.height);
}

可以在参考资料中找到更多信息 here