我有一个问题。 我如何使用重力制作角色并可以正常行走,即我需要使用哪些功能以及如何定义灯具?我需要box2d物理世界(我正在使用平铺地图)吗? 所以,如果可以的话,请告诉我如何使用andengine进行像马里奥这样的2D侧滚动平台游戏。
我的代码我正在尝试做什么:
// Character:
charactersprite = new Sprite(40, 0, this.character);
charactersprite.setScaleX(0.65f);
this.mScene.setOnSceneTouchListener( this);
// PHYSICS
final FixtureDef characterfictur = PhysicsFactory.createFixtureDef(0, 0f,0.5f);
this.mScene.registerUpdateHandler(this.mPhysicsWorld);
final Body body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, charactersprite, BodyType.DynamicBody, characterfictur);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(charactersprite, body, true, false));
mScene.attachChild(charactersprite);
createUnwalkableObjects(mTMXTiledMap);
final PhysicsHandler physicsHandler = new PhysicsHandler(charactersprite);
charactersprite.registerUpdateHandler(physicsHandler);
// HUD
HUD my = new HUD();
Sprite forward = new Sprite( 50, CAMERA_HEIGHT - 170, forwardr){
@Override
public boolean onAreaTouched(TouchEvent pEvent, float pX, float pY){
if(!pEvent.isActionUp()){
charactersprite.getTextureRegion().setFlippedHorizontal(false);
body.setLinearVelocity(new Vector2(CHAR_MOVING_SPEED,body.getLinearVelocity().y)); // Don't look at there
//body.applyLinearImpulse(new Vector2(2,0), body.getPosition());
}else{
//body.applyLinearImpulse(new Vector2(0,0), body.getPosition());
physicsHandler.setVelocity(0, 0);
body.setLinearVelocity(new Vector2(0,body.getLinearVelocity().y)); // Don't look at there
}
return false;
}
};
小前锋:
private void createUnwalkableObjects(TMXTiledMap map){
// Loop through the object groups
for(final TMXObjectGroup group: map.getTMXObjectGroups()) {
//if(group.getTMXObjectGroupProperties().containsTMXProperty("Zeme", "true")){
// This is our "wall" layer. Create the boxes from it
for(final TMXObject object : group.getTMXObjects()) {
final Rectangle rect = new Rectangle(object.getX(), object.getY(),object.getWidth(), object.getHeight());
final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0, 0,1f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, rect, BodyType.StaticBody, boxFixtureDef);
rect.setVisible(false);
mScene.attachChild(rect);
}
//}
}
}
所以它无法正常工作。那我在做错什么?请帮我。 非常感谢你!
答案 0 :(得分:4)
您需要的东西:
上面提到的每个'特征'都有它的例子 - 只需检查和引擎示例。
在这个帖子中,我提供了一些关于如何编写此类游戏代码的提示:CLICK