如何使用AndEngine(安卓)移动精灵对象 [英] how to move sprite object using AndEngine (Android)

查看:99
本文介绍了如何使用AndEngine(安卓)移动精灵对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用andengine开发一款游戏的机器人。我把一个对象的精灵一样

  this.mTexture =新的纹理(32,32,TextureOptions.BILINEAR_ preMULTIPLYALPHA);
            this.mFaceTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture,这样,GFX / bike.png,0,0);
----我的地方,如
雪碧bikeSprite =新的Sprite(20,50,this.mFaceTextureRegion);
 

我想用这个精灵在j2me精灵

bikeSprite.move( - ); 如何做到这一点的机器人。我不想使用setPosition。

解决方案

 公共类EntityModifierExample扩展BaseExample {
    // ================================================ ===========
    //常量
    // ================================================ ===========

    私有静态最终诠释CAMERA_WIDTH = 720;
    私有静态最终诠释CAMERA_HEIGHT = 480;

    // ================================================ ===========
    //领域
    // ================================================ ===========

    私人相机mCamera;
    私人纹理mTexture;
    私人TiledTextureRegion mF​​aceTextureRegion;

    // ================================================ ===========
    //构造函数
    // ================================================ ===========

    // ================================================ ===========
    //吸气和放大器;二传手
    // ================================================ ===========

    // ================================================ ===========
    //方法/从父类/接口
    // ================================================ ===========

    @覆盖
    公共引擎onLoadEngine(){
        this.mCamera =新照相机(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
        返回新引擎(新EngineOptions(真,ScreenOrientation.LANDSCAPE,新RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),this.mCamera));
    }

    @覆盖
    公共无效onLoadResources(){
        this.mTexture =新的纹理(64,32,TextureOptions.BILINEAR_ preMULTIPLYALPHA);
        this.mFaceTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTexture,这样,GFX / face_box_tiled.png,0,0,2,1);

        。this.mEngine.getTextureManager()loadTexture(this.mTexture);
    }

    @覆盖
    公共场景onLoadScene(){
        this.mEngine.registerUpdateHandler(新FPSLogger());

        最后一幕一幕=新场景();
        scene.setBackground(新ColorBackground(0.09804f,0.6274f,0.8784f));

        最终诠释的centerX =(CAMERA_WIDTH  -  this.mFaceTextureRegion.getWidth())/ 2;
        最终诠释centerY =(CAMERA_HEIGHT  -  this.mFaceTextureRegion.getHeight())/ 2;

        最后的矩形RECT =新的Rectangle(的centerX + 100,centerY,32,32);
        rect.setColor(1,0,0);

        最后AnimatedSprite面=新AnimatedSprite(的centerX  -  100,centerY,this.mFaceTextureRegion);
        face.animate(100);
        face.setBlendFunction(GL10.GL_SRC_ALPHA,GL10.GL_ONE_MINUS_SRC_ALPHA);

        最后LoopEntityModifier entityModifier =
            新LoopEntityModifier(
                    新IEntityModifierListener(){
                        @覆盖
                        公共无效onModifierStarted(最终IModifier< IEntity> pModifier,最终IEntity pItem){
                            EntityModifierExample.this.runOnUiThread(新的Runnable(){
                                @覆盖
                                公共无效的run(){
                                    Toast.makeText(EntityModifierExample.this,顺序启动。,Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                        @覆盖
                        公共无效onModifierFinished(最终IModifier< IEntity> pEntityModifier,最终IEntity pEntity){
                            EntityModifierExample.this.runOnUiThread(新的Runnable(){
                                @覆盖
                                公共无效的run(){
                                    Toast.makeText(EntityModifierExample.this,顺序完成,Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                    },
                    1,
                    新ILoopEntityModifierListener(){
                        @覆盖
                        公共无效onLoopStarted(最终LoopModifier< IEntity> pLoopModifier,最终诠释pLoop,最终诠释pLoopCount){
                            EntityModifierExample.this.runOnUiThread(新的Runnable(){
                                @覆盖
                                公共无效的run(){
                                    Toast.makeText(EntityModifierExample.this,循环:'+(pLoop + 1)+'+ pLoopCount +。开始,Toast.LENGTH_SHORT'的').show();
                                }
                            });
                        }

                        @覆盖
                        公共无效onLoopF​​inished(最终LoopModifier< IEntity> pLoopModifier,最终诠释pLoop,最终诠释pLoopCount){
                            EntityModifierExample.this.runOnUiThread(新的Runnable(){
                                @覆盖
                                公共无效的run(){
                                    Toast.makeText(EntityModifierExample.this,循环:'+(pLoop + 1)+'的'+ pLoopCount +'结束。,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    },
                    新SequenceEntityModifier(
                            新RotationModifier(1,0,90),
                            新AlphaModifier(2,1,0),
                            新AlphaModifier(1,0,1),
                            新ScaleModifier(2,1,0.5F),
                            新DelayModifier(0.5F),
                            新ParallelEntityModifier(
                                    新ScaleModifier(3,0.5F,5),
                                    新RotationByModifier(3,90)
                            ),
                            新ParallelEntityModifier(
                                    新ScaleModifier(3,5,1),
                                    新RotationModifier(3,180,0)
                            )
                    )
            );

        face.registerEntityModifier(entityModifier);
        rect.registerEntityModifier(entityModifier.clone());

        scene.attachChild(面);
        scene.attachChild(RECT);

        返回现场;
    }

    @覆盖
    公共无效的onLoadComplete(){

    }

    // ================================================ ===========
    //方法
    // ================================================ ===========

    // ================================================ ===========
    //内部类和匿名类
    // ================================================ ===========
}
 

这code从 samples项目Andengine 。 <一href="https://github.com/nicolasgramlich/AndEngineExamples/blob/GLES2/src/org/andengine/examples/EntityModifierExample.java"相对=nofollow>参考原来的code 。

I am using andengine to develop a game in android. I placed an object in sprite like

 this.mTexture = new Texture(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
            this.mFaceTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/bike.png", 0, 0);
---- and i place like 
Sprite bikeSprite= new Sprite(20, 50, this.mFaceTextureRegion);

I want to use this sprite in j2me sprite

bikeSprite.move(--); How do I do it in android. I dont want to use setPosition .

解决方案

public class EntityModifierExample extends BaseExample {
    // ===========================================================
    // Constants
    // ===========================================================

    private static final int CAMERA_WIDTH = 720;
    private static final int CAMERA_HEIGHT = 480;

    // ===========================================================
    // Fields
    // ===========================================================

    private Camera mCamera;
    private Texture mTexture;
    private TiledTextureRegion mFaceTextureRegion;

    // ===========================================================
    // Constructors
    // ===========================================================

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

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

    @Override
    public void onLoadResources() {
        this.mTexture = new Texture(64, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        this.mFaceTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTexture, this, "gfx/face_box_tiled.png", 0, 0, 2, 1);

        this.mEngine.getTextureManager().loadTexture(this.mTexture);
    }

    @Override
    public Scene onLoadScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger());

        final Scene scene = new Scene();
        scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));

        final int centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
        final int centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;

        final Rectangle rect = new Rectangle(centerX + 100, centerY, 32, 32);
        rect.setColor(1, 0, 0);

        final AnimatedSprite face = new AnimatedSprite(centerX - 100, centerY, this.mFaceTextureRegion);
        face.animate(100);
        face.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

        final LoopEntityModifier entityModifier =
            new LoopEntityModifier(
                    new IEntityModifierListener() {
                        @Override
                        public void onModifierStarted(final IModifier<IEntity> pModifier, final IEntity pItem) {
                            EntityModifierExample.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EntityModifierExample.this, "Sequence started.", Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                        @Override
                        public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
                            EntityModifierExample.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EntityModifierExample.this, "Sequence finished.", Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                    },
                    1,
                    new ILoopEntityModifierListener() {
                        @Override
                        public void onLoopStarted(final LoopModifier<IEntity> pLoopModifier, final int pLoop, final int pLoopCount) {
                            EntityModifierExample.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EntityModifierExample.this, "Loop: '" + (pLoop + 1) + "' of '" + pLoopCount + "' started.", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        @Override
                        public void onLoopFinished(final LoopModifier<IEntity> pLoopModifier, final int pLoop, final int pLoopCount) {
                            EntityModifierExample.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EntityModifierExample.this, "Loop: '" + (pLoop + 1) + "' of '" + pLoopCount + "' finished.", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    },
                    new SequenceEntityModifier(
                            new RotationModifier(1, 0, 90),
                            new AlphaModifier(2, 1, 0),
                            new AlphaModifier(1, 0, 1),
                            new ScaleModifier(2, 1, 0.5f),
                            new DelayModifier(0.5f),
                            new ParallelEntityModifier(
                                    new ScaleModifier(3, 0.5f, 5),
                                    new RotationByModifier(3, 90)
                            ),
                            new ParallelEntityModifier(
                                    new ScaleModifier(3, 5, 1),
                                    new RotationModifier(3, 180, 0)
                            )
                    )
            );

        face.registerEntityModifier(entityModifier);
        rect.registerEntityModifier(entityModifier.clone());

        scene.attachChild(face);
        scene.attachChild(rect);

        return scene;
    }

    @Override
    public void onLoadComplete() {

    }

    // ===========================================================
    // Methods
    // ===========================================================

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}

This code is extracted from the samples project Andengine. Reference to the original code.

这篇关于如何使用AndEngine(安卓)移动精灵对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆