Android 前后移动球
试图让球在屏幕上来回移动(左-右)Android 前后移动球,android,Android,试图让球在屏幕上来回移动(左-右) 我尝试使用draw函数使用if语句更新球的位置 x += speed_x; y += speed_y; canvas.drawCircle(x, y, 20, paint); if (x == 0) speed_x=-1; if (x == getHeight()) speed_x=1; if (y == 0) speed_y = -1; if (y == getWidth()) speed_y = 1; invalidate(
我尝试使用draw函数使用if语句更新球的位置
x += speed_x;
y += speed_y;
canvas.drawCircle(x, y, 20, paint);
if (x == 0)
speed_x=-1;
if (x == getHeight())
speed_x=1;
if (y == 0)
speed_y = -1;
if (y == getWidth())
speed_y = 1;
invalidate();
这不起作用
**game.java*
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.ImageView;
public class GameWorld extends SurfaceView implements Runnable {
boolean isRunning;
GameObject obj;
SurfaceHolder holder;
Canvas canvas;
Thread gameThread;
Paint paint;
private Context mContext;
int x = -1;
int y = -1;
int speed_x=1, speed_y=1;
private int xVelocity = 10;
private int yVelocity = 5;
private Handler h;
private final int FRAME_RATE = 30;
public GameWorld(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
h = new Handler();
}
private Runnable r = new Runnable() {
@Override
public void run() {
invalidate();
}
};
public GameWorld(Context context){
super(context);
isRunning=true;
obj=new GameObject(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher),200,300);
paint=new Paint();
gameThread= new Thread(this);
gameThread.start();
holder=getHolder();
}
public void run(){
while(isRunning){
if(!holder.getSurface().isValid()){
continue;
}
update();
draw();
}
}
private void update(){
obj.update();
}
private void draw(){
canvas=holder.lockCanvas();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
obj.draw(canvas);
canvas.drawColor(Color.WHITE);
x+=speed_x;
y+=speed_y;
canvas.drawCircle(x, y, 20, paint);
if(x==0)
speed_x=-1;
if(x== getHeight())
speed_x=1;
if(y==0)
speed_y=-1;
if(y==getWidth())
speed_y=1;
invalidate();
holder.unlockCanvasAndPost(canvas);
}
public boolean onTouchEvent(MotionEvent event){
obj.jump();
return super.onTouchEvent(event);
}
}
**main:**
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GameWorld(this));
}
}
**gameobject.java**
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
public class GameObject {
int x,y;
int velY;
int width, height;
boolean jump;
Bitmap bitmap;
final int GRAVITY =2;
public GameObject(Bitmap bitmap, int x, int y){
this.x=x;
this.y=y;
this.width=bitmap.getWidth();
this.height=bitmap.getHeight();
this.bitmap=bitmap;
velY=0;
jump=false;
}
public void update(){
//handles input
if (jump){
velY=-30;
}
//add gravity
velY+=GRAVITY;
y+=velY;
//POSITION
if(y>300){
y=300;
velY=0;
}
jump=false;
}
public void jump(){
jump=true;
}
Paint paint = new Paint();
public void draw(Canvas canvas){
canvas.drawBitmap(bitmap,x,y,null);
int x=5; //ball
boolean game = true;
// while(game = true)
// {
int maxx = canvas.getWidth();
if (x <= maxx)
{
paint.setColor(Color.WHITE);
canvas.drawCircle(x, 305, 10, paint);
x= (x+2);
}
///else{
// x= (x-2);
// paint.setColor(Color.WHITE);
// canvas.drawCircle(x, 305, 10, paint);
// game = false;
//}
// }
}
public void moveball()
{
x= (x-2);
}
}
**game.java*
导入android.content.Context;
导入android.graphics.BitmapFactory;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.view.MotionEvent;
导入android.view.SurfaceHolder;
导入android.view.SurfaceView;
导入android.graphics.drawable.BitmapDrawable;
导入android.os.Handler;
导入android.util.AttributeSet;
导入android.widget.ImageView;
公共类游戏世界扩展SurfaceView实现可运行{
布尔运算;
配子对象对象;
表面焊钳;
帆布;
线程游戏线程;
油漆;
私有上下文;
int x=-1;
int y=-1;
整数速度x=1,速度y=1;
私有int xVelocity=10;
私人住宅单位面积=5;
私有处理器h;
专用最终整数帧_率=30;
公共游戏世界(上下文、属性集属性){
超级(上下文,attrs);
mContext=上下文;
h=新处理程序();
}
private Runnable r=new Runnable(){
@凌驾
公开募捐{
使无效();
}
};
公共游戏世界(上下文){
超级(上下文);
isRunning=true;
obj=新游戏对象(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher),200300);
油漆=新油漆();
gameThread=新线程(此线程);
gamesthread.start();
holder=getHolder();
}
公开募捐{
同时(正在运行){
如果(!holder.getSurface().isValid()){
继续;
}
更新();
draw();
}
}
私有void更新(){
obj.update();
}
私人提款(){
canvas=holder.lockCanvas();
油漆。设置颜色(颜色。黑色);
绘制.设置样式(绘制.样式.填充);
drawRect(0,0,getWidth(),getHeight(),paint);
对象绘制(画布);
画布。drawColor(颜色。白色);
x+=速度x;
y+=速度y;
画布.画圈(x,y,20,油漆);
如果(x==0)
速度x=-1;
如果(x==getHeight())
速度_x=1;
如果(y==0)
速度y=-1;
如果(y==getWidth())
速度y=1;
使无效();
支架。解锁画布和立柱(画布);
}
公共布尔onTouchEvent(运动事件){
obj.jump();
返回super.onTouchEvent(事件);
}
}
**主要内容:**
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
公共类MainActivity扩展了活动{
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(新游戏世界(this));
}
}
**gameobject.java**
导入android.graphics.Bitmap;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
公共类游戏对象{
int x,y;
内在的;
int宽度、高度;
布尔跳跃;
位图;
最终重力积分=2;
公共游戏对象(位图、整数x、整数y){
这个.x=x;
这个。y=y;
this.width=bitmap.getWidth();
this.height=bitmap.getHeight();
this.bitmap=位图;
f=0;
跳跃=假;
}
公共无效更新(){
//处理输入
如果(跳转){
VEL=-30;
}
//增加重力
重力+=重力;
y+=0;
//位置
如果(y>300){
y=300;
f=0;
}
跳跃=假;
}
公共跳转{
跳跃=真;
}
油漆=新油漆();
公共空白绘制(画布){
drawBitmap(位图,x,y,null);
int x=5;//球
布尔博弈=真;
//while(game=true)
// {
int maxx=canvas.getWidth();
如果(x这是我的版本,一个球根据它收到的击球移动
import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.View;
public class BouncingBallActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View boundcingBallView = new BouncingBallView(this);
setContentView(boundcingBallView);
}
}
这是将使球的实际视图
package com.example.bouncingball;
import java.util.Formatter;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class BouncingBallView extends View {
private int xMin=0,xMax,yMin=0,yMax;
private float ballRadius = 80,ballX = ballRadius+20, ballY= ballRadius+40,ballSpeedX=5,ballSpeedY=3,previousX,previousY;
private RectF ballBounds;
private Paint paint;
private StringBuilder statusmsg = new StringBuilder();
private Formatter formatter = new Formatter(statusmsg);
public BouncingBallView(Context context) {
super(context);
ballBounds = new RectF();
paint = new Paint();
paint.setDither(true);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setHinting(Paint.HINTING_ON);
paint.setPathEffect(new DashPathEffect(new float[] {1,1}, 0));
paint.setTypeface(Typeface.MONOSPACE);
paint.setTextSize(16);
this.setFocusableInTouchMode(true);
}
@Override
protected void onDraw(Canvas canvas) {
ballBounds.set(ballX-ballRadius , ballY-ballRadius,ballX+ballRadius,ballY+ballRadius);
paint.setColor(Color.GREEN);
canvas.drawOval(ballBounds, paint);
paint.setColor(Color.BLACK);
canvas.drawText(statusmsg.toString(), 10, 30,paint);
update();
invalidate();
}
private void update() {
ballX +=ballSpeedX;
ballY+=ballSpeedY;
if(ballX+ballRadius> yMax) {
ballSpeedX =-ballSpeedX;
ballX = xMax -ballRadius;
}
else if(ballX - ballRadius < xMin) {
ballSpeedX = -ballSpeedX;
ballX = xMin+ballRadius;
}
if(ballY + ballRadius > yMax) {
ballSpeedY = -ballSpeedY;
ballY = yMax-ballRadius;
}
else if (ballY - ballRadius < yMin) {
ballSpeedY = -ballSpeedY;
ballY = yMin+ballRadius;
}
statusmsg.delete(0, statusmsg.length());
formatter.format("Ball@(%3.0f,%3.0f),Speed=(%2.0f,%2.0f)", ballX, ballY,ballSpeedX, ballSpeedY);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
xMax = w-1;
yMax = h-1;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_RIGHT:
ballSpeedX++;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
ballSpeedX--;
break;
case KeyEvent.KEYCODE_DPAD_UP:
ballSpeedY--;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
ballSpeedY++;
break;
case KeyEvent.KEYCODE_DPAD_CENTER:
ballSpeedX = 0;
ballSpeedY = 0;
break;
case KeyEvent.KEYCODE_A:
float maxRadius = (xMax > yMax) ? yMax / 2* 0.9f : xMax / 2 * 0.9f;
if(ballRadius < maxRadius)
ballRadius*=1.05;
break;
case KeyEvent.KEYCODE_Z:
if(ballRadius>20){
ballRadius *=0.95;
}
break;
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float currentX=event.getX();
float currentY = event.getY();
float deltaX,deltaY;
float scalingFactor = 5.0f / ((xMax > yMax) ? yMax : xMax);
switch(event.getAction()) {
case MotionEvent.ACTION_MOVE:
deltaX = currentX - previousX;
deltaY = currentY - previousY;
ballSpeedX += deltaX*scalingFactor;
ballSpeedY += deltaY*scalingFactor;
}
previousX = currentX;
previousY = currentY;
return true;
}
}
package com.example.bouncingball;
导入java.util.Formatter;
导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.DashPathEffect;
导入android.graphics.Paint;
导入android.graphics.RectF;
导入android.graphics.Typeface;
导入android.view.KeyEvent;
导入android.view.MotionEvent;
导入android.view.view;
导入android.widget.Toast;
公共类BouncingBallView扩展视图{
私有整数xMin=0,xMax,yMin=0,yMax;
私人浮动球半径=80,球X=球半径+20,球Y=球半径+40,球速度X=5,球速度=3,上一个,上一个;
私有RectF边界;
私人油漆;
私有StringBuilder statusmsg=新StringBuilder();
专用格式化程序格式化程序=新格式化程序(statusmsg);
公共BouncingBallView(上下文){
超级(上下文);
ballBounds=新的RectF();
油漆=新油漆();
绘制。设置抖动(真);
paint.setAntiAlias(真);
paint.setFilterBitmap(真);
画画。设置暗示(画画。暗示);
setPathEffect(新的DashPathEffect(新的float[]{1,1},0));
绘制.setTypeface(字体.MONOSPACE);
油漆.尺寸(16);
此.setFocusableInTouchMode(true);
}
@凌驾
受保护的void onDraw(画布){
ballBounds.set(球X球半径、球Y球半径、球X+球半径、球Y+球半径);
油漆。设置颜色(颜色。绿色);
帆布。拉延椭圆形(球边、油漆);
油漆。设置颜色(颜色。黑色);
canvas.drawText(状态)