0

您好,我有以下使用 Andengine 库的代码。代码有效,基本上它在这种情况下获取从敌人(黑羊)到玩家的路径,然后使用路径修改器移动羊,在路径结束时再次开始该过程 - 所以我如果您愿意,我正在使用线程创建永久运动。我的问题是这是进行这种永久寻路的最有效的清洁方法吗?谢谢

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() ); 
        } 
}
4

1 回答 1

0

考虑使用 AndEngine 的更新处理程序而不是普通的 Java 线程。您的精灵线程将得到更好的处理。http://andengine.wikidot.com/updatehandler

于 2013-06-04T09:48:11.690 回答