在Cocos2d android遊戲我需要添加關卡,所以當遊戲結束後顯示「You Win」,之後我需要添加下一關,但是當我嘗試添加下面的代碼不會取代現場。任何人都可以幫助我解決這個問題。先謝謝你。包括Cocos2d的遊戲關卡Android沒有進入下一個場景
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)
剛取代這個CCDirector.sharedDirector()。replaceScene(newLayer.scene()); 和它去下一個場景,但在進入下一個場景之前,我需要顯示爲LEVEL1,當玩家點擊確定後,它應該去下一個場景我該如何做到這一點 – Tycoon
用菜單添加一箇中間場景 – LearnCocos2D