gpt4 book ai didi

android - 为什么多点触控不适用于 andengine GLES-2 SimpleBaseGameActivity?

转载 作者:行者123 更新时间:2023-11-30 03:48:33 27 4
gpt4 key购买 nike

您好,我正在尝试制作一款双人乒乓球游戏,但其中的多点触控部分无法正常工作,我知道我使用的设备支持多点触控,因此我的代码中一定有问题。现在我有两个专用的背景部分,当触摸它们时设置桨的位置但是如果有人的手指在屏幕上它不会识别第二次触摸。

public class TwoPlayerMode extends SimpleBaseGameActivity {

private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private ITextureRegion mField, mField1, mField2, mField3, mPaddle;
private Sprite bottomPaddleSprite, topPaddleSprite, backgroundSpriteP2, backgroundSpriteP1, backgroundSpriteP3;
private String homelife, awaylife;
private int homeCount, awayCount;
Font mFont;
Text scores, lives;
SharedPreferences prefs;
MultiTouchController mtc;

@Override
public EngineOptions onCreateEngineOptions() {
mtc = new MultiTouchController();
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
final FixedStepEngine engine = new FixedStepEngine(new EngineOptions(true,
ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
CAMERA_WIDTH, CAMERA_HEIGHT), camera),60);
if (MultiTouch.isSupported(this)) {
Toast.makeText(this, "Multitouch", Toast.LENGTH_LONG).show();
engine.setTouchController(mtc);
}

return engine.getEngineOptions();

}



@Override
protected void onCreateResources() {
prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
try {

ITexture paddle = new BitmapTexture(this.getTextureManager(),
new IInputStreamOpener() {
@Override
public InputStream open() throws IOException {
boolean light = prefs.getBoolean("theme", false);
if (light) {
return getAssets().open("gfx/paddle2.png");
} else {
return getAssets().open("gfx/paddle.png");
}
}
});
ITexture fieldmid = new BitmapTexture(this.getTextureManager(),
new IInputStreamOpener() {
@Override
public InputStream open() throws IOException {
boolean light = prefs.getBoolean("theme", false);
if (light) {
return getAssets().open("gfx/fieldmid2.png");
} else {
return getAssets().open("gfx/fieldmid.png");
}

}
});
ITexture fieldend = new BitmapTexture(this.getTextureManager(),
new IInputStreamOpener() {
@Override
public InputStream open() throws IOException {
boolean light = prefs.getBoolean("theme", false);
if (light) {
return getAssets().open("gfx/fieldend2.png");
} else {
return getAssets().open("gfx/fieldend.png");
}

}
});

// 2 - Load bitmap textures into VRAM

fieldend.load();
fieldmid.load();
paddle.load();

// 3 - Set up texture regions


this.mField1 = TextureRegionFactory.extractFromTexture(fieldend);
this.mField2 = TextureRegionFactory.extractFromTexture(fieldmid);
this.mField3 = TextureRegionFactory.extractFromTexture(fieldend);
this.mPaddle = TextureRegionFactory.extractFromTexture(paddle);
loadFonts();
} catch (IOException e) {
Debug.e(e);
}

}

public void loadFonts() {
FontFactory.setAssetBasePath("font/");
final ITexture fontTexture = new BitmapTextureAtlas(
getTextureManager(), 256, 256,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
boolean light = prefs.getBoolean("theme", false);
if (light) {
mFont = FontFactory.createFromAsset(getFontManager(), fontTexture,
getAssets(), "plaincre.ttf", 40f, true,
Color.rgb(0, 221, 255));

} else {
mFont = FontFactory.createFromAsset(getFontManager(), fontTexture,
getAssets(), "plaincre.ttf", 40f, true,
Color.rgb(0, 153, 204));

}
mFont.load();

}

@Override
protected Scene onCreateScene() {

final Scene scene = new Scene();

topPaddleSprite = new Sprite(0, 195, this.mPaddle,
getVertexBufferObjectManager());
bottomPaddleSprite = new Sprite(785, 195, this.mPaddle,
getVertexBufferObjectManager());
// my spirtes and background
/*
* Sprite backgroundSprite = new Sprite(0, 0, this.mField,
* getVertexBufferObjectManager()); scene.attachChild(backgroundSprite);
*/
backgroundSpriteP1 = new Sprite(0, 0, this.mField1,
getVertexBufferObjectManager()) {
boolean mGrabbed = false;
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
switch(pSceneTouchEvent.getAction()) {
case TouchEvent.ACTION_DOWN:
if (pSceneTouchEvent.isActionMove()
|| pSceneTouchEvent.isActionDown()) {
topPaddleSprite.setPosition(0f,
pSceneTouchEvent.getY() - topPaddleSprite.getHeight() / 2);
}
this.mGrabbed = true;
break;
case TouchEvent.ACTION_MOVE:
if(this.mGrabbed) {
if (pSceneTouchEvent.isActionMove()
|| pSceneTouchEvent.isActionDown()) {
topPaddleSprite.setPosition(0f,
pSceneTouchEvent.getY() - topPaddleSprite.getHeight() / 2);
} }
break;
case TouchEvent.ACTION_UP:
if(this.mGrabbed) {
this.mGrabbed = false;
if (pSceneTouchEvent.isActionMove()
|| pSceneTouchEvent.isActionDown()) {
topPaddleSprite.setPosition(0f,
pSceneTouchEvent.getY() - topPaddleSprite.getHeight() / 2);
}
}
break;
}
return true;
}
};
backgroundSpriteP2 = new Sprite(backgroundSpriteP1.getWidth(), 0,
this.mField2, getVertexBufferObjectManager());
backgroundSpriteP3 = new Sprite(backgroundSpriteP1.getWidth()
+ backgroundSpriteP2.getWidth(), 0, this.mField3,
getVertexBufferObjectManager()) {
boolean mGrabbed = false;
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
switch(pSceneTouchEvent.getAction()) {
case TouchEvent.ACTION_DOWN:

bottomPaddleSprite.setPosition(785f,
pTouchAreaLocalY - bottomPaddleSprite.getHeight() / 2);

this.mGrabbed = true;
break;
case TouchEvent.ACTION_MOVE:
if(this.mGrabbed) {

bottomPaddleSprite.setPosition(785f,
pSceneTouchEvent.getY() - bottomPaddleSprite.getHeight() / 2);
}
break;
case TouchEvent.ACTION_UP:
if(this.mGrabbed) {
this.mGrabbed = false;

bottomPaddleSprite.setPosition(785f,
pSceneTouchEvent.getY() - bottomPaddleSprite.getHeight() / 2);

}
break;
}
return true;
}
};


scene.registerTouchArea(backgroundSpriteP1);
scene.registerTouchArea(backgroundSpriteP3);
scene.setTouchAreaBindingOnActionMoveEnabled(true);
scene.setTouchAreaBindingOnActionDownEnabled(true);
scene.attachChild(backgroundSpriteP1);
scene.attachChild(backgroundSpriteP2);
scene.attachChild(backgroundSpriteP3);
scene.attachChild(topPaddleSprite);
scene.attachChild(bottomPaddleSprite);


awayCount = 3;
awaylife = "3";
homeCount = 3;
homelife = "3";
scores = new Text(50, 50, mFont, awaylife,
getVertexBufferObjectManager());
lives = new Text(650, 50, mFont, homelife,
getVertexBufferObjectManager());
scene.attachChild(lives);
scene.attachChild(scores);

return scene;
}

}

这是截图 screen shot

最佳答案

看起来像描述的问题here .

发布的代码中没有对 TouchOptions 的引用。

关于android - 为什么多点触控不适用于 andengine GLES-2 SimpleBaseGameActivity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14544436/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com