1

レベルのあるゲームを作りたいです。つまり、最初のレベルでは、ユーザーはいくつかのターゲットを殺さなければなりません。彼が勝った場合、彼は新しいターゲットと新しい背景で次のレベルに進みます。

このゲームのチュートリアルを使用します。

私はこれらのクラスを持っています---->

    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 クラスにいくつかの変更を加えました。update メソッドでこれらの行を追加して、ユーザーが勝ったときに次のシーン (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

5 に答える 5

0
CCdirector.sharedDirector().runWithScene(scene);

これにより構文エラーが発生しますか?

于 2013-02-23T07:53:12.450 に答える
0

現在のレイヤーを削除しましたか

 removeFromParentAndCleanup(true);

また

  removeSelf();

その後、シーンを置き換えることができます

  CCScene scene = GameLayer.scene();
  CCDirector.sharedDirector().replaceScene(scene);

また、すべての変数を初期位置にリセットする必要があります。

于 2013-02-25T13:18:19.467 に答える
0

次のレベルでは pushScene を使用する必要があります

    CCScene scene = GameLayerSec.scene();
    CCDirector.sharedDirector().pushScene(scene);

    GameLayerSec.scene() should be the next scene you designed for your next level.
于 2013-03-20T10:55:24.877 に答える