我想用关卡制作游戏。这意味着在第一级用户必须杀死一些目标;如果他获胜,他会以新目标和新背景进入下一个级别。
我有这些课程 ---->
package game.wael.ialhi;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.opengl.CCGLSurfaceView;
import org.cocos2d.sound.SoundEngine;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
public class SimpleGame extends Activity{
protected CCGLSurfaceView _glSurfaceView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
_glSurfaceView = new CCGLSurfaceView(this);
setContentView(_glSurfaceView);
}
@Override
public void onStart()
{
super.onStart();
CCDirector.sharedDirector().attachInView(_glSurfaceView);
CCDirector.sharedDirector().setDeviceOrientation(CCDirector
.kCCDeviceOrientationLandscapeLeft);
CCDirector.sharedDirector().setDisplayFPS(true);
CCDirector.sharedDirector().setAnimationInterval(1.0f / 60.0f);
CCScene scene = GameLayer.scene();
CCDirector.sharedDirector().runWithScene(scene);
}
@Override
public void onPause()
{
super.onPause();
CCDirector.sharedDirector().pause();
SoundEngine.sharedEngine().stopSound();
}
@Override
public void onResume()
{
super.onResume();
CCDirector.sharedDirector().resume();
}
@Override
public void onStop()
{
super.onStop();
CCDirector.sharedDirector().end();
SoundEngine.sharedEngine().stopSound();
}
}
我在这个GameLayer课程中做了一些修改。在更新方法中,我添加了这些行,以便当用户获胜时,他将传递给下一个场景(GameLayer1) ---->
CCScene scene=GameLayer1.scene();
CCDirector.sharedDirector().runWithScene(scene);
这是完整的课程 ------>
package game.wael.ialhi;
public class GameLayer extends CCColorLayer{
protected ArrayList<CCSprite> _targets;
protected ArrayList<CCSprite> _projectiles;
protected int _projectilesDestroyed;
public static int nb;
public static CCScene scene()
{
CCScene scene = CCScene.node();
CCColorLayer layer = new GameLayer(ccColor4B.ccc4(255, 255, 255, 255));
scene.addChild(layer);
return scene;
}
protected GameLayer(ccColor4B color)
{
super(color);
this.setIsTouchEnabled(true);
_targets = new ArrayList<CCSprite>();
_projectiles = new ArrayList<CCSprite>();
_projectilesDestroyed = 0;
CGSize winSize = CCDirector.sharedDirector().displaySize();
CCSprite player = CCSprite.sprite("Player.png");
player.setPosition(CGPoint.ccp(player.getContentSize().width / 2.0f,
winSize.height / 2.0f));
addChild(player);
// Handle sound
Context context = CCDirector.sharedDirector().getActivity();
SoundEngine.sharedEngine().preloadEffect(context, R.raw.pew_pew_lei);
SoundEngine.sharedEngine().playSound(context, R.raw.background_music_aac,
true);
this.schedule("gameLogic", 1.0f);
this.schedule("update");
}
@Override
public boolean ccTouchesEnded(MotionEvent event)
{
// Choose one of the touches to work with
CGPoint location =
CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(),
event.getY()));
// Set up initial location of projectile
CGSize winSize = CCDirector.sharedDirector().displaySize();
CCSprite projectile = CCSprite.sprite("Projectile.png");
projectile.setPosition(20, winSize.height / 2.0f);
// Determine offset of location to projectile
int offX = (int)(location.x - projectile.getPosition().x);
int offY = (int)(location.y - projectile.getPosition().y);
// Bail out if we are shooting down or backwards
if (offX <= 0)
return true;
// Ok to add now - we've double checked position
addChild(projectile);
projectile.setTag(2);
_projectiles.add(projectile);
// Determine where we wish to shoot the projectile to
int realX = (int)(winSize.width + (projectile.getContentSize().width /
2.0f));
float ratio = (float)offY / (float)offX;
int realY = (int)((realX * ratio) + projectile.getPosition().y);
CGPoint realDest = CGPoint.ccp(realX, realY);
// Determine the length of how far we're shooting
int offRealX = (int)(realX - projectile.getPosition().x);
int offRealY = (int)(realY - projectile.getPosition().y);
float length = (float)Math.sqrt((offRealX * offRealX) + (offRealY *
offRealY));
float velocity = 480.0f / 1.0f; // 480 pixels / 1 sec
float realMoveDuration = length / velocity;
// Move projectile to actual endpoint
projectile.runAction(CCSequence.actions(
CCMoveTo.action(realMoveDuration, realDest),
CCCallFuncN.action(this, "spriteMoveFinished")));
// Pew!
Context context = CCDirector.sharedDirector().getActivity();
SoundEngine.sharedEngine().playEffect(context, R.raw.pew_pew_lei);
return true;
}
public void gameLogic(float dt)
{
addTarget();
}
public void update(float dt)
{
ArrayList<CCSprite> projectilesToDelete = new ArrayList<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);
ArrayList<CCSprite> targetsToDelete = new ArrayList<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);
}
int k=0;
for (CCSprite projectile : projectilesToDelete)
{
_projectiles.remove(projectile);
removeChild(projectile, true);
if (++_projectilesDestroyed > 4)
{
_projectilesDestroyed = 0;
CCDirector.sharedDirector().replaceScene(GameOverLayer.
scene("You Win!",255,255,255,255));
nb=1;
/*Thread timer = new Thread() {
@Override
public void run() {
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// i add these lignes to pass to the next scene if the user wins
CCScene scene = GameLayer1.scene();
CCDirector.sharedDirector().runWithScene(scene);
}
}
};
timer.start();*/
}
}
}
protected void addTarget()
{
Random rand = new Random();
CCSprite target = CCSprite.sprite("Target.png");
// Determine where to spawn the target along the Y axis
CGSize winSize = CCDirector.sharedDirector().displaySize();
int minY = (int)(target.getContentSize().height / 2.0f);
int maxY = (int)(winSize.height - target.getContentSize().height / 2.0f);
int rangeY = maxY - minY;
int actualY = rand.nextInt(rangeY) + minY;
// Create the target slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
target.setPosition(winSize.width + (target.getContentSize().width / 2.0f),
actualY);
addChild(target);
target.setTag(1);
_targets.add(target);
// Determine speed of the target
int minDuration = 2;
int maxDuration = 4;
int rangeDuration = maxDuration - minDuration;
int actualDuration = rand.nextInt(rangeDuration) + minDuration;
// Create the actions
CCMoveTo actionMove = CCMoveTo.action(actualDuration,
CGPoint.ccp(-target.getContentSize().width / 2.0f, actualY));
CCCallFuncN actionMoveDone = CCCallFuncN.action(this,
"spriteMoveFinished");
CCSequence actions = CCSequence.actions(actionMove, actionMoveDone);
target.runAction(actions);
}
public void spriteMoveFinished(Object sender)
{
CCSprite sprite = (CCSprite)sender;
if (sprite.getTag() == 1)
{
_targets.remove(sprite);
_projectilesDestroyed = 0;
CCDirector.sharedDirector().replaceScene(GameOverLayer.
scene("You Lose :(",255,255,255,255));
nb=0;
}
else if (sprite.getTag() == 2)
_projectiles.remove(sprite);
this.removeChild(sprite, true);
}
}
当用户赢得后台更改但新精灵永远不会显示时。我怎样才能让新的精灵出现?
答案 0 :(得分:1)
CCMenuItem level1 = CCMenuItemLabel.item("Level 1", this, "level1");
CCMenuItem items[] = { level1};
CCMenu menu = CCMenu.menu(items);
menu.alignItemsVertically(50);
this.addChild(menu);
public void level1(Object sender) {
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!");
CCScene scene = GameLayer.scene();
CCDirector.sharedDirector().pushScene(scene);
System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&");
}
答案 1 :(得分:1)
CCDirector.sharedDirector().replaceScene(Level1.scene());
答案 2 :(得分:0)
CCdirector.sharedDirector().runWithScene(scene);
这会导致语法错误吗?
答案 3 :(得分:0)
您是否删除了当前图层
removeFromParentAndCleanup(true);
或
removeSelf();
之后你可以替换场景
CCScene scene = GameLayer.scene();
CCDirector.sharedDirector().replaceScene(scene);
您还必须将所有变量重置为初始位置。
答案 4 :(得分:0)
您必须将pushScene用于下一级
CCScene scene = GameLayerSec.scene();
CCDirector.sharedDirector().pushScene(scene);
GameLayerSec.scene() should be the next scene you designed for your next level.