2

我想制作一个有关卡的游戏。这意味着在第一关中用户必须杀死一些目标,如果他赢了,他会通过新目标和新背景进入下一个关卡。我用这个游戏教程!

我有这些课程---->

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 类中进行了一些修改。在更新方法中,我添加了这些 ligne,以便当用户获胜时,他传递到下一个场景(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);
    }
  }

但是当用户只赢得背景变化时,新的精灵永远不会出现..请帮助

4

1 回答 1

0

我希望这能帮到您。我需要创建 Level2.class 并更改背景图像。使用 getter 和 setter 在类中设置级别值。

if (Score.getLevel() == 1) {

            CCScene scene = Level2.scene();
            CCDirector.sharedDirector().replaceScene(
                    CCSlideInRTransition.transition(.5f, scene));

        } 

从 Level 屏幕设置 Level 值。

于 2013-08-06T13:58:34.900 回答