我是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谷歌搜索了很多,這讓我什麼這一點。