gpt4 book ai didi

java - AndEngine - 删除特定的 Sprite 及其主体

转载 作者:太空宇宙 更新时间:2023-11-03 11:52:19 25 4
gpt4 key购买 nike

我想在单击项目时删除正文。如您所见,我已将其设置为墙面固定装置。

当点击项目时, Sprite 被移除,现在我只想移除 body ..

public void addSprites(Scene scene, int x,int y,int width,int height,String type,Body body){
Sprite sprite = null;

if(type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_WOOD)) {
sprite = new Sprite(x, y, width, height, this.wood, this.getVertexBufferObjectManager()){
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
mScene.detachChild(this);
mPhysicsWorld.destroyBody(woodBody);
return true;
}

};
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
woodBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, sprite, BodyType.StaticBody, wallFixtureDef);
mScene.registerTouchArea(sprite);
Log.e("TYPE", "Wood");
}
scene.attachChild(sprite);
}

我想做的是当点击该项目时我想删除 Sprite 和它所附着的 body 。我的代码有效,但唯一的问题是它删除了所有的 body ,我使用该方法将项目附加到一个级别,所以它总共有 3 个项目,当点击一个时, body 从所有 Sprite 中删除,当它应该只是被点击的那个。

有人知道怎么做吗?

最佳答案

我不确定,但可能会发生这种情况,因为您对所有主体使用相同的变量

woodBody =  PhysicsFactory.createBoxBody(this.mPhysicsWorld, sprite, BodyType.StaticBody, wallFixtureDef);

为了在 body 和 sprite 之间建立良好的联系,我建议您扩展 Sprite 并在您的类中声明 body 变量。例如我就是这样做的

public class Ball extends Sprite{

final FixtureDef ballFixtureDef = PhysicsFactory.createFixtureDef(1.0f, 0.0f, 0.0f, false, Main.CATEGORYBIT_BALL, Main.MASKBITS_BALL, (short)0);

Body body;
float velocityX, velocityY;
int type;

public Ball(float pX, float pY, TextureRegion pTextureRegion, PhysicsWorld pWorld, float velocityX, float velocityY, int type)
{
super(pX, pY, pTextureRegion);
this.type = type;
this.velocityX = velocityX;
this.velocityY = velocityY;
body = PhysicsFactory.createCircleBody(pWorld, this, BodyType.DynamicBody, ballFixtureDef);
body.setUserData(Ball.this);
pWorld.registerPhysicsConnector(new PhysicsConnector(this, body, true, true));
}
}

以及用body销毁sprite的方法

private void destroyBall(final Ball ball)
{
this.runOnUpdateThread(new Runnable(){

@Override
public void run() {

final Body body = ball.body;
mPhysicsWorld.unregisterPhysicsConnector(mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(ball));
mPhysicsWorld.destroyBody(body);
mScene.detachChild(ball);
ballsList.remove(ball);
}});

}

你只是用那种方式创建对象

Ball b = new Ball(float pX, float pY, TextureRegion pTextureRegion, PhysicsWorld pWorld, float velocityX, float velocityY, int type) 
{
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
{
destroyBall(this);
}
}

关于java - AndEngine - 删除特定的 Sprite 及其主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9526600/

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