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

您还可以使用移动修饰符上下移动。

 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 回答