andengineandroid动画线程
您好,我有以下使用Andengine库的代码。代码是有效的,基本上它得到了从敌人(一只害群之马)到玩家的路径,然后使用路径修饰符移动羊,在路径的末尾,过程再次开始——因此,如果你愿意,我将使用线程创建永久移动。我的问题是,这是进行这种永久性路径查找的最有效的干净方法吗??谢谢andengineandroid动画线程,android,multithreading,animation,andengine,Android,Multithreading,Animation,Andengine,您好,我有以下使用Andengine库的代码。代码是有效的,基本上它得到了从敌人(一只害群之马)到玩家的路径,然后使用路径修饰符移动羊,在路径的末尾,过程再次开始——因此,如果你愿意,我将使用线程创建永久移动。我的问题是,这是进行这种永久性路径查找的最有效的干净方法吗??谢谢 public void startThread(){ stop1=true; t1 = new Thread(mMoveBlackSheep); t1.start(); } public void stopThrea
public void startThread(){ stop1=true; t1 = new Thread(mMoveBlackSheep); t1.start(); }
public void stopThread(){ if(t1 != null){ stop1=false; t1.interrupt(); }}
public void startThread1(){ stop2=true; t2 = new Thread(mMoveBlackSheep1); t2.start(); }
public void stopThread1(){ if(t2 != null){ stop2=false; t2.interrupt(); }}
private synchronized Path get_coords( int sheep ) {
Path retxy=null;
try {
final float[] playerFootCordinates = player.convertLocalToSceneCoordinates( 12,31 );
final TMXTile tmxTile = tmxLayer.getTMXTileAt(playerFootCordinates[Constants.VERTEX_INDEX_X], playerFootCordinates[Constants.VERTEX_INDEX_Y]);
int pColPlayer = 0;
int pRowPlayer = 0;
if(tmxTile != null) {
pColPlayer = tmxTile.getTileColumn();
pRowPlayer = tmxTile.getTileRow();
}
AnimatedSprite blacksheep = black_sheep.get(sheep);
int pColBlackSheep = 0;
int pRowBlackSheep = 0;
final float[] MonsterFootCordinates = blacksheep.convertLocalToSceneCoordinates( 12,31 );
final TMXTile tmxTile1 = tmxLayer.getTMXTileAt(MonsterFootCordinates[Constants.VERTEX_INDEX_X], MonsterFootCordinates [Constants.VERTEX_INDEX_Y]);
if(tmxTile1 != null) {
pColBlackSheep = tmxTile1.getTileColumn();
pRowBlackSheep = tmxTile1.getTileRow();
}
int xx1 = (int)(player.getX() / TileWidth );
int yy1 = (int)(player.getY() / TileHeight );
int selectedx = pRowBlackSheep;
int selectedy = pColBlackSheep;
pathfinding.Path path=null;
Random r1 = new Random();
int d1 = r1.nextInt(20);
path = finder_sheep.findPath(new UnitMover(1), selectedx, selectedy, pRowPlayer , pColPlayer );
// still null restart thread.
if (path==null)
{
try {
int randomAmountOfTime=500;
if ( !Thread.interrupted() ) {
Thread.sleep(randomAmountOfTime);
}
} catch (InterruptedException e) {
return null;
}
catch (Exception e) { e.printStackTrace(); }
}
if (path != null) {
int p1=path.getLength();
float[] x1 = new float[p1];
float[] y1 = new float[p1];
int[] direction = new int[p1];
for (int i=0; i<p1; i++)
{
int vecX = path.getX(i) * TileWidth;
int vecY = path.getY(i) * TileHeight;
x1[i] = vecX;
y1[i] = vecY;
if ( i > 0 ) {
int vecXPrev = path.getX(i-1) * TileWidth;
int vecYPrev = path.getY(i-1) * TileHeight;
} else { direction[0]=0; }
}
retxy = new Path(y1 , x1 );
Log.d("END CREATE XY ARRAY", "END CREATE XY ARRAY");
} else { retxy=null; }
}
catch (Exception e) { }
return retxy;
}
Runnable mMoveBlackSheep = new Runnable() {public void run() { if (t1!=null ) { while(!t1.isInterrupted()) { run1(); }}}};
Runnable mMoveBlackSheep1 = new Runnable() {public void run() { if (t2!=null) { while(!t2.isInterrupted()) { run2(); }}}};
private void run1() {
try {
Path co1 = get_coords(0);
if (co1 != null) {
followPath(co1 , 0 ,0 );
}
} catch (Exception e) {
//Log.d("run1", e.getMessage());
}
}
private void run2() {
try {
// get the Column / Row for the player.
Path co1 = get_coords(1);
if (co1 != null) {
followPath(co1 , 1 ,1 );
}
} catch (Exception e) {
//Log.d("run1", e.getMessage());
}
}
private synchronized void followPath( final Path p1 , final int current_sheep , final int threadnum )
{
try
{
float speedOfPlayer= 4;
black_sheep.get(current_sheep).registerEntityModifier(new PathModifier(speedOfPlayer, p1, null, new IPathModifierListener() {
public void onPathStarted(final PathModifier pPathModifier, final IEntity pEntity) {
}
//@Override
public void onPathWaypointStarted(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
final long[] frameDurations = new long[3];
Arrays.fill(frameDurations, 500);
black_sheep.get(current_sheep).animate(frameDurations, 0, 2, true);
}
//@Override
public void onPathWaypointFinished(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
}
//@Override
public void onPathFinished(final PathModifier pPathModifier, final IEntity pEntity) {
if ( current_sheep==0) {
stopThread();
startThread(); }
if ( current_sheep==1) {
stopThread1();
startThread1(); }
}
}));
}
catch (Exception e) {
//Log.d("ERROR~~~", e.getMessage() );
}
}
public void startThread(){stop1=true;t1=new Thread(mMoveBlackSheep);t1.start();}
public void stopThread(){if(t1!=null){stop1=false;t1.interrupt();}}
public void startThread1(){stop2=true;t2=new Thread(mMoveBlackSheep1);t2.start();}
public void stopThread1(){if(t2!=null){stop2=false;t2.interrupt();}}
专用同步路径获取协调(int-sheep){
路径retxy=null;
试一试{
最终浮动[]玩家坐标=玩家。将本地坐标转换为中心坐标(12,31);
final TMXTile TMXTile=tmxLayer.getTMXTileAt(playerFootCoordinates[Constants.VERTEX_INDEX_X],playerFootCoordinates[Constants.VERTEX_INDEX_Y]);
int pColPlayer=0;
int pRowPlayer=0;
如果(tmxTile!=null){
pColPlayer=tmxTile.getTileColumn();
pRowPlayer=tmxTile.getTileRow();
}
AnimatedSprite blacksheep=黑_sheep.get(sheep);
int pColBlackSheep=0;
int pRowBlackSheep=0;
最终浮点数[]footcordinates=黑羊。转换本地坐标到中心坐标(12,31);
最终的TMXTile tmxTile1=tmxLayer.getTMXTileAt(MonsterFootCordinates[Constants.VERTEX\u INDEX\u X],MonsterFootCordinates[Constants.VERTEX\u INDEX\u Y]);
if(tmxTile1!=null){
pColBlackSheep=tmxTile1.getTileColumn();
pRowBlackSheep=tmxTile1.getTileRow();
}
int xx1=(int)(player.getX()/TileWidth);
int yy1=(int)(player.getY()/TileHeight);
int selectedx=pRowBlackSheep;
int selectedy=pColBlackSheep;
寻路。路径路径=空;
Random r1=新的Random();
int d1=r1.nextInt(20);
path=finder\u sheep.findPath(新单元移动器(1)、selectedx、selectedy、pRowPlayer、pColPlayer);
//重新启动线程仍然为空。
if(路径==null)
{
试一试{
int-randomountoftime=500;
如果(!Thread.interrupted()){
睡眠时间(随机数);
}
}捕捉(中断异常e){
返回null;
}
catch(异常e){e.printStackTrace();}
}
if(路径!=null){
int p1=path.getLength();
float[]x1=新的float[p1];
浮动[]y1=新浮动[p1];
int[]方向=新int[p1];
对于(int i=0;i 0){
int-vecXPrev=path.getX(i-1)*TileWidth;
int-vecYPrev=path.getY(i-1)*TileHeight;
}else{direction[0]=0;}
}
retxy=新路径(y1,x1);
Log.d(“结束创建XY数组”,“结束创建XY数组”);
}else{retxy=null;}
}
捕获(例外e){}
返回retxy;
}
Runnable mMoveBlackSheep=new Runnable(){public void run(){if(t1!=null){while(!t1.isInterrupted()){run1();}}}};
Runnable mMoveBlackSheep1=new Runnable(){public void run(){if(t2!=null){while(!t2.isInterrupted()){run2();}}};
私有void run1(){
试一试{
路径co1=获取坐标(0);
如果(co1!=null){
跟随路径(co1,0,0);
}
}捕获(例外e){
//Log.d(“run1”,e.getMessage());
}
}
私有void run2(){
试一试{
//获取播放机的列/行。
路径co1=获取坐标(1);
如果(co1!=null){
跟随路径(co1,1,1);
}
}捕获(例外e){
//Log.d(“run1”,e.getMessage());
}
}
专用同步的void followPath(最终路径p1、最终int current_sheep、最终int threadnum)
{
尝试
{
浮点数=4;
black_sheep.get(当前_sheep.registerEntityModifier)(新路径修饰符(speedOfPlayer,p1,null,新IPathModifierListener)(){
public void onPathStarted(最终路径修改器pPathModifier,最终路径修改器){
}
//@凌驾
公共void onPathWaypointStarted(最终路径修饰符pPathModifier、最终方向奔腾、最终int pWaypointIndex){
最终长[]帧持续时间=新长[3];
数组。填充(帧持续时间,500);
黑羊。获取(当前羊)。设置动画(帧持续时间,0,2,真);
}
//@凌驾
公共void onPathWaypointFinished(最终路径修饰符pPathModifier、最终方向性奔腾、最终整数pWaypointIndex){
}
//@凌驾
public void onPathFinished(最终路径修饰符pPathModifier,最终路径修饰符pEntity){
如果(当前_=0){
止动螺纹();
startThread();}
如果(当前_=1){
停止线程1();
startThread1();}
}
}));
}
捕获(例外e){
//Log.d(“ERROR~~”,e.getMessage());
}
}
考虑使用AndEngine的更新处理程序,而不是普通的Java线程。您的精灵线程将得到更好的处理