问题描述
我正在学习如何使用Box2D的和andEngine。我试图让我的身体与移动精灵。我做到了,当我在onCreateScene codeD之前的一切工作,但现在我想打一个单独的类我的精灵。所以现在我onCreateScene看起来是这样的:
I'm learning how to use box2d and andEngine. I am trying to make my sprite with body move. I made it work before when I coded everything in onCreateScene, but now I want to make a separate class for my sprite. So right now my onCreateScene looks like this:
mScene = new Scene();
mScene.registerUpdateHandler(physicsWorld);
Kapsel kapselBialy = new Kapsel(100, 100, 100, 100, ResourceManager.getInstance().mBialyKapselRegion, getVertexBufferObjectManager(), physicsWorld);
mScene.registerTouchArea(kapselBialy);
mScene.setTouchAreaBindingOnActionDownEnabled(true);
mScene.attachChild(kapselBialy);
pOnCreateSceneCallback.onCreateSceneFinished(mScene);
和我Kapsel类看起来是这样的:
And my Kapsel class looks like this:
public class Kapsel extends Sprite {
private Body body;
public Kapsel(float pX, float pY, float pWidth, float pHeight,
ITextureRegion pTextureRegion,
VertexBufferObjectManager pVertexBufferObjectManager, PhysicsWorld physicsWorld) {
super(pX, pY, pWidth, pHeight, pTextureRegion, pVertexBufferObjectManager);
createPhysics(physicsWorld);
}
//definiowanie zahchowań po dotyku
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY){
if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
body.setLinearVelocity(-((pSceneTouchEvent.getX()/32 - body.getPosition().x) * 10), -((pSceneTouchEvent.getY()/32 - body.getPosition().y) * 10));
}
return true;
}
//Tworzenie ciała i fizyki dla kapsla
private void createPhysics(PhysicsWorld physicsWorld){
body = PhysicsFactory.createCircleBody(physicsWorld, this, BodyType.DynamicBody, PhysicsFactory.createFixtureDef(1.0f, 0.5f, 0.5f));
physicsWorld.registerPhysicsConnector(new PhysicsConnector(this, body, true, true));
}
}
我的精灵显示正常,但触摸事件不起作用。我缺少的东西吗?
My sprite shows properly, but the touch event doesn't work. Am I missing something?
推荐答案
确定这样的问题是,在 onPopulateScene()
我没有把 pOnPopulateSceneCallback.onPopulateSceneFinished();
。事实证明,即使你在onCreateScene定义场景和精灵()它仍然需要牛逼把在onPopulateScene()。不过我会记住,我不应该把'这个'中的构造函数(即使它的工作原理:))。
Ok so the problem was that in onPopulateScene()
i did not put pOnPopulateSceneCallback.onPopulateSceneFinished();
. Turns out that even if you define your scene and sprites in onCreateScene() it is still needed t put that in onPopulateScene(). Still I will keep in mind that I should not put 'this' in constructor (even if it works :) ).
这篇关于andEngine:为什么触摸事件不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!