当我旋转图像时,它会离开屏幕 [英] When i rotate an image it goes off screen

查看:67
本文介绍了当我旋转图像时,它会离开屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将图像旋转90度,但是当我离开屏幕时(在窗口左侧),这是我的代码:

im trying to rotate an image 90 degrees but when i do it goes off screen (to the left of the window) here is my code:

这是我的图片的链接: http://imgur.com/gallery/pQ85Z

现在我只是想让它在按"r"键时旋转,但是此后我需要使其在碰到屏幕边缘时旋转.

right now im just trying to get it to rotate when i press 'r' but after this i need to make it rotate when it hits the edge of the screen.

PImage head;
int fizzyX = 400;
int fizzyY = 50;
int movementX=0;
int movementY=0;
float x;

void setup() {
  size(800,800);
  background(255);
  head = loadImage("Fizzy.PNG");
}

void collisionDetection() {
  if (fizzyX == (750)) {
    movementX=-1;
    key = 'a';
  }
}



void keyPressed() {
  if (key == 'a' || key == 'A') {
    movementX = -1;
    movementY = 0;
  }
  if (key == 'd' || key == 'D') {
    movementX = 1;
    movementY = 0;
  }
  if (key == 'w' || key == 'W') {
    movementX = 0;
    movementY = -1;
  }
  if (key == 's' || key == 'S') {
    movementX = 0;
    movementY = 1;
  }
  if (key == 'r') {
    x = PI/2;
  }
}

void draw() {
  rotate(x);
  background(255);
  imageMode(CENTER);
  image(head,fizzyX,fizzyY);
  fizzyX+=movementX;
  fizzyY+=movementY;
  keyPressed();
  collisionDetection();
  translate(width/2,height/2);

}

推荐答案

rotate()函数围绕原点执行旋转,该原点默认为0,0.这会使您的图像围绕窗口的左上角旋转,从而使图像偏离屏幕.

The rotate() function performs rotation around the origin, which by default is at 0,0. This causes your image to rotate around the upper-left corner of the window, which causes your image to go off the screen.

如果要围绕图像的中心旋转图像,则必须首先将原点移动到图像的中心.您可以使用 translate()函数执行此操作,但是必须之前执行此操作.

If you want to rotate the image around its center, then you first have to move the origin to the center of the image. You do this using the translate() function, but you have to do it before you to the rotation.

将其放在一起,看起来像这样:

Putting it together, it would look like this:

void draw() {

  background(255);

  imageMode(CENTER);
  translate(fizzyX, fizzyY);
  rotate(x);

  image(head,0,0);

  fizzyX+=movementX;
  fizzyY+=movementY;
  collisionDetection();

}

还请注意,您不应手动调用 keyPressed()函数.处理将为您调用.

Also note that you should not manually call the keyPressed() function. Processing will call it for you.

这篇关于当我旋转图像时,它会离开屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆