按下TouchArea时如何做某事

时间:2014-01-20 16:25:25

标签: android andengine sprite

我是andengine的新手。 作为一项基本练习,我尝试制作一个游戏,当你按下一个按钮时,精灵向左移动,当我按下另一个按钮时,精灵向右移动。精灵应向左或向右移动,而任何按钮都是但是,使用我当前的代码,当我按下任何按钮时,精灵向左或向右移动10个像素,然后无论我按住按钮多久都停止。 所以这是代码。

package com.example.and_tryout;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.adt.io.in.IInputStreamOpener;
import org.andengine.util.debug.Debug;

import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends SimpleBaseGameActivity {
    private static int CAMERA_WIDTH = 800;
    private static int CAMERA_HEIGHT = 480;
    private ITextureRegion mtexture;
    private ITextureRegion mleft,mright;

    @Override
    public EngineOptions onCreateEngineOptions() {
        // TODO Auto-generated method stub
        Camera camera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
        return new EngineOptions(true,ScreenOrientation.LANDSCAPE_FIXED,new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),camera);
}

    @Override
    protected void onCreateResources() {
    // TODO Auto-generated method stub
        try{
            ITexture texture = new BitmapTexture (this.getTextureManager(), new IInputStreamOpener(){

                @Override
                public InputStream open() throws IOException {
                    // TODO Auto-generated method stub
                    return getAssets().open("png/ring2.png");
               }

            });
            ITexture leftt = new BitmapTexture(this.getTextureManager(),new IInputStreamOpener(){

                @Override
                public InputStream open() throws IOException {
                   // TODO Auto-generated method stub
                    return getAssets().open("png/left.png");
                }
            });
            ITexture rightt = new BitmapTexture(this.getTextureManager(),new IInputStreamOpener(){

                @Override
                public InputStream open() throws IOException {
                    // TODO Auto-generated method stub
                    return getAssets().open("png/right.png");
                }
            });
            texture.load();
            leftt.load();
            rightt.load();
            this.mtexture = TextureRegionFactory.extractFromTexture(texture);
            this.mleft = TextureRegionFactory.extractFromTexture(leftt);
            this.mright = TextureRegionFactory.extractFromTexture(rightt);

            }catch(IOException e){
                Debug.e (e);
            }

    }

    @Override
    protected Scene onCreateScene() {
        Scene scene = new Scene();
        scene.getBackground().setColor(0.09804f, 0.6274f, 0.8784f);
        final Sprite spr = new Sprite(0,0,mtexture,getVertexBufferObjectManager());
        Sprite left = new Sprite(0,420,60,60,mleft,getVertexBufferObjectManager()){
            @Override
            public boolean onAreaTouched(TouchEvent event,float X,float Y){
                spr.registerEntityModifier(new MoveXModifier(0.5f,spr.getX(),spr.getX()-10));
                return true;
            }

        };
        Sprite right = new Sprite(740,420,60,60,mright,getVertexBufferObjectManager()){
            @Override
            public boolean onAreaTouched(TouchEvent event,float X,float Y){
                spr.registerEntityModifier(new MoveXModifier(0.5f,spr.getX(),spr.getX()+10));
                return true;
            }
        };
        scene.attachChild(spr);
        scene.attachChild(left);
        scene.attachChild(right);
        scene.registerTouchArea(left);
        scene.registerTouchArea(right);
        return scene;
    }

}

我无法找到任何解决方案。我已经搜索了很多关于Google的内容,它给了我 nothing

3 个答案:

答案 0 :(得分:1)

地点条件触及,触摸和触摸在精灵的onTouchArea中移动,如下所示:

         public boolean onAreaTouched(TouchEvent event,float X,float Y){
         if (event.getAction() == MotionEvent.ACTION_DOWN) {
                     spr.registerEntityModifier(new MoveXModifier(0.5f,spr.getX(),spr.getX()-10));
        }
      if (event.getAction() == MotionEvent.ACTION_UP) {
}
    }

答案 1 :(得分:0)

    Try this

    Sprite left = new Sprite(0,420,60,60,mleft,getVertexBufferObjectManager())
    {
                @Override
                public boolean onAreaTouched(TouchEvent event,float X,float Y)
                {

                    if (event.getAction() == MotionEvent.ACTION_DOWN) 
                    {
                         spr.clearEntityModifiers()
                         spr.registerEntityModifier(new MoveXModifier(0.5f,spr.getX(),spr.getX()-10));
                    }                
                    return true;
                }
    };
    Sprite right = new Sprite(740,420,60,60,mright,getVertexBufferObjectManager())
    {
                @Override
                public boolean onAreaTouched(TouchEvent event,float X,float Y)
                {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) 
                    {

Blockquote

                         spr.clearEntityModifiers()
                         spr.registerEntityModifier(new MoveXModifier(0.5f,spr.getX(),spr.getX()+10));
                    }    
                    return true;
                }
    };

答案 2 :(得分:0)

最好使用PhysicsHandler以速度移动精灵。修改器在完成之前无法访问,这就是为什么即使你按下按钮你的精灵也不会再进一步​​。

否则,如果你想要简单的连续运动你可以简单地增加精灵的位置。

@Override
                public boolean onAreaTouched(TouchEvent event,float X,float Y)
                {

                    if (event.getAction() == MotionEvent.ACTION_DOWN) 
                    {
                         spr.setPosition(spr.getX()+1, spr.getY());
                    }                
                    return true;
                } 

使用spr.getX() - 1

对左运动执行相同操作