Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Andengine输入事件_Java_Android_Import - Fatal编程技术网

Java Andengine输入事件

Java Andengine输入事件,java,android,import,Java,Android,Import,如何将移动事件添加到精灵中? 我真的不知道如何使用输入事件,我在这个网站和其他网站上查看了大量不同的链接,没有一个真正起到帮助作用。我需要有人解释并帮助我解决这个问题 package com.KDevs.test; import java.util.Random; import java.util.Vector; import org.andengine.engine.camera.Camera; import org.andengine.engine.options.EngineOptio

如何将移动事件添加到精灵中? 我真的不知道如何使用输入事件,我在这个网站和其他网站上查看了大量不同的链接,没有一个真正起到帮助作用。我需要有人解释并帮助我解决这个问题

package com.KDevs.test;

import java.util.Random;
import java.util.Vector;

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.IEntityModifier;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.BaseGameActivity;

import android.hardware.SensorManager;
import android.util.Log;
import android.view.MotionEvent;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;

public class Main extends BaseGameActivity   {
Random rand;

Scene scene;
int x  , y;
protected static final int CAMERA_WIDTH = 800;
protected static final int CAMERA_HEIGHT = 480;
BitmapTextureAtlas playerTexture;
ITextureRegion playerTexureRegion;
BitmapTextureAtlas blockTexture;
ITextureRegion blockTexureRegion;
BitmapTextureAtlas ZeusTexture;
ITextureRegion ZeusTexureRegion;
PhysicsWorld physicsWorld;
Sprite sPlayer;
Sprite block;


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

@Override
public void onCreateResources(
        OnCreateResourcesCallback pOnCreateResourcesCallback)
        throws Exception {
    // TODO Auto-generated method stub
    loadGfx();
    // resource
    pOnCreateResourcesCallback.onCreateResourcesFinished();
}

private void loadGfx() {
    // TODO Auto-generated method stub

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    ZeusTexture = new BitmapTextureAtlas(getTextureManager(), 64, 64);
    ZeusTexureRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(ZeusTexture, this, "zeus.png", 0, 0);
    ZeusTexture.load();


    // width and height power of 2^x
    playerTexture = new BitmapTextureAtlas(getTextureManager(), 64, 64);
    playerTexureRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(playerTexture, this, "player.png", 0, 0);
    playerTexture.load();


    blockTexture = new BitmapTextureAtlas(getTextureManager(), 64, 256);
    blockTexureRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(blockTexture, this, "block.png", 0, 0);
    blockTexture.load();
}

@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
        throws Exception {
    // TODO Auto-generated method stub


    this.scene = new Scene();
    this.scene.setBackground(new Background(0, 0, 0));
    physicsWorld = new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_MARS),                   false);
    this.scene.registerUpdateHandler(physicsWorld);
    pOnCreateSceneCallback.onCreateSceneFinished(this.scene);

}

@Override
public void onPopulateScene(Scene pScene,
        OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {

    int counter = 0 , timer = 0;
    int x2 , y2;
    counter++;
    x =CAMERA_WIDTH /2;
    y = 416;
    x2 = 300;
    y2= -40;
    //if(counter % 100 == 0){
        //x++;
        //timer++;
    //}




    rand = new Random();
    int area = rand.nextInt(800);
    //Player
     sPlayer = new Sprite(x, y,
            playerTexureRegion, this.mEngine.getVertexBufferObjectManager());
    scene.registerTouchArea(sPlayer);
    scene.setTouchAreaBindingOnActionDownEnabled(true);
    scene.setTouchAreaBindingOnActionMoveEnabled(true);
    //Block
     block = new Sprite(x2 , y2,
            blockTexureRegion, this.mEngine.getVertexBufferObjectManager());
    //Zeus Sprite
    Sprite zeus = new Sprite(CAMERA_WIDTH/2 , 11,
            ZeusTexureRegion, this.mEngine.getVertexBufferObjectManager());
    //sPlayer.setRotation(45.0f);
    //I knew this shit would be helpful
    block.setRotation(90.0f);
    FixtureDef PLAYERBlock = PhysicsFactory.createFixtureDef(5.0f, 0.0f, 0.0f);
    Body body = PhysicsFactory.createCircleBody(physicsWorld, block, BodyType.DynamicBody, PLAYERBlock);
    this.scene.attachChild(sPlayer);
    this.scene.attachChild(block);
    this.scene.attachChild(zeus);
    physicsWorld.registerPhysicsConnector(new PhysicsConnector(block,      body,true,false));

    pOnPopulateSceneCallback.onPopulateSceneFinished();

}


public boolean onTouchEvent(MotionEvent event) {
       if(event.getAction==MotionEvent.ACTION_DOWN){
        Log.e("Touching", "Touching the Screen");
      }
        else if(event.getAction==MotionEvent.ACTION_UP){
        Log.e("Touching up", "Touching the Screen up");}
        return true;
    }
}

首先,我将从SimpleBaseAgameActivity扩展。 您需要覆盖精灵对象的OnAreTouch方法。这样:

Sprite sprite = new Sprite(x, y, texture, this.getVertexBufferObjectManager()) {

    public boolean onAreaTouched(org.andengine.input.touch.TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) 
    {
        //<DO ANYTHING this.setPosition(x,y) method to move the sprite>
        this.setPosition(this.getX() + 10 , this.getY() + 10);
        return true;
    };
};
scene.setTouchAreaBindingOnActionDownEnabled(true);
scene.registerTouchArea(sprite);

看起来你只发布了部分代码示例?很抱歉我会修复它。你的确切意思是什么,你想移动精灵?是的,我想移动精灵