gpt4 book ai didi

java - 我正在构建一个汽车游戏我想通过 keyevent 移动汽车,算法没问题,但汽车不动

转载 作者:行者123 更新时间:2023-11-30 03:24:28 25 4
gpt4 key购买 nike

主类是:

public class test extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Game(this,null));
}

Game类是view类的扩展,可以让我们画小车并设置位置,我调用了onkey方法来控制小车的方向和角度

public class Game extends View {
private Paint paint= new Paint();
float x,y;
float speed = 5,angle = 0,mod = 0;
Bitmap car;
Boolean b=false;
public Game(Context context, AttributeSet attrs) {
super(context, attrs);
try { // here I call the car image as a bitmap object
AssetManager assetManager=context.getAssets();
InputStream inputStream= assetManager.open("car.png");
BitmapFactory.Options options=new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_4444;
car=BitmapFactory.decodeStream(inputStream,null,options);
inputStream.close();
Log.d("Bitmap Text", "bobrgb888.png format: "+car.getConfig());
} catch (Exception e) {
// TODO: handle exception
}
}

@Override
protected void onDraw(Canvas canvas) {
x += (speed*mod) * Math.cos(Math.PI/180 * angle);
y += (speed*mod) * Math.sin(Math.PI/180 * angle);
canvas.rotate((float) (Math.PI/180 * angle));
canvas.drawBitmap(car,-(car.getWidth()/2), -(car.getHeight()/2), null);
canvas.restore();
}
public boolean onKey(View view, int keyCode, KeyEvent event) {
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
break;
case KeyEvent.ACTION_UP:
{if(event.getKeyCode() == KeyEvent.KEYCODE_A || event.getKeyCode() == KeyEvent.KEYCODE_B)
{
this.mod = 0;
}}
break;

case KeyEvent.ACTION_MULTIPLE:{
if(event.getKeyCode() == KeyEvent.KEYCODE_A)
{
mod = 1;
}
if(event.getKeyCode() == KeyEvent.KEYCODE_B)
{
mod = -1;
}
if(event.getKeyCode() == KeyEvent.KEYCODE_C)
{
angle -= 5;
}

if(event.getKeyCode() == KeyEvent.KEYCODE_D)
{
angle+=5;
}
}
break;
}
invalidate();
return event.getKeyCode() != KeyEvent.KEYCODE_BACK;
}
}

我设置下一个点的 x 和 y我按照选择的角度旋转图像我在当前位置画车。当 A 或 B 键向上时,汽车停止长按按键(A、B、C、D)时,角度和方向发生变化。

最佳答案

使用这个:

@Override
public boolean onKeyDown(View view, Editable arg1, int keyCode, KeyEvent event) {

而不是这个:

public boolean onKey(View view, int keyCode, KeyEvent event) {

关于java - 我正在构建一个汽车游戏我想通过 keyevent 移动汽车,算法没问题,但汽车不动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18478116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com