2012-11-23 63 views
2

對不起,如果這是一個新手問題,但我剛開始使用cocos2d-html,我有一些問題創建一個圖像的ccmenu。這裏是我的代碼:顯示在屏幕上cocos2d-html5不能做一個基本的ccmenu工作

var ActionLayer = cc.Layer.extend({ 
getTexture: function (name) { 
return cc.TextureCache.getInstance() 
.addImage('./images/' + name + '.png'); 
}, 
addObject: function (desc) { 
var sprite = cc.Sprite.createWithTexture(this.getTexture(desc.name)); 

sprite.setAnchorPoint(desc.anchor || cc.p(0.5, 0.5)); 
     sprite.setScaleX(desc.scaleX || desc.scale || 1); 
     sprite.setScaleY(desc.scaleY || desc.scale || 1); 
     sprite.setRotation(desc.rotation || 0); 
     sprite.setPosition(cc.p(desc.x || 0, desc.y || 0)); 
this.addChild(sprite, desc.z || 0); 
     return sprite; 
    }, 
    checkAnswer:function(){ 
     alert('yay'); 
    }, 
    init: function() { 
     this._super(); 
     this.removeAllChildrenWithCleanup(true); 
     this.setTouchEnabled(true); 
var layer1 = cc.LayerColor.create(
      new cc.Color4B(00, 185, 214, 255), 1024, 768); 
layer1.setPosition(new cc.Point(0.0,0.0)); 
     this.addChild(layer1,-2); 
var director = cc.Director.getInstance(), 
      self = this, 
      winSize = director.getWinSize(); 
var bgSprite = this.addObject({ 
      name: "GenericBG", 
      scaleY: 1, 
      anchor: cc.p(0, 0), 
      z: 0 
}); 
var closeItem = cc.MenuItemImage.create('./images/sign.png','./images/sign.png',this,'checkAnswer'); 
closeItem.setAnchorPoint(cc.p(0.5, 0.5)); 
     var menu = cc.Menu.create(closeItem); 
menu.setPosition(500,300); 
     this.addChild(menu, 5); 
    } 
}); //end ActionLayer 

//--------------------- Scene --------------------- 

var ActionLayerScene = cc.Scene.extend({ 
onEnter: function() { 
this._super(); 

var layer = new ActionLayer(); 
     layer.init(); 
this.addChild(layer); 
    } 
}); 

菜單,但是當我點擊菜單項,沒有任何反應 有控制檯上沒有JavaScript錯誤,所以我真的不知道如何調試這一點。 謝謝 Cyril

回答

0

cc.MenuItemImage.create()的參數是錯誤的。相反的:

var closeItem = cc.MenuItemImage.create('./images/sign.png','./images/sign.png',this,'checkAnswer'); 

它應該是:

var closeItem = cc.MenuItemImage.create('./images/sign.png','./images/sign.png','checkAnswer', this); 
0
for(var i=0; i<5; i++){ 
    var colorImg = cc.MenuItemImage.create(color_list[i], color_list[i], "colorAction", this); 
    colorImg.setPosition(new cc.Point(40+i*70, 38)); 
    colorImg.tag = i+100; 
    this.colorImageArray.push(colorImg); 
    colorMenu.addChild(colorImg); 
} 


colorAction:function(event) 
{ 
    var colorTag = event.tag; 
    switch(colorTag) 
    { 
     case 1: 

       break; 
     case 2: 

       break; 
     case 3: 
       break; 
    } 
}