Can't make a basic ccmenu work


#1

Hi guys,

Sorry if this is a newbie question but I have just started using cocos2d-html and I am having some issues creating a ccmenu with an image. Here is my code:

`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);
}

});`
The menu is displayed on the screen but when I click on the menuitem, nothing happens
There is no javascript error on the console so I really don’t know how to debug this.
Thanks
Cyril


#2

checkAnswer is a member function of the layer,

change this line from
cc.MenuItemImage.create(‘./images/sign.png’,‘./images/sign.png’,this,‘checkAnswer’);
to
cc.MenuItemImage.create(‘./images/sign.png’,‘./images/sign.png’,this,this.checkAnswer);