我正在快速接近我的高中毕业项目的几个截止日期,所以你可以给出的任何建议都会很棒。我的项目是编写电影代码。我们的想法是使用处理来移动像令牌这样的字符,并使用代码来制作像雨和火这样的东西。现在我发现移动图像和/或旋转它们非常困难。这对我的项目来说是一个巨大的冲击。我对此的当前代码是经过屠杀和复杂的,但现在就是这样。 女主角的形象,径向:https://www.dropbox.com/s/x3gvendnaeftapj/radialoutlinel.png
/*
Radialrolling
*/
PImage img; // Declare variable "a" of type PImage
float ballX = 10;
float ballY = 200;
float h = 300;
//create a variable for speed
float speedY = 2; // spped set to 0 in order to test rotating. Necessary for rolling motion
float speedX = 0.;
void setup() {
size(800,800);
smooth();
noStroke();
background(0, 0, 0);// Load the image into the program
// change the mode we draw circles so they are
// aligned in the top left
ellipseMode(CORNER);
img = loadImage("radialoutlinel.png");
}
void RadialRoll(){
rotate(0);//if this is anything but 0 ball will appear of screen
image(img, ballX, ballY, h, h); //create a continuos rotation using the new fun
//nctions in the draw things
}
void draw() {
//clear the background and set the fill colour
background(0);
fill(255);
//draw the circle in it's current position
// ellipse(ballX, ballY, h,h);
//add a little gravity to the speed
speedY = speedY + 0;
speedX = speedX + .02;
ballX = ballX + speedX;
RadialRoll();
if (ballX > width - h) {
// set the position to be on the floor
ballX = width - h;
// and make the y speed 90% of what it was,
// but in the opposite direction
speedX = speedX * -1;
//switch the direction
//speedY = speedY;
}
if (ballX > width - h) {
// set the position to be on the floor
ballX = width - h;
// and make the y speed 90% of what it was,
// but in the opposite direction
speedX = speedX * -1;
//switch the direction
//speedY = speedY;
}
else if (ballX <= 0) {
// if the ball hits the top,
// make it bounce off
speedX = -speedX;
}
if (ballY > height - h) {
// set the position to be on the floor
ballY = height - h;
// and make the y speed 90% of what it was,
// but in the opposite direction
speedY = speedY * -1;
//switch the direction
//speedY = speedY;
}
else if (ballY <= 0) {
// if the ball hits the top,
// make it bounce off
speedY = -speedY;
}
}
如何在屏幕上移动图像并轻松旋转它们? 非常感谢你。 -TheIronHobo
答案 0 :(得分:1)
首先,当你看rotate()
函数时,它将弧度(0到TWO_PI之间的值)作为参数,所以当你想要流畅的旋转使用这样的东西时
rotate(counter*TWO_PI/360);
其中,计数器可以在每个draw()
循环中增加整数值。但是如果你只是将它添加到你的代码图像将围绕点[0,0](左上角)旋转,你将看不到旋转1/4的图像。为了更好地理解这一点,您应该阅读此TUTORIAL,然后您可以从基本轮换开始:
PImage img;
int counter = 1;
void setup() {
size(800, 800);
smooth();
background(0, 0, 0);
img = loadImage("radialoutlinel.png");
imageMode(CENTER); //you can change mode to CORNER to see the difference.
}
void draw() {
background(0);
fill(255);
counter++;
translate(width/2, height/2);
rotate(counter*TWO_PI/360);
image(img, 0, 0, 300, 300);
}
然后,如果您还想从左到右移动图像,您只需调整translate()
第一个参数。