我在andEngine和我的代码中制作了一个音乐钢琴,我创建了两个矩形图像,并且在手指上放置它们播放声音,我在我的代码中使用了scene.setOnAreaTouch(),在其中我使用了isActionMoove()和现在的问题是,当我移动手指时它会播放声音,但它会播放连续的声音,而不是等待停止第一个声音。
这是我的代码请帮助
enter code here
public class TouchActivity extends SimpleBaseGameActivity {
private Camera camera;
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private BitmapTextureAtlas texImage1, texImage2, textimage;
private TextureRegion regImage1, regImage2;
private TiledTextureRegion regCat;
private AnimatedSprite sprCat;
private static int SPR_COLUMN = 2;
private static int SPR_ROWS = 2;
MediaPlayer mediaPlayer;
private Sprite sprImage1, sprImage2;
private Sound sound_piano1, sound_piano2;
@Override
protected void onCreateResources() {
texImage1 = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
regImage1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texImage1, this.getAssets(), "gfx/pinksmall.png", 0, 0);
texImage1.load();
texImage2 = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
regImage2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texImage2, this.getAssets(), "gfx/pinksmall.png", 0, 0);
texImage2.load();
SoundFactory.setAssetBasePath("sounds/");
try {
this.sound_piano1 = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this,
"a4.wav");
this.sound_piano2 = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this,
"c5.wav");
} catch (final IOException e) {
Debug.e(e);
}
}
@Override
protected Scene onCreateScene() {
Scene scene = new Scene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
sprImage1 = new Sprite(0, 0, regImage1, this.getVertexBufferObjectManager());
scene.attachChild(sprImage1);
sprImage2 = new Sprite(50, 0, regImage2, this.getVertexBufferObjectManager());
scene.attachChild(sprImage2);
scene.registerTouchArea(sprImage1);
scene.registerTouchArea(sprImage2);
scene.setOnAreaTouchListener(new IOnAreaTouchListener() {
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY) {
float X = pSceneTouchEvent.getX();
float Y = pSceneTouchEvent.getY();
// if (pSceneTouchEvent.isActionDown()) {
//
// if(sprImage1.contains(X,Y)){
// sound_piano1.play();
//// sound_piano2.setLoopCount(0);
// }
//
// if(sprImage2.contains(X,Y)){
// sound_piano2.play();
//// sound_piano2.setLoopCount(0);
// }
//
//
//
// }
// if (pSceneTouchEvent.isActionOutside()) {
// if(!sprImage2.contains(X,Y)){
// sound_piano2.stop();
// }
// if(!sprImage1.contains(X,Y)){
// sound_piano1.stop();
// }
// }
if(pSceneTouchEvent.isActionMove()){
if(!sprImage1.contains(X,Y) ){
sound_piano1.stop();
}
else {
sound_piano1.play();
}
if(!sprImage2.contains(X,Y) ){
sound_piano2.stop();
}
else {
sound_piano2.play();
}
}
return false;
}
});
return scene;
}
@Override
public EngineOptions onCreateEngineOptions() {
camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), camera);
engineOptions.getAudioOptions().setNeedsSound(true);
return engineOptions;
}
}