屏幕不会注册触摸,其中一个精灵/体已被删除(Box2D的)? [英] Screen won't register a touch where a sprite/body has been removed (Box2D)?
问题描述
我使用的是AndEngine / Box2D的创建一个游戏。当游戏加载,包括精灵和机构一类的阵列创建。当用户触摸屏幕时,为了下一个子画面被安装到现场和它的主体被设置为活动状态。精灵/体可以在以后予以销毁。要做到这一点,我取下精灵,并设置身体不活动。然而,在这一点上,现场不再注册的触摸。当拖着精灵/身体进入的路径,它停止。然而,现场的其他机构不与它进行交互。这使我相信这是与触摸错误。这是我的code:
私人无效destroyFiller(){//删除填料
如果(填料[fillerNum]。主动){
填料[fillerNum]。主动= FALSE;
mPhysicsWorld.unregisterPhysicsConnector(mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(filler[fillerNum].sprite));
填料[fillerNum] .body.setUserData(毁灭);
scene.detachChild(填料[fillerNum] .sprite);
fillerCount--;
fillersLeftText.setText(球左:+ Integer.toString(fillerCount));
如果(fillerCount == 0)
游戏结束();
}
}
和它套在身上为无效
scene.registerUpdateHandler(新IUpdateHandler(){
@覆盖
公共无效复位(){
}
@覆盖
公共无效的OnUpdate(浮动pSecondsElapsed){
如果(fillerNum> -1){
如果(填料[fillerNum] .body.getUserData()。等于(毁灭)){//破阵,它只能在更新完成身体
填料[fillerNum] .body.setActive(假);
填料[fillerNum] .body.setUserData(破坏);
如果(soundOn)
popSound.play();
}
这是现场的触摸事件:
公共布尔onSceneTouchEvent(场景pScene,的TouchEvent pSceneTouchEvent){
如果(this.mPhysicsWorld = NULL和放大器;!&安培;!mEngine = NULL){
如果(pSceneTouchEvent.isActionDown()){
Log.e(SceneTouchEvent,触摸); //动它通过destroyFiller身体时,这行不执行
createFiller(pSceneTouchEvent.getX(),pSceneTouchEvent.getY());
返回true;
}
}
返回false;
}
和createFiller如果需要的话:
私人无效createFiller(浮X,浮动Y){
fillerNum ++;
填料[fillerNum]。主动= TRUE;
填料[fillerNum] .sprite.setPosition(X-fillerTR.getWidth()/ 2,Y - fillerTR.getHeight()/ 2);
scene.registerTouchArea(填料[fillerNum] .sprite);
填料[fillerNum] .body.setUserData(补);
填料[fillerNum] .body.setActive(真正的);
mPhysicsWorld.registerPhysicsConnector(新PhysicsConnector(填料[fillerNum] .sprite,填料[fillerNum]。体,真,真));
scene.attachChild(填料[fillerNum] .sprite);
}
尝试使用unregisterTouchArea在你的删除方法
也尝试使用它在onUpdateThread
I'm using AndEngine/Box2D to create a game. When the game loads, an array of a class containing sprites and bodies are created. When the user touches the screen, the next sprite in order is attached to the scene and it's body is set to active. The sprite/body can later be destroyed. To do this, I detach the sprite and set the body to inactive. However, at that point, the scene no longer registers a touch. When dragging a sprite/body into the path, it stops. However, other bodies on the scene do not interact with it. This leads me to believe it has something to do with a touch error. Here is my code:
private void destroyFiller(){ //Deletes filler
if(filler[fillerNum].active){
filler[fillerNum].active=false;
mPhysicsWorld.unregisterPhysicsConnector(mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(filler[fillerNum].sprite));
filler[fillerNum].body.setUserData("destroy");
scene.detachChild(filler[fillerNum].sprite);
fillerCount--;
fillersLeftText.setText("Balls left: "+Integer.toString(fillerCount));
if(fillerCount==0)
gameOver();
}
}
and where it sets the body to inactive
scene.registerUpdateHandler(new IUpdateHandler() {
@Override
public void reset() {
}
@Override
public void onUpdate(float pSecondsElapsed) {
if(fillerNum>-1){
if(filler[fillerNum].body.getUserData().equals("destroy")){ //"Destroys" the body which can only be done on an update
filler[fillerNum].body.setActive(false);
filler[fillerNum].body.setUserData("destroyed");
if(soundOn)
popSound.play();
}
This is the scene touch event:
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
if(this.mPhysicsWorld != null && mEngine != null) {
if(pSceneTouchEvent.isActionDown()) {
Log.e("SceneTouchEvent","Touch"); //This line doesn't execute when touching a body that passed through destroyFiller
createFiller(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
return true;
}
}
return false;
}
And createFiller if needed:
private void createFiller(float x, float y) {
fillerNum++;
filler[fillerNum].active=true;
filler[fillerNum].sprite.setPosition(x-fillerTR.getWidth()/2,y - fillerTR.getHeight()/2);
scene.registerTouchArea(filler[fillerNum].sprite);
filler[fillerNum].body.setUserData("fill");
filler[fillerNum].body.setActive(true);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(filler[fillerNum].sprite, filler[fillerNum].body, true, true));
scene.attachChild(filler[fillerNum].sprite);
}
try to use unregisterTouchArea in your deleting method
also try to use it in onUpdateThread
这篇关于屏幕不会注册触摸,其中一个精灵/体已被删除(Box2D的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!