2014-01-20 76 views
0

我是andengine的新手。 作爲一個基本的練習,我嘗試了一個遊戲,在這個遊戲中,當你按下一個按鈕時,精靈會向左移動,當我按下另一個按鈕時,精靈會向右移動。精靈應該向左或向右移動。儘管任何按鈕但是,按照我現在的代碼,當我按下任何按鈕時,精靈向左或向右移動10個像素,然後停止,無論持續按住多長時間。 所以這裏是代碼。按下TouchArea時如何做某事

package com.example.and_tryout; 

import org.andengine.engine.camera.Camera; 
import org.andengine.engine.options.EngineOptions; 
import org.andengine.engine.options.ScreenOrientation; 
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy; 
import org.andengine.entity.modifier.MoveXModifier; 
import org.andengine.entity.scene.Scene; 
import org.andengine.entity.sprite.Sprite; 
import org.andengine.input.touch.TouchEvent; 
import org.andengine.opengl.texture.ITexture; 
import org.andengine.opengl.texture.bitmap.BitmapTexture; 
import org.andengine.opengl.texture.region.ITextureRegion; 
import org.andengine.opengl.texture.region.TextureRegionFactory; 
import org.andengine.ui.activity.SimpleBaseGameActivity; 
import org.andengine.util.adt.io.in.IInputStreamOpener; 
import org.andengine.util.debug.Debug; 

import java.io.IOException; 
import java.io.InputStream; 

public class MainActivity extends SimpleBaseGameActivity { 
    private static int CAMERA_WIDTH = 800; 
    private static int CAMERA_HEIGHT = 480; 
    private ITextureRegion mtexture; 
    private ITextureRegion mleft,mright; 

    @Override 
    public EngineOptions onCreateEngineOptions() { 
     // TODO Auto-generated method stub 
     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 
    protected void onCreateResources() { 
    // TODO Auto-generated method stub 
     try{ 
      ITexture texture = new BitmapTexture (this.getTextureManager(), new IInputStreamOpener(){ 

       @Override 
       public InputStream open() throws IOException { 
        // TODO Auto-generated method stub 
        return getAssets().open("png/ring2.png"); 
       } 

      }); 
      ITexture leftt = new BitmapTexture(this.getTextureManager(),new IInputStreamOpener(){ 

       @Override 
       public InputStream open() throws IOException { 
        // TODO Auto-generated method stub 
        return getAssets().open("png/left.png"); 
       } 
      }); 
      ITexture rightt = new BitmapTexture(this.getTextureManager(),new IInputStreamOpener(){ 

       @Override 
       public InputStream open() throws IOException { 
        // TODO Auto-generated method stub 
        return getAssets().open("png/right.png"); 
       } 
      }); 
      texture.load(); 
      leftt.load(); 
      rightt.load(); 
      this.mtexture = TextureRegionFactory.extractFromTexture(texture); 
      this.mleft = TextureRegionFactory.extractFromTexture(leftt); 
      this.mright = TextureRegionFactory.extractFromTexture(rightt); 

      }catch(IOException e){ 
       Debug.e (e); 
      } 

    } 

    @Override 
    protected Scene onCreateScene() { 
     Scene scene = new Scene(); 
     scene.getBackground().setColor(0.09804f, 0.6274f, 0.8784f); 
     final Sprite spr = new Sprite(0,0,mtexture,getVertexBufferObjectManager()); 
     Sprite left = new Sprite(0,420,60,60,mleft,getVertexBufferObjectManager()){ 
      @Override 
      public boolean onAreaTouched(TouchEvent event,float X,float Y){ 
       spr.registerEntityModifier(new MoveXModifier(0.5f,spr.getX(),spr.getX()-10)); 
       return true; 
      } 

     }; 
     Sprite right = new Sprite(740,420,60,60,mright,getVertexBufferObjectManager()){ 
      @Override 
      public boolean onAreaTouched(TouchEvent event,float X,float Y){ 
       spr.registerEntityModifier(new MoveXModifier(0.5f,spr.getX(),spr.getX()+10)); 
       return true; 
      } 
     }; 
     scene.attachChild(spr); 
     scene.attachChild(left); 
     scene.attachChild(right); 
     scene.registerTouchArea(left); 
     scene.registerTouchArea(right); 
     return scene; 
    } 

} 

我想不出任何solution.I've谷歌搜索了很多,這讓我什麼這一點。

回答

1

地方條件接觸下來,潤色,並觸摸移動在onTouchArea精靈如下:

  public boolean onAreaTouched(TouchEvent event,float X,float Y){ 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        spr.registerEntityModifier(new MoveXModifier(0.5f,spr.getX(),spr.getX()-10)); 
     } 
     if (event.getAction() == MotionEvent.ACTION_UP) { 
} 
    } 
0
Try this 

    Sprite left = new Sprite(0,420,60,60,mleft,getVertexBufferObjectManager()) 
    { 
       @Override 
       public boolean onAreaTouched(TouchEvent event,float X,float Y) 
       { 

        if (event.getAction() == MotionEvent.ACTION_DOWN) 
        { 
         spr.clearEntityModifiers() 
         spr.registerEntityModifier(new MoveXModifier(0.5f,spr.getX(),spr.getX()-10)); 
        }     
        return true; 
       } 
    }; 
    Sprite right = new Sprite(740,420,60,60,mright,getVertexBufferObjectManager()) 
    { 
       @Override 
       public boolean onAreaTouched(TouchEvent event,float X,float Y) 
       { 
        if (event.getAction() == MotionEvent.ACTION_DOWN) 
        { 

Blockquote 

         spr.clearEntityModifiers() 
         spr.registerEntityModifier(new MoveXModifier(0.5f,spr.getX(),spr.getX()+10)); 
        }  
        return true; 
       } 
    }; 
0

這是更好地使用PhysicsHandler移動精靈與velocity.Modifiers不可以直到他們完成,這就是爲什麼你的精靈不會再繼續下去,即使你按下按鈕。

否則,如果你想只是簡單的連續移動你可以簡單地增加精靈的位置。

@Override 
       public boolean onAreaTouched(TouchEvent event,float X,float Y) 
       { 

        if (event.getAction() == MotionEvent.ACTION_DOWN) 
        { 
         spr.setPosition(spr.getX()+1, spr.getY()); 
        }     
        return true; 
       } 

爲左運動做相同與spr.getX() - 1

相關問題