创建一个随机的正弦图,如景观精灵

时间:2011-11-28 15:53:51

标签: android box2d sprite andengine

让我说我有这个精灵:

Sample sprite

我在运行时创建了一个随机的景观:

Dynamic runtime landscape

然后,我想用精灵来平铺线下方的区域:

enter image description here

这是游戏场所,因此它也应该是一个物理对象(在Box2D中)

Here,他给出了一个如何使其成为物理体的样本。那么,我如何在代码中执行图形部分?

编辑:查看AndEngine示例,文件RepeatingSpriteBackgroundExample.java,这不是我需要的,但我应该坚持这个想法并根据我的需要更改RepeatingSpriteBackground class吗?我认为这不是最好的方法......

提前致谢。

1 个答案:

答案 0 :(得分:6)

我真的不知道Box2D或AndEngine,但我认为这是一个有趣的问题,并制作了一个自定义的SurfaceView,可以绘制一个随机的“地形”,如图片中的那个。 (希望它对你或其他人有用,至少我学到了一些新东西:p)

单色地形: screenshot 平铺的背景 - 地形: screenshot 平铺位图:
resource bitmap

我的代码如下:

public class PathView extends SurfaceView implements SurfaceHolder.Callback{
private class DrawingRunnable implements Runnable{
    private final static int minPointsOnScreen = 3;
    SurfaceHolder surfaceHolder;
    Random rand = new Random();

    private Path path;
    private Paint pathPaint;
    Bitmap background;
    private Paint tilePaint;        

    volatile boolean running = false;

    int width;
    int height;
    int maxHeight;

    protected DrawingRunnable(SurfaceHolder sh){
        surfaceHolder = sh;

        pathPaint = new Paint();
        pathPaint.setColor(0xFF000000);
        pathPaint.setStrokeWidth(4);

        tilePaint = new Paint();
    }

    protected void createPath(){
        path = new Path();
        path.setFillType(Path.FillType.WINDING);

        path.setLastPoint(0, height);
        int lastX = 0, lastY = height - rand.nextInt(maxHeight);
        path.lineTo(lastX,lastY);

        int newX=lastX, newY=lastY;

        do{ 
            lastX = newX; lastY = newY;
            newX += rand.nextInt(width/minPointsOnScreen);
            newY = height - rand.nextInt(maxHeight);
            path.cubicTo(
                    interpolateLinear(lastX, newX, 0.333f),
                    lastY,
                    interpolateLinear(lastX, newX, 0.666f),
                    newY,
                    newX, newY);
        }while(newX <= width);

        path.lineTo(width, height);
    }

    private int interpolateLinear(int start, int end, float part){
        return (int) (start*(1-part) + end*part);
    }

    @Override
    public void run(){
        while(running){
            Canvas c = null;
            try{
                c = surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder) { 
                    doDraw(c); 
                }
            } finally{ if(c!=null) surfaceHolder.unlockCanvasAndPost(c); }
            SystemClock.sleep(40);
        }
    }

    private void doDraw(Canvas c){
        c.drawColor(0xFFFFFFFF);
        //c.drawPath(path, pathPaint); //Use this to draw a single-colour. (First screenshot)
        c.clipPath(path);
        for(int y = 0; y+background.getHeight() < height+background.getHeight(); y+=background.getHeight()){
            for(int x = 0; x+background.getWidth() < width+background.getWidth(); x+=background.getWidth()){
                c.drawBitmap(background, x, y, tilePaint);
            }
        }
    }
}

private ExecutorService exec;
private SurfaceHolder holder;
private DrawingRunnable drawer;

public PathView(Context c){ super(c); init(c); }
public PathView(Context c, AttributeSet as){ super(c, as); init(c); }
public PathView(Context c, AttributeSet as, int defStyle){ super(c, as, defStyle); init(c); }

private void init(Context c){
    exec = Executors.newSingleThreadExecutor();
    holder = getHolder();
    holder.addCallback(this);
}

public void surfaceCreated(SurfaceHolder sh){
    if( drawer == null ){
        drawer = new DrawingRunnable(holder);
        drawer.width = getWidth();
        drawer.height = getHeight();
        drawer.maxHeight = drawer.height/2;
        drawer.createPath();
        drawer.background = BitmapFactory.decodeResource(getResources(), R.drawable.tile);
    }
    drawer.running = true;
    exec.execute(drawer);
}
public void surfaceDestroyed(SurfaceHolder sh){
    drawer.running = false;
}
public void surfaceChanged(SurfaceHolder sh, int format, int width, int height){}
}

如果这对您有任何帮助,您可能需要使用这些参数来获取适合您需求的形状,并且最有可能为点之间的最小距离添加参数等。 优化背景的绘制也是一个好主意,例如从底部绘制到地形的最大高度,以最小化绘制到不可见区域。我也应该可以减少对getHeight()和getWidth()的调用量。

干杯!