所以我想弄清楚爲什麼它說display()不是函數,我覺得這是代碼可能是完全錯誤的...構造函數無法正常工作? P5.JS
另外,我有點困惑圖像的x和y位置?我在哪裏定義它們。
這段代碼的目的是爲了讓Stormtrooper png在屏幕上浮動,最終讓他在點擊時發出聲音,並讓他旋轉(我也被困住了) - 一個現場版本可以在這裏看到:https://benjamingibbsportfolio.000webhostapp.com/(這是我的半成品組合,PLZ不要苛刻)
我已經以不同的方式正確完成了這段代碼,我只是瞭解構造函數atm。我最初很興奮,因爲我認爲我終於掌握了'this'這個關鍵詞,然後它全部崩潰了!
function preload() {
img = loadImage("stormy3.png");
}
function storm(x,y,xSpeed,ySpeed,img) {
this.x = x;
this.y = y;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
this.img = img;
this.display = function() {
image(this.img,this.x,this.y);
}
this.move = function() {
this.x = this.x + this.xSpeed;
this.y = this.y + this.ySpeed;
}
this.bounce = function() {
if(this.x > width || this.x < 0) {
this.xSpeed = this.xSpeed * -1;
}
if(this.y > height || this.y < 0) {
this.ySpeed = this.ySpeed * -1;
}
}
}
function setup() {
// TRANSPARENT BACKGROUND*
background(255, 0, 0, 0.4);
//
var myCanvas = createCanvas(1440, 4000);
myCanvas.position(0, 0);
}
function draw() {
// TRANSPARENT BACKGROUND*
clear();
//
storm.display();
storm.move();
storm.bounce();
}
你是怎麼構建'storm'的?你試過新的風暴(...) – ChrisG
告訴我們'風暴'的定義 –
是的,我已經完全搞砸了這個tbh,它沒有任何意義 –