AndEngine如何让精灵在调用jump方法时不跳转

发布于 2024-11-29 16:28:21 字数 4644 浏览 0 评论 0原文

所以我有这种方法,当我触摸屏幕时,我的精灵会跳跃。现在的问题是,当我连续触摸屏幕时,精灵会一次又一次地跳跃。我想做的是,如果它跳跃,除非我的精灵落地,否则无法调用跳跃方法。

这是代码。

public class PhyiscsActivity extends BaseGameActivity implements IAccelerometerListener, IOnSceneTouchListener{

private static final int CAMERA_WIDTH = 720; private static final int CAMERA_HEIGHT = 480; private Camera mCamera; private BitmapTextureAtlas mBitmapTextureAtlas; private TiledTextureRegion mTextureRegion; private Scene mScene; private FixtureDef mFixtureDef = PhysicsFactory.createFixtureDef(1,-10f, 0.5f); private PhysicsWorld mPhysicsWorld; private Body body; private AnimatedSprite facebox; private FixtureDef wallfixture = PhysicsFactory.createFixtureDef(-1,0.5f, 0.5f); @Override public Engine onLoadEngine() { // TODO Auto-generated method stub this.mCamera = new Camera(0,0,CAMERA_WIDTH, CAMERA_HEIGHT); final EngineOptions mEngineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),this.mCamera); mEngineOptions.getTouchOptions().setRunOnUpdateThread(true); return new Engine(mEngineOptions); } @Override public void onLoadResources() { // TODO Auto-generated method stub this.mBitmapTextureAtlas = new BitmapTextureAtlas(128,128, TextureOptions.BILINEAR_PREMULTIPLYALPHA); this.mTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBitmapTextureAtlas, this, "gfx/face_box_tiled.png", 0, 0, 2, 1); this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas); } @Override public Scene onLoadScene() { // TODO Auto-generated method stub this.mEngine.registerUpdateHandler(new FPSLogger()); this.mScene = new Scene(); this.mScene.setBackground(new ColorBackground(1,1,1)); this.mPhysicsWorld = new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH),false); // This is the walls final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2); final Shape ground = new Rectangle(0,CAMERA_HEIGHT - 2,CAMERA_WIDTH,2); final Shape left = new Rectangle(0,0,2,CAMERA_HEIGHT); final Shape right = new Rectangle(CAMERA_WIDTH -2, 0,2,CAMERA_HEIGHT); PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallfixture); PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallfixture); PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallfixture); PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallfixture); // This is the Sprite facebox = new AnimatedSprite(30,(CAMERA_HEIGHT - 2) - this.mTextureRegion.getHeight() ,this.mTextureRegion); body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, facebox, BodyType.DynamicBody, mFixtureDef); this.mScene.attachChild(facebox); this.mScene.registerUpdateHandler(this.mPhysicsWorld); this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(facebox,body,true,true)); this.mScene.setOnSceneTouchListener(this); return this.mScene; } @Override public void onLoadComplete() { // TODO Auto-generated method stub } //Accelerometer @Override public void onAccelerometerChanged(AccelerometerData pAccelerometerData) { // TODO Auto-generated method stub final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX()* 3, 10); this.mPhysicsWorld.setGravity(gravity); Vector2Pool.recycle(gravity); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); this.enableAccelerometerSensor(this); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); this.disableAccelerometerSensor(); } //This is where i make the sprite jump @Override public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) { pSceneTouchEvent.isActionDown();{ this.jump(facebox); } return false; } public void jump(AnimatedSprite sprite){ boolean jumping = false; if(!jumping ){ body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-8f)); } } public void jump(AnimatedSprite sprite){ if(!jumping){ body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-10)); jumping = true; } } @Override public void beginContact(Contact contact) { // TODO Auto-generated method stub jumping = true; } @Override public void endContact(Contact contact) { // TODO Auto-generated method stub jumping = false; }

我不知道为什么 beginContact 和 endContact 没有初始化。我应该做些什么来初始化它吗?例如更新处理程序?联系人监听器?

So I have this method that when i touch the screen my sprite will jump. Now the problem is when I continuously touch the screen the sprite will jump again and again. What I want to do is if it jumps, the jump method cannot be called unless my sprite hits the ground.

Here is the code.

public class PhyiscsActivity extends BaseGameActivity implements IAccelerometerListener, IOnSceneTouchListener{

private static final int CAMERA_WIDTH = 720; private static final int CAMERA_HEIGHT = 480; private Camera mCamera; private BitmapTextureAtlas mBitmapTextureAtlas; private TiledTextureRegion mTextureRegion; private Scene mScene; private FixtureDef mFixtureDef = PhysicsFactory.createFixtureDef(1,-10f, 0.5f); private PhysicsWorld mPhysicsWorld; private Body body; private AnimatedSprite facebox; private FixtureDef wallfixture = PhysicsFactory.createFixtureDef(-1,0.5f, 0.5f); @Override public Engine onLoadEngine() { // TODO Auto-generated method stub this.mCamera = new Camera(0,0,CAMERA_WIDTH, CAMERA_HEIGHT); final EngineOptions mEngineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),this.mCamera); mEngineOptions.getTouchOptions().setRunOnUpdateThread(true); return new Engine(mEngineOptions); } @Override public void onLoadResources() { // TODO Auto-generated method stub this.mBitmapTextureAtlas = new BitmapTextureAtlas(128,128, TextureOptions.BILINEAR_PREMULTIPLYALPHA); this.mTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBitmapTextureAtlas, this, "gfx/face_box_tiled.png", 0, 0, 2, 1); this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas); } @Override public Scene onLoadScene() { // TODO Auto-generated method stub this.mEngine.registerUpdateHandler(new FPSLogger()); this.mScene = new Scene(); this.mScene.setBackground(new ColorBackground(1,1,1)); this.mPhysicsWorld = new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH),false); // This is the walls final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2); final Shape ground = new Rectangle(0,CAMERA_HEIGHT - 2,CAMERA_WIDTH,2); final Shape left = new Rectangle(0,0,2,CAMERA_HEIGHT); final Shape right = new Rectangle(CAMERA_WIDTH -2, 0,2,CAMERA_HEIGHT); PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallfixture); PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallfixture); PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallfixture); PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallfixture); // This is the Sprite facebox = new AnimatedSprite(30,(CAMERA_HEIGHT - 2) - this.mTextureRegion.getHeight() ,this.mTextureRegion); body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, facebox, BodyType.DynamicBody, mFixtureDef); this.mScene.attachChild(facebox); this.mScene.registerUpdateHandler(this.mPhysicsWorld); this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(facebox,body,true,true)); this.mScene.setOnSceneTouchListener(this); return this.mScene; } @Override public void onLoadComplete() { // TODO Auto-generated method stub } //Accelerometer @Override public void onAccelerometerChanged(AccelerometerData pAccelerometerData) { // TODO Auto-generated method stub final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX()* 3, 10); this.mPhysicsWorld.setGravity(gravity); Vector2Pool.recycle(gravity); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); this.enableAccelerometerSensor(this); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); this.disableAccelerometerSensor(); } //This is where i make the sprite jump @Override public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) { pSceneTouchEvent.isActionDown();{ this.jump(facebox); } return false; } public void jump(AnimatedSprite sprite){ boolean jumping = false; if(!jumping ){ body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-8f)); } } public void jump(AnimatedSprite sprite){ if(!jumping){ body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-10)); jumping = true; } } @Override public void beginContact(Contact contact) { // TODO Auto-generated method stub jumping = true; } @Override public void endContact(Contact contact) { // TODO Auto-generated method stub jumping = false; }

I don't know why the beginContact and endContact isn't initialzing. are there things that i should do to initialize this? example like updating a handler? contactlistener?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕梦 2024-12-06 16:28:21

您需要有一种方法来告诉您玩家是否在跳跃。

public void jump(AnimatedSprite sprite){

    if( isJumping(sprite)){
        body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-8f));        
        }

在此方法中,您应该设置计算玩家是否跳跃的方法。就像他正在触摸墙壁一样(如果玩家的身体(x,y)在墙壁(矩形)中)

好吧,你正在使用box2d来制作物理=)

你必须创建一个ContactListener,你必须实现这个类和函数你想添加。
http://code.google.com/p/andenginephysicalsbox2dextension/source/browse/src/com/badlogic/gdx/physicals/box2d/ContactListener.java?r=1605f6e82f710ef9ebbe07632d6b055239d3b520

public void beginContact (Contact contact);
public void endContact (Contact contact);

联系人对象将计算您的对象(Fixture)令人感动。你只需要检查,当身体接触时,将jump设置为false;当你调用jump并且你可以跳跃时,将jump设置为true;

好的?

@Override
public void beginContact(Contact contact) {
    // TODO Auto-generated method stub
    jumping = true;
}

@Override
public void endContact(Contact contact) {
    // TODO Auto-generated method stub
jumping = false;
}

这是错误的..我只会这样想:

@Override
public void beginContact(Contact contact) {
    jumping = false; //you touched ground so you aren't jumping anymore
}

@Override
public void endContact(Contact contact) {
jumping = true; //you leave ground so you're jumping
}

和一种跳跃方法(不是两种):

   public void jump(AnimatedSprite sprite){
        if(!jumping ){
            jumping = true;
            body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-8f));        
            }
        }

你看到了吗?

You need to have a method wich tell you if your player is jumping or not.

public void jump(AnimatedSprite sprite){

    if( isJumping(sprite)){
        body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-8f));        
        }

In this method you should put the way to compute if the player is jumping. Like if he is touching a wall (if player's body (x,y) is in a wall (rectangle))

Ok you are using box2d to make physics =)

You have to create a ContactListener, you have to implement this class and the function ou want to add.
http://code.google.com/p/andenginephysicsbox2dextension/source/browse/src/com/badlogic/gdx/physics/box2d/ContactListener.java?r=1605f6e82f710ef9ebbe07632d6b055239d3b520

public void beginContact (Contact contact);
public void endContact (Contact contact);

The contact object will countain your objects (Fixture) that are touching. You just have to check and set jumping to false when the body is touching and true when you call jump and you can jump, set jumping to true;

Ok?

@Override
public void beginContact(Contact contact) {
    // TODO Auto-generated method stub
    jumping = true;
}

@Override
public void endContact(Contact contact) {
    // TODO Auto-generated method stub
jumping = false;
}

This is false.. I would think of it like that only:

@Override
public void beginContact(Contact contact) {
    jumping = false; //you touched ground so you aren't jumping anymore
}

@Override
public void endContact(Contact contact) {
jumping = true; //you leave ground so you're jumping
}

and one jump method (not two):

   public void jump(AnimatedSprite sprite){
        if(!jumping ){
            jumping = true;
            body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-8f));        
            }
        }

You see?;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文