问题描述
我正在尝试使我的Projectile(drawCircle)朝着目标端点(drawLine aimX,aimY)向移动。我有一个方法movetowards,用于处理弹丸向端点的移动。您可以使用getY()和getX()方法通过滑动将端点拖出。我尝试使用斜率来获得上升并运行,使弹丸向端点移动,但是我不能将斜率用作小数。我也尝试使用有条件但无济于事的方法使射弹走向终点。到现在为止,无论我的端点是什么,我的弹丸都直接沿直线移动到某个位置。我在代码中使用注释,以专注于所有重要部分。
//MainActivity.java
package com.example.homework2_hakeemthomas;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Debug;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
public class MainActivity extends AppCompatActivity {
MyCanvas canvas;
public static float slope = 0.0f;
public static float xslope = 0.0f;
public static float yslope = 0.0f;
Point pointA;
Point pointB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
canvas = findViewById(R.id.canvas);
canvas.setonTouchListener(fe);
}
//Moves the ball towards endpoint
public void movetowards()
{
slope += 2;
}
@Override
protected void onStart()
{
super.onStart();
final Handler handler0 = new Handler();
handler0.postDelayed(new Runnable() {
public void run() {
canvas.platformSpeed = canvas.platformSpeed + (2.0f * canvas.multiplier);
//if the platform goes out of bound then flip the direction
if(canvas.platformSpeed < 0 || canvas.platformSpeed > 600)
{
canvas.multiplier *= -1;
}
if(canvas.isBallMoving)
{
movetowards();
}
canvas.clearCanvas();
canvas.invalidate();
handler0.postDelayed(this,1);
}
},100);
}
private View.OnTouchListener fe = new View.OnTouchListener() {
@Override
public boolean onTouch(View v,MotionEvent ev) {
float invertedX = ev.getX();
float y = ev.getY();
int he = canvas.getMyBitmap().getHeight();
int wd = canvas.getMyBitmap().getWidth();
switch (ev.getAction())
{
case MotionEvent.ACTION_DOWN:
canvas.hasstarted = false;
case MotionEvent.ACTION_MOVE:
//Inverts for aiming like slingshot
canvas.aimX = wd - ev.getX();
canvas.aimY = he - ev.getY();
canvas.clearCanvas();
canvas.invalidate();
break;
case MotionEvent.ACTION_UP:
canvas.isBallMoving = true;
canvas.aimY = y;
float y1 = (canvas.projectileY * canvas.myBitmap.getHeight());
float y2 = canvas.aimY;
float y3 = (y2 - y1);
float x1 = (canvas.projectileX * canvas.myBitmap.getWidth());
float x2 = canvas.aimX;
float x3 = (x2 - x1);
slope = (y3 / x3);
xslope = (y3 / slope);
yslope = (x3 * slope);
}
return false;
}
};
}
我的画布
//Canvas.java
package com.example.homework2_hakeemthomas;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class MyCanvas extends View {
private Paint pixelPaint;
public Bitmap getMyBitmap() {
return myBitmap;
}
private Paint myBitMapPaint;
public Bitmap myBitmap;
private Canvas myCanvas;
private static int pixelPaintColor = Color.BLUE;
//Holds the movement for all objects that apply
public float projectileX = 0.2f,projectileY = 0.5f,projectileSize = 5.0f,platformX = 0.65f,platformY = 0.5f,aimX = 0.0f,aimY = 0.0f;
public float platformSpeed = 0.0f;
public float projectileSpeed = 0.0f;
//Changes the platforms direction
public int multiplier = 1;
public MainActivity ma;
public boolean hasstarted = true;
public boolean isBallMoving = false;
public MyCanvas(Context context,AttributeSet attrs)
{
super(context,attrs);
myBitMapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
pixelPaint = new Paint();
pixelPaint.setAntiAlias(true);
pixelPaint.setStyle(Paint.Style.stroke);
pixelPaint.setColor(pixelPaintColor);
pixelPaint.setstrokeCap(Paint.Cap.ROUND);
pixelPaint.setstrokeWidth(42);
}
@Override
protected void onSizeChanged(int w,int h,int oldw,int oldh)
{
super.onSizeChanged(w,h,oldw,oldh);
if(myBitmap == null)
{
myBitmap = Bitmap.createBitmap(w,Bitmap.Config.ARGB_8888);
myCanvas = new Canvas(myBitmap);
}
}
//Used to reset and move projectile
public void clearCanvas() {
myBitmap = Bitmap.createBitmap(myBitmap.getWidth(),myBitmap.getHeight(),Bitmap.Config.ARGB_8888);
myCanvas = new Canvas(myBitmap);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
int w = myBitmap.getWidth();
int h = myBitmap.getHeight();
pixelPaint.setColor(Color.RED);
pixelPaint.setstrokeWidth(60);
//The Projectile
myCanvas.drawCircle(((ma.slope + (w * projectileX)) + projectileSpeed),(((h * projectileY) - ma.slope) + projectileSpeed),projectileSize,pixelPaint);
if (hasstarted) {
myCanvas.drawLine(w * (projectileX + 0.05f),h * projectileY,w * (aimX + .25f),h * (aimY + .5f),pixelPaint);
}
if(!isBallMoving && !hasstarted) {
//The aimX and aimY are the endPoints
myCanvas.drawLine(w * (projectileX + 0.05f),aimX,aimY,pixelPaint);
}
pixelPaint.setColor(Color.GREEN);
myCanvas.drawRect((w * (platformX - .15f)) + platformSpeed,h * .5f,(w * platformX) + platformSpeed,h *.5f,pixelPaint);
canvas.drawBitmap(myBitmap,myBitMapPaint);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)