Cocos2d-android CCMenu不响应触摸
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 或其他人有什么问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过向回调方法添加参数来修复它。
示例:
我还没有弄清楚为什么需要这样做,但它有效。
I fixed it by adding a parameter to the callback methods.
Example:
I haven't yet figured out why that was required but it worked.