【发布时间】:2021-08-15 00:04:52
【问题描述】:
(初学者) 我正在创建一个游戏,我希望图像在触及画布限制时水平翻转,所以当我第一次应用 scale(-1,1) 时它工作得很好。在对代码进行了一些更改后,它停止了工作。我也尝试创建一个函数并调用它,但出现“不是函数”错误。有什么建议吗?
let fish1, fish2, fish3, fish4;
let img1, img2, img3, img4;
function preload() {
img1 = loadImage("Fishes/fish1.png");
img2 = loadImage("Fishes/fish2.png");
img3 = loadImage("Fishes/fish3.png");
img4 = loadImage("Fishes/fish4.gif");
}
function setup() {
createCanvas(1920, 1080);
fish1 = new Fish(400, 150, img1);
fish2= new Fish(200, 150, img2);
fish3= new Fish(100, 50, img3);
fish4= new Fish(200, 150, img4);
}
function draw() {
background(bg);
fish1.display();
fish2.display();
fish3.display();
fish4.display();
fish1.swim1();
fish2.swim2();
fish3.swim3();
fish4.swim4();
fish1.limits();
fish2.limits();
fish3.limits();
fish4.limits();
}
function flip() {
scale(-1, 1);
}
class Fish{
constructor(x, y, inImg) {
this.img1 = inImg;
this.img2 = inImg;
this.img3 = inImg;
this.img4 = inImg;
this.x1 = x;
this.y1 = y;
this.x2 = x + 1000;
this.y2 = y + 200;
this.x3 = x + 100;
this.y3 = y + 400;
this.x4 = x + 1000;
this.y4 = y + 400;
this.x1direction = random(0, 1);
this.y1direction = random(0, 1);
this.x2direction = random(0, 2);
this.y2direction = random(0, 1);
this.x3direction = random(0, 2.5);
this.y3direction = random(0, 3);
this.x4direction = random(0.5, 2);
this.y4direction = random(0.3, 1.5);
}
display() {
image(img1, this.x1, this.y1);
image(img2, this.x2, this.y2);
image(img3, this.x3, this.y3);
image(img4, this.x4, this.y4);
}
swim1() {
push();
translate(this.x1 + px / 80, this.y1 + px / 80);
if (this.x1direction < 0) {
//this.img1.flip();
scale(-1, 1);
}
pop();
this.x1 += this.x1direction;
this.y1 += this.y1direction;
}
swim2() {
push();
translate(this.x2 + px / 80, this.y2 + px / 80);
if (this.x2direction < 0) {
//this.img2.flip();
scale(-1, 1);
}
pop();
this.x2 += this.x2direction;
this.y2 += this.y2direction;
}
swim3() {
push();
translate(this.x3 + px / 80, this.y3 + px / 80);
if (this.x3direction < 0) {
//this.img3.flip();
scale(-1, 1);
}
pop();
this.x3 += this.x3direction;
this.y3 += this.y3direction;
}
swim4() {
push();
translate(this.x4 + px / 80, this.y4 + px / 80);
if (this.x4direction < 0) {
//this.img4.flip();
scale(-1, 1);
}
pop();
this.x4 += this.x4direction;
this.y4 += this.y4direction;
}
limits() {
if (this.x1 > width || this.x1 < 0) {
this.x1direction = -this.x1direction;
}
if (this.y1 > height || this.y1 < 0) {
this.y1direction = -this.y1direction;
}
if (this.x2 > width || this.x2 < 0) {
this.x2direction = -this.x2direction;
}
if (this.y2 > height || this.y2 < 0) {
this.y2direction = -this.y2direction;
}
if (this.x3 > width || this.x3 < 0) {
this.x3direction = -this.x3direction;
}
if (this.y3 > height || this.y3 < 0) {
this.y3direction = -this.y3direction;
}
if (this.x4 > width || this.x4 < 0) {
this.x4direction = -this.x4direction;
}
if (this.y4 > height || this.y4 < 0) {
this.y4direction = -this.y4direction;
}
}
}
【问题讨论】:
-
能否告诉我我是否成功回答了您的问题,或者是否有遗漏或不清楚的地方?
-
嘿,保罗! (我是新来的,我可以这样回答用户吗?)非常感谢您的回答。我还没有时间重新编写我的代码,但是当我这样做时,我会告诉你!
标签: javascript scale p5.js flip