我正在制作应用,但我遇到了问题。当我按下一个按钮时,我需要在我的画布上添加新点(图像)。这是一个代码:
var ID = 0;
var points = [];
function addPoint(){
points.push({id: ID, posX: 0, posY: 0, url: "img/point.png"});
ID++;
showPoints();
}
function showPoints(){
var img = new Array();
var point = new Array();
var stage = new Kinetic.Stage({
container: 'cvsCroatia',
width: 574,
height: 508
});
var layer = new Kinetic.Layer();
for(var j=0; j < ID; j++){
img[j] = new Image();
img[j].src = 'img/point.png';
img[j].onload = (function(){
point[j] = new Kinetic.Image({
x: points[j].posX,
y: points[j].posY,
image: img[j],
width: 13,
height: 13,
name: img[j],
draggable: true
});
});
layer.add(point[j]);
}
stage.add(layer);
}
但我收到了一个错误:
未捕获的TypeError:无法设置未定义的属性'index'(kinetic-v4.4.0.min.js:29)
你有什么想法,有什么不对吗?谢谢答案。艾伦..
答案 0 :(得分:0)
var img = new Array(); // the simplest solution is to make img visible outside your function
function showPoints(){
// var img = new Array(); // HERE img is a LOCAL variable, only visible to the showPoints() function
var point = new Array();
var stage = new Kinetic.Stage({
container: 'cvsCroatia',
width: 574,
height: 508
});
答案 1 :(得分:0)
感谢您的回答,我把所有变量都放到全局范围内,但不幸的是得到了同样的错误..
var ID = 0;
var points = [];
var img = new Array();
var point = new Array();
function showPoints(){
var stage = new Kinetic.Stage({
container: 'cvsCroatia',
width: 574,
height: 508
});
var layer = new Kinetic.Layer();
for(var j=0; j < ID; j++){
img[j] = new Image();
img[j].src = 'img/point.png';
img[j].onload = (function(){
point[j] = new Kinetic.Image({
x: points[j].posX,
y: points[j].posY,
image: img[j],
width: 13,
height: 13,
name: img[j],
draggable: true
});
});
layer.add(point[j]);
}
stage.add(layer);
}
function addPoint(){
points.push({id: ID, posX: 0, posY: 0, url: "img/point.png"});
ID++;
showPoints();
}