2012-02-19 58 views
2

这个触摸事件永远不会触发缓解作用。我一直在通过例子和源代码,现在我对做我想做的最好的方法有些困惑。如何正确使用触摸监听器/事件和andEngine

我画一个精灵到现场。我希望该精灵能够通过EaseFunction移动到用户触摸事件的坐标。

这是到目前为止的代码:

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

// =========================================================== 
// Fields 
// =========================================================== 

private Camera mCamera; 
private BitmapTextureAtlas mTexture; 
private TextureRegion mFaceTextureRegion; 
// =========================================================== 
// Constructors 
// =========================================================== 

// =========================================================== 
// Getter & Setter 
// =========================================================== 

// =========================================================== 
// Methods for/from SuperClass/Interfaces 
// =========================================================== 

@Override 
public Engine onLoadEngine() { 
    this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 
    return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera)); 
} 

@Override 
public void onLoadResources() { 
    this.mTexture = new BitmapTextureAtlas(64, 32, TextureOptions.BILINEAR); 
    this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/boxface.png", 0, 0); 

    this.getEngine().getTextureManager().loadTexture(this.mTexture); 
} 

@Override 
public Scene onLoadScene() { 
    this.getEngine().registerUpdateHandler(new FPSLogger()); 

    final Scene scene = new Scene(); 
    scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f)); 

    final int x = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth())/2; 
    final int y = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight())/2; 
    final Sprite face = new Sprite(x, y, this.mFaceTextureRegion); 
    scene.attachChild(face); 
    face.setPosition(134, 200); 

//这是我尝试实现触摸监听器和代码//是不是?

scene.setOnAreaTouchListener(new IOnAreaTouchListener() { 
     //@Override 
     public boolean onAreaTouched(TouchEvent pSceneTouchEvent,ITouchArea pTouchArea, float pTouchAreaLocalX,float pTouchAreaLocalY) { 
      if(pSceneTouchEvent.isActionDown()) { 
       int facePosX = (int) (pSceneTouchEvent.getX() - face.getWidth()/2); 
       int facePosY = (int) (pSceneTouchEvent.getY() - face.getHeight()/2); 

       face.registerEntityModifier(new MoveModifier(500, face.getX(), facePosX, face.getY(),facePosY,EaseQuadIn.getInstance())); 
      } 
      return false; 
     } 
    }); 
    return scene; 
} 


@Override 
public void onLoadComplete() { 

} 

终于我很确定我需要使用pScene.registerTouchArea(face);和 pScene.setTouchAreaBindingEnabled(true);

任何澄清会有很大的用途。 谢谢

+0

我找到了答案。但即时新的。所以我必须等待4个小时才能回答我自己的帖子。 – 2012-02-19 06:31:44

回答

3

这个作品!只是不停地挖,在这里找到了答案:HTTP://www.andengine.org/forums/tutorials/touchevent-t1490.html

这里是完整的代码。精灵加载到页面的中心。在哪里你点击精灵将'补间'或缓解到你的触摸坐标。

package and.engine.test; 

import org.anddev.andengine.engine.Engine; 
import org.anddev.andengine.engine.camera.Camera; 
import org.anddev.andengine.engine.options.EngineOptions; 
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation; 
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy; 
import org.anddev.andengine.entity.util.FPSLogger; 
import org.anddev.andengine.ui.activity.BaseGameActivity; 
import org.anddev.andengine.entity.modifier.MoveModifier; 
import org.anddev.andengine.entity.scene.Scene; 
import org.anddev.andengine.util.modifier.ease.EaseQuadIn; 
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener; 
import org.anddev.andengine.entity.scene.background.ColorBackground; 
import org.anddev.andengine.entity.sprite.Sprite; 
import org.anddev.andengine.input.touch.TouchEvent; 
import org.anddev.andengine.opengl.texture.TextureOptions; 
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas; 
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory; 
import org.anddev.andengine.opengl.texture.region.TextureRegion; 



public class AndEnginetestActivity extends BaseGameActivity { 
    // =========================================================== 
    // Constants 
    // =========================================================== 

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

    // =========================================================== 
    // Fields 
    // =========================================================== 

    private Camera mCamera; 
    private BitmapTextureAtlas mTexture; 
    private TextureRegion mFaceTextureRegion; 
    // =========================================================== 
    // Constructors 
    // =========================================================== 

    // =========================================================== 
    // Getter & Setter 
    // =========================================================== 

    // =========================================================== 
    // Methods for/from SuperClass/Interfaces 
    // =========================================================== 

    @Override 
    public Engine onLoadEngine() { 
     this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 
     return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera)); 
    } 

    @Override 
    public void onLoadResources() { 
     this.mTexture = new BitmapTextureAtlas(64, 32, TextureOptions.BILINEAR); 
     this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/boxface.png", 0, 0); 

     this.getEngine().getTextureManager().loadTexture(this.mTexture); 
    } 

    @Override 
    public Scene onLoadScene() { 
     this.getEngine().registerUpdateHandler(new FPSLogger()); 

     final Scene scene = new Scene(); 
     scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f)); 

     final int x = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth())/2; 
     final int y = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight())/2; 
     final Sprite face = new Sprite(x, y, this.mFaceTextureRegion); 
     scene.registerTouchArea(face); 
     scene.attachChild(face); 
     //face.setPosition(134, 200); 

     scene.setOnSceneTouchListener(new IOnSceneTouchListener() { 

      @Override 
      public boolean onSceneTouchEvent(Scene pScene,TouchEvent pSceneTouchEvent) { 
       int facePosX = (int) (pSceneTouchEvent.getX() - face.getWidth()/2); 
       int facePosY = (int) (pSceneTouchEvent.getY() - face.getHeight()/2); 
       face.registerEntityModifier(new MoveModifier(1, face.getX(), facePosX,face.getY(), facePosY, EaseQuadIn.getInstance()));    
       return false; 
      } 
     }); 


     scene.setTouchAreaBindingEnabled(true); 
     return scene; 
    } 


    @Override 
    public void onLoadComplete() { 

    } 

    // =========================================================== 
    // Methods 
    // =========================================================== 

    // =========================================================== 
    // Inner and Anonymous Classes 
    // =========================================================== 
}