本文介绍了Andengine,如何移动精灵带触摸屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始Andengine,和有困难的时候我触摸屏幕(不是精灵)移动我的精灵。我真的需要你的帮助,非常感谢。

这是我的code:

  @覆盖
保护场景onCreateScene(){
    最后一幕一幕=新场景();
    精灵backgroundSprite =新精灵(0,0,this.mBackgroundTextureRegion,getVertexBufferObjectManager());
    scene.attachChild(backgroundSprite);
    卡尔卡尔=新卡尔(1,30,361,this.carl,get​​VertexBufferObjectManager()){        };    scene.attachChild(卡尔);    this.mStack1.add(卡尔);
    carl.setmStack(mStack1);
    scene.registerTouchArea(卡尔);
    scene.setTouchAreaBindingOnActionDownEnabled(真);
    返回现场;
}
公共布尔onTouchEvent(MotionEvent事件,卡尔·卡尔){    carl.setPosition(carl.getX()+2,carl.getY());
    返回true;
}


解决方案

您可以实施 IOnSceneTouchListener 接口,比覆盖onSceneTouchEvent方法,在这里你可以与你的球

  @覆盖
公共布尔onSceneTouchEvent(场景pScene,的TouchEvent pSceneTouchEvent){
     carl.setPosition(carl.getX()+2,carl.getY());
    返回false;
}

但不要忘了添加此 scene.setOnSceneTouchListener(本); 保护场景onCreateScene()

编辑:

 布尔甘果= FALSE;@覆盖
公共布尔onSceneTouchEvent(场景pScene,的TouchEvent pSceneTouchEvent){
    如果(pSceneTouchEvent.getAction()== MotionEvent.ACTION_DOWN)
        中国民促会=真;
    如果(pSceneTouchEvent.getAction()== MotionEvent.ACTION_UP)
        中国民促会= FALSE;
    返回false;
}

和添加到您的公共场景onCreateScene(){

  scene.registerUpdateHandler(新IUpdateHandler(){
    @覆盖
    公共无效复位(){}
    @覆盖
    公共无效的onUpdate(浮动pSecondsElapsed){
        // TODO自动生成方法存根
        如果(中国民促会)
            ball.setPosition(ball.getX()+2,ball.getY());
    }
});

I begin with Andengine, and have difficulties to move my sprite when i touch the screen ( not the sprite ). I really need your help, thanks a lot.

here is my code:

    @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;
}
解决方案

You can implement IOnSceneTouchListener interface, than override onSceneTouchEvent method, where you can act with your ball

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

but don't forget to add this scene.setOnSceneTouchListener(this); to protected Scene onCreateScene()

EDIT:

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;
}

and this add to your 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());
    }
});

这篇关于Andengine,如何移动精灵带触摸屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 21:42