0

I want to dispatch my event,but it doesn't work. this is my code and I've deleted some irrelevant parts .

(function(window){
  function HeroSelectView(){
  this.initialize();
}
createjs.EventDispatcher.initialize(HeroSelectView.prototype);
HeroSelectView.prototype = new createjs.Container();

var selectedName;
HeroSelectView.prototype.Container_initialize=HeroSelectView.prototype.initialize;

HeroSelectView.prototype.initialize=function(){
this.Container_initialize();

this.initView();
}

HeroSelectView.prototype.initView = function(){

for (var i = 0; i < heroArray.length; i++) {
    heroArray[i].x=100+40*i;
    heroArray[i].y=200;
    this.addChild(heroArray[i]);
    heroArray[i].addEventListener("click",onSelectHero);
  };
}

 function onSelectHero(event){
   selectedName=event.target.name;
   var myevent = {
     type: "selectedEvent",
     param: selectedName
   };
    //var myevent=new createjs.Event("selectedEvent");  //createjs 0.7 doesnot work  either.
   this.dispatchEvent(myevent);
}
window.HeroSelectView=HeroSelectView;

}(window))

in the onSelectedHero, at first I tried it with createjs-0.61. you can see the "myevent" .But there is an error " TypeError: Argument 1 of EventTarget.dispatchEvent is not an object.". Then I tried it with version 0.7, but still got the same error. How can I fix this?

Thanks

Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
Metsa
  • 1
  • 2
  • 1
    not sure if this helps, but you can give it a try: `heroArray[i].addEventListener("click",onSelectHero.bind(this));` – olsn Oct 06 '13 at 17:02
  • 1
    To elaborate on @olsn's answer, If you don't have a proper scope when adding the listener, then "this" will be a different scope (usually window), which is not the object you added the listener to. – Lanny Oct 07 '13 at 15:18
  • @olsn wow,thanks. It works! By the way, I've learnt a lot from your website, great tutorials. – Metsa Oct 11 '13 at 13:19
  • @Lanny. thanks. easeljs is a great framework to us who use AS3. It saves us a lot of time. – Metsa Oct 11 '13 at 13:24
  • @Lanny hi, there is no such example in the easeljs example file. Well, maybe it will be better to add an example about customized event. you know people who deal with AS3 are now familiar with JS. That's just a suggsetion :) – Metsa Oct 11 '13 at 14:35
  • 1
    Check out the new EventDispatcher method "on", which provides automatic, as well as manual scoping really easily. There is an example in the Mouse Interaction Tutorial: http://createjs.com/tutorials/Mouse%20Interaction/ – Lanny Oct 19 '13 at 17:19

0 Answers0