jsimage.onload调用父对象上的方法
js image.onload call method on parent
我正在制作一个网络应用程序,在这里我从网络加载图像并显示它们。我正在使用KineticJS。我所做的是首先进行预加载,在预加载中我加载一个"加载"图像,直到实际图像加载并准备好显示为止。
所以,这是我的代码:
function CardObject(cardName)
{
this.name = cardName;
this.tapped = false;
// Create kinetic image with loading image
this.image = new Kinetic.Image({
x: 10,
y: 10,
image: CardLoadingImage,
width: CardWidth,
height: CardHeight
});
// Add actual image, and when loaded change it
this.imageObj = new Image();
this.imageObj.onload = function() //Problem here
{
this.image.setImage(this.imageObj);
}
this.imageObj.src = "testImage.jpg";
// Add it to the stage
this.layer = new Kinetic.Layer();
this.layer.add(this.image);
Stage.add(this.layer);
}
不过,我的imageObj的onload函数有问题。我得到错误:未捕获类型错误:无法调用未定义的方法"setImage"
当我在调试器中查看时,函数中的"this"是那个图像对象,而不是我的卡。。。这不是我所期望的,也不是我所需要的。我该怎么解决?
所以它要做的是,首先用我的loadingImage制作一个动态图像。然后,当加载实际图像时,将图像更改为该新图像。
谢谢!-Pablo
将自定义变量分配给this
,然后使用该自定义变量或使用jquery代理
function CardObject(cardName)
{
this.name = cardName;
this.tapped = false;
var that = this;//assigning custom variable
// Create kinetic image with loading image
this.image = new Kinetic.Image({
x: 10,
y: 10,
image: CardLoadingImage,
width: CardWidth,
height: CardHeight
});
// Add actual image, and when loaded change it
this.imageObj = new Image();
this.imageObj.onload = function() //Problem here
{
that.image.setImage(this);//edited
}
this.imageObj.src = "testImage.jpg";
// Add it to the stage
this.layer = new Kinetic.Layer();
this.layer.add(this.image);
Stage.add(this.layer);
}
相关文章:
- 序列化数据属性中对象的最可靠方法
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 如何从对象的原型方法访问JavaScript对象属性
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- 设置嵌套对象属性的更好方法
- JQuery示例不起作用-“;对象没有't支持属性或方法'按钮'&”;
- 对象不支持属性或方法“自动完成”
- call()和apply()实际上是用来欺骗方法处理类似数组的对象的
- 对象文字方法上的Javascript绑定不起作用
- Meteor应用程序无法运行-对象#<编译器>没有方法'主机'
- 为什么我可以在Array属性对象中找到Javascript Array for Each方法
- Underscore.js某些对象的所有方法的总和
- 对象#<XMLHttpRequest>没有方法'完成'
- Es6:能够在设置/更新/删除对象属性时调用自定义方法
- 丢失对象“;这个“;方法中的上下文
- 如何在ajax中调用javascript对象的方法
- 为什么页面方法对象未定义
- 如何使用在另一个文件中定义的JavaScript方法/对象
- 如何告诉JsHint忽略“未解析的函数或方法”?对象上的警告
- 使用jinja/javascript动态创建方法/对象