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);
}
}
我开始使用 cocos2d-android 并且在获取菜单以响应触摸事件时遇到了麻烦。
场景加载并运行正常,只是不会响应触摸事件。如果我在 ccTouchesEnded 中设置断点,我可以拦截事件,但 startTouched、optionsTouched 和 helpTouched 永远不会被调用。据我所知,CCMenu 应该内置此功能。
cocos2d-android 是从 cocos2d-iphone 移植过来的。
谁能指出我正确的方向?
更新:
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
看起来它没有将选择器参数解析为正确的方法。任何人都可以看到 startTouched 或其他人有什么问题吗?