簡體   English   中英

從一點到另一點移動精靈

[英]Moving sprite from point to point

我想在兩點之間移動一個精靈。

第一個點是x=0y=0 ,第二個點是用戶觸摸屏幕的點。

要移動,我想用一個方程來經過兩個點,即y=ax+b

我在方法move()嘗試了這個,但精靈沒有移動。

請幫忙。

班級顯示:

package com.example.name;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Display extends SurfaceView {
private Bitmap bmp;
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private Sprite sprite;
private long lastClick;
private float x = 0.0F;
private float y = 0.0F;

public Display(Context context) {
    super(context);

    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.gonia);
    sprite = new Sprite(this, bmp, x, y, x, y);

    gameLoopThread = new GameLoopThread(this);
    holder = getHolder();
    holder.addCallback(new SurfaceHolder.Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            boolean retry = true;
            gameLoopThread.setRunning(false);
            while (retry) {
                try {
                    gameLoopThread.join();
                    retry = false;
                } catch (InterruptedException e) {
                }
            }
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            gameLoopThread.setRunning(true);
            gameLoopThread.start();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {
        }
    });

}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.BLACK);
    sprite.onDraw(canvas);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (System.currentTimeMillis() - lastClick > 500) {
        lastClick = System.currentTimeMillis();
        x = (float) sprite.ostatniaWartoscX();
        y = (float) sprite.ostatniaWartoscY();
        float gotox = (float) event.getX();
        float gotoy = (float) event.getY();
        synchronized (getHolder()) {
            sprite = new Sprite(this, bmp, x, y, gotox, gotoy);

        }
    }
    return true;
}

}

類精靈:

package com.example.name;

import android.graphics.Bitmap;
import android.graphics.Canvas;


public class Sprite {


private float a;
private float b;

private float x;
private float y;
private float gotox;
private float gotoy;
private int executeMove = 0;
private Display display;
private Bitmap bmp;


public Sprite(Display display, Bitmap bmp, float x, float y, float gotox,
        float gotoy) {
    this.display = display;
    this.bmp = bmp;
    this.x = x;
    this.y = y;
    this.gotox = gotox;
    this.gotoy = gotoy;
}

void update() {

    if (x < gotox) {x++;executeMove = 1;}
    if (x > gotox) {x--;executeMove = 1;}

    if (executeMove == 1) {move();}
    executeMove = 0;


}

void move() {
    float x1 = x;
    float y1 = y;
    float x2 = gotox;
    float y2 = gotoy;

    a = (y2-y1)/(x2-x1);
    b = y1 - x1*a;
    y = x1 * a + b;


}


public float ostatniaWartoscX() {
    return x;
}

public float ostatniaWartoscY() {
    return y;
}

public void onDraw(Canvas canvas) {
    update();
    canvas.drawBitmap(bmp, x, y, null);
}

}

謝謝!

你需要Bresenham的線算法來移動你的玩家。 您甚至可以縮短它,只需要計算下一個動作(而不是整行)。 這是一種簡單/簡單,低卡路里的算法。

您必須根據自己的需要進行調整。

public static ArrayList<Point> getLine(Point start, Point target) {
    ArrayList<Point> ret = new ArrayList<Point>();

    int x0 =  start.x;
    int y0 =  start.y;

    int x1 = target.x;
    int y1 = target.y;

    int sx = 0;
    int sy = 0;

    int dx =  Math.abs(x1-x0);
    sx = x0<x1 ? 1 : -1;
    int dy = -1*Math.abs(y1-y0);
    sy = y0<y1 ? 1 : -1; 
    int err = dx+dy, e2; /* error value e_xy */

    for(;;){  /* loop */
        ret.add( new Point(x0,y0) );
        if (x0==x1 && y0==y1) break;
        e2 = 2*err;
        if (e2 >= dy) { err += dy; x0 += sx; } /* e_xy+e_x > 0 */
        if (e2 <= dx) { err += dx; y0 += sy; } /* e_xy+e_y < 0 */
    }

    return ret;
}

這是我用來在兩點之間移動對象的代碼示例。

Math.atan2計算θ角度(-pi到+ pi),這是物體需要行進的軌跡。 Delta是此更新與上次更新之間的時間,速度是對象的所需速度。 這些都需要相乘,然后添加到當前位置以獲得新位置。

@Override
protected void update(float delta) {
        double theta = Math.atan2(targetPos.y - pos.y, targetPos.x - pos.x);

        double valX = (delta * velocity) * Math.cos(theta);
        double valY = (delta * velocity) * Math.sin(theta);

        pos.x += valX;
        pos.y += valY;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM