当我触摸屏幕时,如何使用moveYModifier跳转精灵

时间:2014-04-27 03:07:45

标签: sprite andengine

我还是编程android的新手。我的游戏项目有问题。问题是我触摸屏幕时spj跳跃的moveYModifier有问题。其他问题是当我尝试制作触摸事件方法时出现错误。有人可以帮助我,所以我可以理解如何正确编写方法。我没有使用引擎扩展,因为我认为使用moveYModifier足够了。我已经找到了答案,但我仍然对此感到困惑。感谢。

这里是我的代码:

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.modifier.MoveYModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.background.AutoParallaxBackground;
import org.anddev.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import  org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

public class KetigaMainActivity extends BaseGameActivity implements IOnSceneTouchListener{
private int CAMERA_WIDTH = 800;
private int CAMERA_HEIGHT = 480;
private BitmapTextureAtlas bitmapTextureAtlas;
private TiledTextureRegion playerTextureRegion;

private BitmapTextureAtlas autoParallaxBackgroundTexture;

private TextureRegion parallaxLayerBack;
private TextureRegion parallaxLayerMid;
private TextureRegion parallaxLayerFront;
private TextureRegion parallaxLayerBackMid;
private int jumpHeight = 100;
private int jumpDuration = 2;

private int playerX = CAMERA_WIDTH/2;
private int playerY = CAMERA_HEIGHT - playerTextureRegion.getTileHeight() - (parallaxLayerFront.getHeight()/3);


@Override
public Engine onLoadEngine() {
    final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera));

}
@Override
public void onLoadResources(){

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    this.bitmapTextureAtlas = new BitmapTextureAtlas(512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    this.playerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bitmapTextureAtlas, this, "ulat10.png",0,0,4,2);
    this.autoParallaxBackgroundTexture = new BitmapTextureAtlas(1024, 1024, TextureOptions.DEFAULT);
    this.parallaxLayerFront = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.autoParallaxBackgroundTexture, this, "tanah6.png",0,810);
    this.parallaxLayerBack = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.autoParallaxBackgroundTexture, this, "background1.png",0,0);
    this.parallaxLayerBackMid = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.autoParallaxBackgroundTexture, this, "gunung3.png",0,490);
    this.parallaxLayerMid = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.autoParallaxBackgroundTexture, this, "awan5.png",0,700);
    this.mEngine.getTextureManager().loadTextures(this.bitmapTextureAtlas, this.autoParallaxBackgroundTexture);
}
@Override
public Scene onLoadScene(){
    new Scene();
    this.mEngine.registerUpdateHandler(new FPSLogger());

    final Scene scene = new Scene();
    final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 5);
    autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(0.0f, new Sprite(0, CAMERA_HEIGHT - this.parallaxLayerBack.getHeight(), this.parallaxLayerBack)));
    autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-1/4.0f, new Sprite(0, CAMERA_HEIGHT - this.parallaxLayerBackMid.getHeight() - (parallaxLayerFront.getHeight()/6), this.parallaxLayerBackMid)));
    autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-1/2.0f, new Sprite(0, 0,this.parallaxLayerMid)));
    autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-3.0f, new Sprite(0, CAMERA_HEIGHT - this.parallaxLayerFront.getHeight(), this.parallaxLayerFront)));
    scene.setBackground(autoParallaxBackground);

    AnimatedSprite player = new AnimatedSprite(playerX, playerY, this.playerTextureRegion);
    player.setScaleCenterY(this.playerTextureRegion.getTileHeight());
    player.animate(new long[]{100, 100, 100},0 ,2, true);
    scene.setOnSceneTouchListener(this);

    scene.attachChild(player);



    return scene;
}
@Override   
public void onLoadComplete(){
}   
@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
    if (pSceneTouchEvent.isActionDown()) {
        jump(); // this where I got an error
    }
    return false;   
}
public boolean jump(AnimatedSprite player){

final MoveYModifier moveUpModifier = new MoveYModifier(jumpDuration  /2, playerY,   playerY + jumpHeight);
    final MoveYModifier moveDownModivier = new MoveYModifier(jumpDuration  /2, playerY + jumpHeight, playerY );
    final SequenceEntityModifier modifier = new         SequenceEntityModifier(moveUpModifier, moveDownModivier);
    player.registerEntityModifier(new SequenceEntityModifier (modifier));

    return true;
    }
}

2 个答案:

答案 0 :(得分:0)

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) 
{
    if (pSceneTouchEvent.isActionDown()) 
    {
        jump(player); 
    }
    return false;   
}

答案 1 :(得分:0)

您可以使用JumpModifier

 public boolean jump(AnimatedSprite player){
        //your innitial y-position
        final float innitialYPosition = 120;

        //-140 means jump upward, positive move downward
        JumpModifier jumpModifier = new JumpModifier(1, fromX, toX, fromY, toY, -140, new IEntityModifier.IEntityModifierListener() {
            @Override
            public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
                animatedSprite.stopAnimation(0);
            }

            @Override
            public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
                animatedSprite.animate(50);
                animatedSprite.setY(innitialYPosition);
            }
        });

        animatedSprite.registerEntityModifier(jumpModifier);
    }