bitmaplist[i] is undefined
bitmaplist[i] is undefined
这里的代码在画布上随机的位置绘制5个草的图像。我给所有的草物体一个独特的名字。然后,我为每个草对象创建了一个click事件,它将显示它们各自的名称。
bitmaplist[i].on('click',function vanish(event){
alert(bitmaplist[i].name);
}
);
但它一直给我错误,bitmaplist[i]是未定义的。这个问题的原因是什么?如何解决这个问题?
完整代码:
<html>
<head>
<script src="easeljs.js"></script>
</head>
<body>
<canvas id="mycanvas" width="1000" height="500" style="border:1px solid black;"></canvas>
<script>
function makeit(){
var bitmaplist=[];
var i;
var canvas=document.getElementById("mycanvas");
var ctx=canvas.getContext('2d');
var stage=new createjs.Stage(canvas);
var image=new Image();
image.src="grass.jpg";
for(i=0;i<5;i++){
bitmaplist[i]=new createjs.Bitmap(image);
bitmaplist[i].x=stage.canvas.width-Math.random()*900;
bitmaplist[i].y=Math.random()*400;
bitmaplist[i].name="grass"+i;
bitmaplist[i].speed=Math.random()*5;
bitmaplist[i].mouseEnabled=true;
stage.addChild(bitmaplist[i]);
stage.update();
bitmaplist[i].on('click',function vanish(event){
alert(bitmaplist[i].name);
}
);
}
}
window.onload=makeit;
</script>
</body>
</html>
尝试这个对象:
bitmaplist[i].on('click',function vanish(event){
alert(this.name);
});
或使用target属性的事件对象:
bitmaplist[i].on('click',function vanish(event){
alert(event.target.name);
});
点击这里阅读更多EventDispatcher
相关文章:
- Jquery - table.row(tr) is undefined
- $routeParams is undefined
- Angular js $scope is Undefined
- Angular - $cookies is undefined
- TypeError: e is undefined - javascript
- “google.maps.MapTypeId is undefined”在FF 14中使用GMAP3时
- chrome.storage.local["mykey"] is undefined
- 回调在 Angular2/Firebase 中生成“TypeError: this is undefined”
- KendoWindow .visible(); is undefined?
- jquery: $ is undefined
- ReactDom is undefined
- TypeScript: TypeError b is undefined
- rowdata is undefined
- mozChannels/mozSampleRate is undefined
- phonegap build cordova.file is undefined
- window.jQuery.ui is undefined
- blackberry.io.dir.appDirs is undefined
- Youtube iFrame API 'YT is undefined'
- SignalR $.connection is undefined
- “window.angular is undefined.” 当使用量角器进行自动测试时