2013-02-19 113 views

回答

0

下面是从AndEngine的TouchDragExample:

public class TouchDragExample extends SimpleBaseGameActivity { 
    // =========================================================== 
    // Constants 
    // =========================================================== 

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

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

    private BitmapTextureAtlas mBitmapTextureAtlas; 
    private ITextureRegion mFaceTextureRegion; 

    // =========================================================== 
    // Constructors 
    // =========================================================== 

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

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

    @Override 
    public EngineOptions onCreateEngineOptions() { 
     Toast.makeText(this, "Touch & Drag the face!", Toast.LENGTH_LONG).show(); 

     final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 

     return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera); 
    } 

    @Override 
    public void onCreateResources() { 
     BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 

     this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR); 
     this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0); 
     this.mBitmapTextureAtlas.load(); 
    } 

    @Override 
    public Scene onCreateScene() { 
     this.mEngine.registerUpdateHandler(new FPSLogger()); 

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

     final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth())/2; 
     final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight())/2; 
     final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager()) { 
      @Override 
      public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) { 
       this.setPosition(pSceneTouchEvent.getX() - this.getWidth()/2, pSceneTouchEvent.getY() - this.getHeight()/2); 
       return true; 
      } 
     }; 
     face.setScale(4); 
     scene.attachChild(face); 
     scene.registerTouchArea(face); 
     scene.setTouchAreaBindingOnActionDownEnabled(true); 

     return scene; 
    } 

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

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

您还可以将图片用pathmodifiers各地:

yourBackgroundSprite.registerEntityModifier(new PathModifier(parameters...)); 

,也可以使用setChaseEntity()遵循一些其他实体比设定相机你想要平移的图像。

最后,看看自带AndEngine的AutoParallaxBackgroundExample。这不仅会给你一个移动背景的例子,而且会给你一个背景多层视差层的例子。

+0

谢谢您的回答,但我的意思是,如果我有一个lagrge图像,并将其设置为场景背景,则只能看到我的手机背景的一部分。如何使用手指移动/滚动/平移/拖到场景中背景和看到背景的其他部分? – kylin17 2013-02-20 06:40:58

+0

@ kylin17这是否做到这一点? – 2013-02-20 06:54:04

+0

是的,它的工作原理!感谢您的帮助! – kylin17 2013-02-20 07:19:00