3

私は Andengine から始めましたが、画面に触れたときにスプライトを動かすのが困難です (スプライトではありません)。私は本当にあなたの助けが必要です、どうもありがとう。

ここに私のコードがあります:

    @Override
protected Scene onCreateScene() {
    final Scene scene = new Scene();
    Sprite backgroundSprite = new Sprite(0, 0, this.mBackgroundTextureRegion, getVertexBufferObjectManager());
    scene.attachChild(backgroundSprite);
    Carl carl = new Carl(1, 30, 361, this.carl, getVertexBufferObjectManager()){

        };

    scene.attachChild(carl);



    this.mStack1.add(carl);
    carl.setmStack(mStack1);
    scene.registerTouchArea(carl);
    scene.setTouchAreaBindingOnActionDownEnabled(true);


    return scene;
}
public boolean onTouchEvent(MotionEvent event, Carl carl) {

    carl.setPosition(carl.getX() +2,carl.getY() );
    return true;
}
4

2 に答える 2

3

IOnSceneTouchListenerボールを操作できる onSceneTouchEvent メソッドをオーバーライドするよりも、インターフェイスを実装できます。

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
     carl.setPosition(carl.getX() +2,carl.getY() );
    return false;
}

scene.setOnSceneTouchListener(this);ただし、これを追加することを忘れないでくださいprotected Scene onCreateScene()

編集:

boolean canGo = false;

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
    if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN)
        canGo = true;
    if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_UP)
        canGo = false;
    return false;
}

そしてこれはあなたに追加しますpublic Scene onCreateScene() {

scene.registerUpdateHandler(new IUpdateHandler() {
    @Override
    public void reset() {}
    @Override
    public void onUpdate(float pSecondsElapsed) {
        // TODO Auto-generated method stub
        if (canGo)
            ball.setPosition(ball.getX() + 2, ball.getY());
    }
});
于 2012-11-17T19:36:08.030 に答える
0

move修飾子を使用して上下に移動することもできます。

 public class MainActivity extends BaseGameActivity implements IOnSceneTouchListener {

    private static final int CAMERA_WIDTH = 800;
    private static final int CAMERA_HEIGHT = 480;

    private BitmapTextureAtlas characterTextureAtlas;
    private ITiledTextureRegion characterTiledTextureRegion;
    private AnimatedSprite characterAnimatedSprite;

    boolean canGo = false;

    public static final String TAG = "Cowboy#";


    private Scene scene;

    @Override
    public EngineOptions onCreateEngineOptions() {
        Camera camera = new Camera(0,0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), camera);
    }

    @Override
    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException {
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        characterTextureAtlas = new BitmapTextureAtlas(mEngine.getTextureManager(), 640, 320, TextureOptions.BILINEAR);
        characterTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(characterTextureAtlas, this, "walkman640x320.png", 0, 0, 8, 1);
        characterTextureAtlas.load();

        pOnCreateResourcesCallback.onCreateResourcesFinished();
    }

    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException {
        scene = new Scene();
        scene.setOnSceneTouchListener(this);

        characterAnimatedSprite = new AnimatedSprite(120, 120, characterTiledTextureRegion, mEngine.getVertexBufferObjectManager()){


            @Override
            protected void onManagedUpdate(float pSecondsElapsed) {
                super.onManagedUpdate(pSecondsElapsed);
//                Log.d(TAG, "onManagedUpdate: counting#");
            }
        };
        characterAnimatedSprite.animate(50);

        scene.attachChild(characterAnimatedSprite);
        scene.setBackground(new Background(Color.CYAN));
        scene.registerUpdateHandler(new IUpdateHandler() {
            @Override
            public void reset() {}
            @Override
            public void onUpdate(float pSecondsElapsed) {
                // TODO Auto-generated method stub
                //moves character 2 steps forward along x-axis
//                if (canGo){
//                    characterAnimatedSprite.setPosition(characterAnimatedSprite.getX() + 2, characterAnimatedSprite.getY());
//                }
                //moves character 8 steps upward along y-axis and back
//                if (canGo){
//                    characterAnimatedSprite.setPosition(characterAnimatedSprite.getX(), characterAnimatedSprite.getY()+30);
////                    characterAnimatedSprite.setPosition(characterAnimatedSprite.getX(), characterAnimatedSprite.getY()-8);
//                }

                if (canGo){
                    final float duration = 1;
//                    final float duration = 3;
                    final float fromX = characterAnimatedSprite.getX();
                    final float toX = characterAnimatedSprite.getX();
                    final float fromY = characterAnimatedSprite.getY();
                    final float toY = characterAnimatedSprite.getY() + 80;


                    //you can move in on axis using MoveYModifier or MoveXModifier
//                    MoveYModifier mod1=new MoveYModifier(constanttime,fromY,toY);
//                    sprite.registerEntityModifier(mod1);

                    final MoveModifier downMoveModifier = new MoveModifier(0.4f, fromX, toY, toX, fromY, new IEntityModifier.IEntityModifierListener() {
                        @Override
                        public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
                            characterAnimatedSprite.stopAnimation(0);
                        }

                        @Override
                        public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
                            characterAnimatedSprite.animate(50);
                        }
                    });

                    final MoveModifier upMoveModifier = new MoveModifier(0.2f, fromX, fromY, toX, toY, new IEntityModifier.IEntityModifierListener() {
                        @Override
                        public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
                            characterAnimatedSprite.stopAnimation(0);
                        }

                        @Override
                        public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
                            characterAnimatedSprite.registerEntityModifier(downMoveModifier);
                        }
                    });

                    characterAnimatedSprite.registerEntityModifier(upMoveModifier);
                }
            }
        });
        pOnCreateSceneCallback.onCreateSceneFinished(scene);
    }

    @Override
    public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException {
        pOnPopulateSceneCallback.onPopulateSceneFinished();
    }

    @Override
    public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {

        if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN)
            canGo = true;
        if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_UP)
            canGo = false;
        return false;
    }
}
于 2016-06-17T09:41:30.093 に答える