2

あらゆる方向からシーンに入るには、約50個の小さな虫が必要なゲームを作っています。画面上でランダムに動き回ってほしいのですが、不可能なようです。MoveModifierを使用する場合、各スプライトの終了位置を指定する必要があるようです。移動修飾子を使用せずにこれを行う方法はありますか?私はbox2d拡張機能に精通していませんが、多くの人がスプライトを物理ボディに取り付けて移動するために使用しているのを見てきました。この拡張機能が必要になるのでしょうか。はっきりしていません。また、スプライトと他のアニメーション化されたスプライトとの間の衝突検出を検出するためのスプライトが必要です。どうすればこれを行うことができますか私はそれほど明確ではありません。助けてください。以下は私のコードです..それは正しいように見えますか

    private Runnable mStartMosq = new Runnable() {
    public void run() {
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        Log.d("Level1Activity", "width " + dm.widthPixels + " height " + dm.heightPixels);

        int i = nMosq++;

        Scene scene = Level1Activity.this.mEngine.getScene();
        float startX = gen.nextFloat() * CAMERA_WIDTH;
        float startY = gen.nextFloat() * (CAMERA_HEIGHT);   // - 50.0f);
        sprMosq[i] = new Sprite(startX, startY,
                mMosquitoTextureRegion,getVertexBufferObjectManager());
        body[i] = PhysicsFactory.createBoxBody(mPhysicsWorld, sprMosq[i], BodyType.DynamicBody, FIXTURE_DEF);

        sprMosq[i].registerEntityModifier(new SequenceEntityModifier(
                new AlphaModifier(5.0f, 0.0f, 1.0f), 
                new MoveModifier(60.0f, sprMosq[i].getX(), dm.widthPixels/2 , sprMosq[i].getY(), dm.heightPixels/2 , EaseBounceInOut.getInstance())));

        scene.getLastChild().attachChild(sprMosq[i]);
        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(sprMosq[i], body[i], true, true));
        if (nMosq < 50) {
            mHandler.postDelayed(mStartMosq, 5000);
        }
    }
};
4

1 に答える 1

2

正直なところ、この問題では、バグが衝突しているときに何か凝ったことをしたいのでなければ、box2dは必要ありません。これを行うには、IUpdateHandlerを使用する必要があります。私があなたの問題に対してこれをどのように行うかについての大まかな例をあなたに与えるでしょう:

    //Velocity modifier for the bugs. Play with this till you are happy with their speed
    private final velocity = 1.0f;

    sprMosq[i] = new Sprite(startX, startY,
    mMosquitoTextureRegion,getVertexBufferObjectManager());

    //Register update handlers
    sprMosq[i].registerUpdateHandler(new IUpdateHandler(){

        @Override
        public void onUpdate(float pSecondsElapsed) {
            //First check if this sprite collides with any other bug.
            //you can do this by creating a loop which checks every sprite
            //using sprite1.collidesWith(sprite2)

            if(collision){
                doCollisionAction(); //Whatever way you want to do it
            } else {
                float XVelocity;  //Velocity on the X axis
                float YVelocity;  //Velocity on the Y axis
                //Generate a different random number between -1 and 1
                //for both XVelocity and YVelocity. Done using Math.random I believe

                //Move the sprite
                sprMosq[i].setPosition(XVelocity*velocity*pSecondsElapsed,
                                    YVelocity*velocity*pSecondsElapsed); 

            }


        }

        @Override
        public void reset() {
        }

    });

これにより、ゲームのすべてのフレームがレンダリングされるときに、各バグがランダムに移動するはずです。バグを追加して処理能力を消費したい場合は、ランダム性の頻度を減らすことができますが、このアルゴリズムが実行しているように、すべてのフレームよりも少ないチェックを行うことをお勧めします。そのためには、代わりに、特定の期間の後に自動リセットするTimerHandlerを使用することをお勧めします。

于 2012-07-10T19:08:44.500 に答える