[英]Not working 'PImage' as Class Property in Processing.js
What i was trying to do is, declare a class which has an Image as it's property.Then I am declaring an Object of that class and call draw function.But it's not working....Why? 我试图做的是,声明一个具有Image属性的类,然后声明该类的Object并调用draw函数。但是它不起作用....为什么? Here is my processing.js part of my .html page 这是我的.html页面中的processing.js部分
<script type="text/processing"> PImage starImage; void setup(){ size(400,400); smooth(); starImage = loadImage("star.png"); } var Star = function(x,y) { this.x = x; this.y = y; this.img = starImage; }; Star.prototype.drawStar = function(){ image(this.img,this.x,this.y,50,50); }; var obj = new Star(50,50); draw = function(){ obj.drawStar(); }; </script>
But if I change "image(this.img,this.x,this.y,50,50);" 但是如果我更改“ image(this.img,this.x,this.y,50,50);” to "image(starImage,this.x,this.y,50,50);",it's works.That seems assignment of 'starImage' (PImage) in this.img is not working. 到“ image(starImage,this.x,this.y,50,50);”,它起作用了。这似乎在this.img中分配了'starImage'(PImage)无效。
I think your main problem is that you're creating your Star
object before you load your starImage
PImage
. 我认为您的主要问题是,在加载starImage
PImage
之前先创建Star
对象。 Try refactoring your code to make sure that you load the image before you create the Star
object. 尝试重构代码,以确保在创建Star
对象之前先加载图像。
Here is how you'd do it in pure Processing: 这是在纯处理中的处理方式:
PImage starImage;
Star obj;
void setup() {
size(400, 400);
smooth();
starImage = loadImage("star.png");
obj = new Star(50, 50, starImage);
}
class Star{
float x;
float y;
PImage img;
public Star(float x, float y, PImage img){
this.x = x;
this.y = y;
this.img = img;
}
public void drawStar(){
image(this.img, this.x, this.y, 50, 50);
}
}
void draw(){
obj.drawStar();
}
That should work in Processing.js too. 这也应该在Processing.js中起作用。 Notice that the Star
object isn't being created until after the image is loaded. 请注意,直到加载图像后才创建Star
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.