在 Cocos2d android 游戏中,我需要添加关卡,所以当游戏完成时它会显示“You Win”,然后我需要添加下一个关卡,但是当我尝试添加以下代码时,它不会替换场景。任何人都可以帮助我解决这个问题。先感谢您。
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();
}
}
日志错误:
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)