问题描述
可以说我有这个精灵:
和我在运行时创建一个随机的风景:
And I created a random landscape during runtime:
然后,我想下面的瓷砖与精灵行的区域:
And then, I want to tile the area below the line with the sprite:
这是本场比赛地,因此,也应该是一个物理对象(在Box2D的)的。
这里,他给出了如何使这是一个物理的身体样本。那么,我该怎么办显卡部分在code?
Here, he given a sample of how to make this a physics body. So, how do I do the graphics part in code?
编辑:寻找在AndEngine例子,文件<一个href="http://$c$c.google.com/p/andengineexamples/source/browse/src/org/anddev/andengine/examples/RepeatingSpriteBackgroundExample.java"相对=nofollow> RepeatingSpriteBackgroundExample.java ,这不正是我需要的,但我要坚持这个想法,并修改<一个href="http://$c$c.google.com/p/andengine/source/browse/src/org/anddev/andengine/entity/scene/background/RepeatingSpriteBackground.java"相对=nofollow> RepeatingSpriteBackground类我的需求?我不认为这是最好的方法,但是...
Looking in the AndEngine examples, file RepeatingSpriteBackgroundExample.java, it's not exactly what I need but should I stick to this idea and change the RepeatingSpriteBackground class for my needs? I don't think that would be the best method however...
在此先感谢。
推荐答案
我真的不知道Box2D的或AndEngine,但我认为这是一个有趣的问题,并提出了自定义的SurfaceView,可以得出一个随机地形像一个在你的照片。 (希望这将是使用你或其他人,至少我学到了一些新的东西:P)的
单色的地形:瓷砖背景地形:该铺位图:
I don't really know about Box2D or AndEngine, but I thought this was an interesting problem and made a custom SurfaceView that can draw a random "terrain" like the one in your picture. (Hopefully it will be of use to you or someone else, at least I learnt some new stuff :p)
Single colour terrain:Tiled background-terrain:The tiled bitmap:
我的code是如下:
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()。
If this is of any help to you you'll probably have to play around with the parameters to get shapes that suits your needs and most likely add a parameter for minimum distance between points etc.It is also a good idea to optimize the drawing of the background a bit, like drawing from the bottom up to the maximum height of the terrain, to minimize drawing to invisible areas. I should also be possible to reduce the amount of calls to getHeight() and getWidth().
干杯!
这篇关于创建一个随机,正弦曲线一样的风景精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!