简体   繁体   English

从一点到另一点移动精灵

[英]Moving sprite from point to point

I want to move a sprite between two points. 我想在两点之间移动一个精灵。

first point is x=0 and y=0 and the second point is a point where the user touched the screen. 第一个点是x=0y=0 ,第二个点是用户触摸屏幕的点。

To move, I want to use an equation to go through the two points which is y=ax+b . 要移动,我想用一个方程来经过两个点,即y=ax+b

I have attempted this in the method move() but the sprite does not move. 我在方法move()尝试了这个,但精灵没有移动。

Please help. 请帮忙。

Class Display: 班级显示:

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;
}

}

Class Sprite: 类精灵:

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

}

Thank You! 谢谢!

You need Bresenham's line algorithm to move your player. 你需要Bresenham的线算法来移动你的玩家。 You can even cut it short that you only need to calculate the next movement (not the whole line). 您甚至可以缩短它,只需要计算下一个动作(而不是整行)。 It's a simple/easy and low-calorie-algorithm. 这是一种简单/简单,低卡路里的算法。

You must adjust it to your needs. 您必须根据自己的需要进行调整。

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;
}

Here is an example of code I use to move an object between two points. 这是我用来在两点之间移动对象的代码示例。

Math.atan2 calculates the theta angle (-pi to +pi) which is the trajectory the object needs to travel. Math.atan2计算θ角度(-pi到+ pi),这是物体需要行进的轨迹。 Delta is the time between this this update and the last update and velocity is the desired speed of the object. Delta是此更新与上次更新之间的时间,速度是对象的所需速度。 These all need to be multiplied together and then added to the current position to get the new position. 这些都需要相乘,然后添加到当前位置以获得新位置。

@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