@Rama 코드를 사용해 보았지만 스프라이트를 스 와이프하면 오류가 발생합니다.Andengine을 사용하여 스 와이프 방향으로 스프라이트를 던지시겠습니까?
실제로 내가 원했던 것은 사용자가 스프라이트를 스 와이프하여 발사체로 그 방향으로 던져야 할 때입니다.
package com.hrh.chini;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.input.sensor.acceleration.AccelerationData;
import org.andengine.input.sensor.acceleration.IAccelerationListener;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import android.graphics.Point;
import android.hardware.SensorManager;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
public class MovingBallExample extends SimpleBaseGameActivity implements IAccelerationListener{
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
private static final float DEMO_VELOCITY = 100.0f;
private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(15, 0.5f, 1f);
// ===========================================================
// Fields
// ===========================================================
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mFaceTextureRegion;
Body body;
Sprite face;
PhysicsWorld mPhysicsWorld;
Point touchDownLocation, touchMoveLocation;
int SWIPE_THRESHOLD = 10;
float mGravityX;
float mGravityY;
@Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, MovingBallExample.CAMERA_WIDTH, MovingBallExample.CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(MovingBallExample.CAMERA_WIDTH, MovingBallExample.CAMERA_HEIGHT), camera);
}
@Override
public void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 32, TextureOptions.BILINEAR);
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this,"ball2.png", 0, 0);
this.mBitmapTextureAtlas.load();
}
@Override
public void onResumeGame() {
super.onResumeGame();
this.enableAccelerationSensor(this);
}
@Override
public void onPauseGame() {
super.onPauseGame();
this.disableAccelerationSensor();
}
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
Scene scene = new Scene();
this.mPhysicsWorld = new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH), false);
touchDownLocation = new Point();
touchMoveLocation = new Point();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
final float centerX = (MovingBallExample.CAMERA_WIDTH - this.mFaceTextureRegion.getWidth())/2;
final float centerY = (MovingBallExample.CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight())/2;
// final Ball ball = new Ball(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
face = new Sprite(450,80,this.mFaceTextureRegion, this.getVertexBufferObjectManager()){
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
touchDownLocation.set((int)pTouchAreaLocalX, (int)pTouchAreaLocalY);
}
if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_MOVE){
int direction = 0;
touchMoveLocation.set((int)pTouchAreaLocalX, (int)pTouchAreaLocalY);
int diffX = touchDownLocation.x - touchMoveLocation.x;
if(diffX< -SWIPE_THRESHOLD){
direction = -1;
}else if(diffX> SWIPE_THRESHOLD){
direction = 1;
}
jumpFace(this, direction);
}
return true;
}
private void jumpFace(Sprite sprite, int direction) {
// Based on direction you can apply velovity
final Body faceBody = (Body)face.getUserData();
Vector2 velocity2 = null;
if(direction == 1){
final Vector2 velocity = Vector2Pool.obtain(MovingBallExample.this.mGravityX * 5, MovingBallExample.this.mGravityY * -5);
velocity2 = velocity;
}else if(direction == -1){
final Vector2 velocity = Vector2Pool.obtain(MovingBallExample.this.mGravityX * -5, MovingBallExample.this.mGravityY * -5);
velocity2 = velocity;
}
faceBody.setLinearVelocity(velocity2);
Vector2Pool.recycle(velocity2);
}
};
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
face.setUserData(body);
scene.attachChild(face);
scene.registerTouchArea(face);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
scene.registerUpdateHandler(this.mPhysicsWorld);
return scene;
}
@Override
public void onAccelerationAccuracyChanged(AccelerationData pAccelerationData) {
// TODO Auto-generated method stub
}
@Override
public void onAccelerationChanged(AccelerationData pAccelerationData) {
// TODO Auto-generated method stub
this.mGravityX = pAccelerationData.getX();
this.mGravityY = pAccelerationData.getY();
final Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY());
this.mPhysicsWorld.setGravity(gravity);
Vector2Pool.recycle(gravity);
}
}
했던 시도 u는 여기 링크 중 하나를 시도 : 1. http://stackoverflow.com/questions/13433916/andengine-how-to-move-sprite -with-touchscreen 2. http://stackoverflow.com/questions/19202068/andengine-move-move-sprite-to-onscenetouchevent – KOTIOS
이 링크는 영역 터치에 스프라이트를 놓고 있으며, 내가하려고하는 것은 던지기입니다. 스윕에 대한 스프라이트, 나는 스프라이트에 물리학을 추가했습니다. – Uday
당신은 내 post.Hoping을 통해 갈 수 있습니다 당신이 도움이 될 수도 있습니다 – Rama