Search code examples
androidcocos2d-android

Cocos2d-android CCMenu does not respond to touch


import org.cocos2d.events.CCTouchDispatcher;
import org.cocos2d.layers.CCColorLayer;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.menus.CCMenu;
import org.cocos2d.menus.CCMenuItem;
import org.cocos2d.menus.CCMenuItemImage;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.types.ccColor4B;
import android.view.MotionEvent;

public class MenuLayer extends CCColorLayer {


public static CCScene scene() {
    // Create the scene
    CCScene scene = CCScene.node();
    // Create a background layer.
    CCLayer background = new MenuLayer(ccColor4B.ccc4(255, 255, 255, 255));
    scene.addChild(background);
    return scene;
}

protected MenuLayer(ccColor4B colour) {
    super(colour);

    // Create a menu items.
    CCMenuItem start = CCMenuItemImage.item("Start.png", "Start.png",
             this, "startTouched"); 
    CCMenuItem options = CCMenuItemImage.item("Options.png",
            "Options.png", this, "optionsTouched");
    CCMenuItem help = CCMenuItemImage.item("Help.png", "Help.png", this,
            "helpTouched");
    // Create array of CCMenuItem object to add to CCMenu
    CCMenuItem[] items = { start, options, help };
    // Add menu items to menu
    CCMenu menu = CCMenu.menu(items);
    // Align items with 150px adding
    menu.alignItemsVertically(150);
    // Add menu to the scene
    addChild(menu);
    CCTouchDispatcher.sharedDispatcher().addTargetedDelegate(this, 0, true);
    this.setIsTouchEnabled(true);
}

@Override
public boolean ccTouchesBegan(MotionEvent event) {
    return true;
}

@Override
public boolean ccTouchesEnded(MotionEvent event) {
    return super.ccTouchesEnded(event);
}

/**This method is called when the start menu item is touched**/
public void startTouched() {
    CCScene scene = GameLayer.scene();
    CCDirector.sharedDirector().pushScene(scene);
}

/**This method is called when the options menu item is touched**/
public void optionsTouched() {
    CCScene scene = OptionsLayer.scene();
    CCDirector.sharedDirector().replaceScene(scene);
}

/**This method is called when the help menu item is touched**/
public void helpTouched() {
    CCScene scene = HelpLayer.scene();
    CCDirector.sharedDirector().replaceScene(scene);
}
}

I'm getting started with cocos2d-android and I'm having trouble getting a menu to respond to touch events.

The scene loads up and runs okay it just won't respond to touch events. If I put a breakpoint in ccTouchesEnded I can intercept the event but startTouched, optionsTouched, and helpTouched are never called. As far as I have read, CCMenu should have this functionality built in.

cocos2d-android is ported from cocos2d-iphone.

Can anyone point me in the right direction?

UPDATE:

07-21 13:31:17.933: WARN/System.err(6660): java.lang.NoSuchMethodException: startTouched
07-21 13:31:17.933: WARN/System.err(6660): at
java.lang.ClassCache.findMethodByName(ClassCache.java:247)
07-21 13:31:17.933: WARN/System.err(6660): at
java.lang.Class.getMethod(Class.java:962)
07-21 13:31:17.933: WARN/System.err(6660): at org.cocos2d.menus.CCMenuItem.<init>
(CCMenuItem.java:54)
...
// The errors repeat for optionsTouched and helpTouched

It looks like it not resolving the selector parameter to the correct method. Can anyone see anything wrong with startTouched or the others?


Solution

  • I fixed it by adding a parameter to the callback methods.

    Example:

    public void startTouched(Object sender) {
        CCScene scene = GameLayer.scene();
        CCDirector.sharedDirector().pushScene(scene);
    }
    

    I haven't yet figured out why that was required but it worked.