0
如何將移動事件添加到精靈? 我真的不知道如何使用輸入事件我已經檢查了這個網站和其他人的噸噸不同的鏈接,沒有人真的幫助過。我需要有人解釋和幫助我這個請。Andengine輸入事件
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;
}
}
看起來你只貼出了部分代碼示例? – Durandal
即時對不起,我會修復它 – user3330550
你是什麼意思,你想要移動一個精靈? – kabuto178