包括Cocos2d Android中的游戏关卡没有进入下一个场景

时间:2013-06-05 07:54:00

标签: android cocos2d-iphone cocos2d-android

在Cocos2d安卓游戏中我需要添加关卡,所以当游戏完成时它显示“你赢了”之后我需要添加下一个关卡但是当我尝试添加以下代码时它不会替换场景。任何人都可以帮我解决这个问题。提前谢谢。

public void update(float dt)
{
    LinkedList<CCSprite> projectilesToDelete = new LinkedList<CCSprite>();

    for (CCSprite projectile : _projectiles)
    {
        CGRect projectileRect = CGRect.make(projectile.getPosition().x - (projectile.getContentSize().width / 2.0f),
                                            projectile.getPosition().y - (projectile.getContentSize().height / 2.0f),
                                            projectile.getContentSize().width,
                                            projectile.getContentSize().height);

        LinkedList<CCSprite> targetsToDelete = new LinkedList<CCSprite>();

        for (CCSprite target : _targets)
        {
            CGRect targetRect = CGRect.make(target.getPosition().x - (target.getContentSize().width),
                                            target.getPosition().y - (target.getContentSize().height),
                                            target.getContentSize().width,
                                            target.getContentSize().height);

            if (CGRect.intersects(projectileRect, targetRect))
                targetsToDelete.add(target);
        }

        for (CCSprite target : targetsToDelete)
        {
            _targets.remove(target);
            removeChild(target, true);
        }

        if (targetsToDelete.size() > 0)
            projectilesToDelete.add(projectile);
    }

    for (CCSprite projectile : projectilesToDelete)
    {
        _projectiles.remove(projectile);
        removeChild(projectile, true);

        if (++_projectilesDestroyed > 30)
        {
            _projectilesDestroyed = 0;
            CCDirector.sharedDirector().replaceScene(GameOverLayer.scene("You Win!"));

        CCMenuItemFont item1 = CCMenuItemFont.item("Next level", this, "onNextlevel");
        item1.setColor(new ccColor3B(0,0,0));
        CCMenu menu = CCMenu.menu(item1);
        menu.alignItemsVertically();
        addChild(menu);
    }
    }
    }



      public void onNextlevel()
        {
        try {

            CCScene scene = CCScene.node();
            scene.addChild(new Level1(color1), 0);
            CCDirector.sharedDirector().replaceScene(scene);

        } catch (Exception e) {
            e.printStackTrace();
        }
       }

Logcat错误:

W/System.err(2037): java.lang.NoSuchMethodException: onNextlevel [class java.lang.Object] W/System.err(2037): at java.lang.Class.getConstructorOrMethod(Class.java:460) W/System.err(2037): at org.cocos2d.menus.CCMenuItem.<init>(CCMenuItem.java:54) W/System.err(2037): at org.cocos2d.menus.CCMenuItemLabel.<init>(CCMenuItemLabel.java:43) W/System.err(2037): at org.cocos2d.menus.CCMenuItemFont.<init>(CCMenuItemFont.java:49) W/System.err(2037): at org.cocos2d.menus.CCMenuItemFont.item(CCMenuItemFont.java:44) W/System.err(2037): at com.tutorials.cocos2dsimplegame.GameLayer.update(GameLayer.java:253) W/System.err(2037): at java.lang.reflect.Method.invokeNative(Native Method) W/System.err(2037): at java.lang.reflect.Method.invoke(Method.java:513) W/System.err(2037): at org.cocos2d.actions.CCTimer.update(CCTimer.java:82) W/System.err(2037): at org.cocos2d.actions.CCScheduler.tick(CCScheduler.java:252) W/System.err(2037): at org.cocos2d.nodes.CCDirector.drawCCScene(CCDirector.java:692) W/System.err(2037): at org.cocos2d.nodes.CCDirector.onDrawFrame(CCDirector.java:663) W/System.err(2037): at org.cocos2d.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1171) W/System.err(2037): at org.cocos2d.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1080)

1 个答案:

答案 0 :(得分:1)

它告诉你“this”类没有onNextLevel方法。

将一个菜单添加到即将替换的情景中也没有任何意义,因为您将无法与它进行交互(更不用说看到它)了。您可能想要新场景上的菜单,对吗?