首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创造落球

创造落球
EN

Stack Overflow用户
提问于 2015-08-01 17:22:45
回答 1查看 76关注 0票数 0

当按下play按钮开始游戏(活动)时,我想要创建一个从屏幕顶部掉下来的球的实例(球稍后会碰到一些东西)。我不知道该怎么做。有人能帮忙吗?

到目前为止我正在做的是:

代码语言:javascript
复制
public class GameActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Turn title off
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // set Game to fullscreen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        View ballView = new BallView(this);
        setContentView(ballView);
        // ballView.setBackgroundColor(Color.TRANSPARENT);
    }
}

代码语言:javascript
复制
public class BallView extends View {

    // Ball attributes
    private float ballRadius = 30;
    private float ballX = 50;
    private float ballY = 50;
    private RectF ballBounds;
    private Paint ballColor;
    // For game elements
    private int score = 0;

    public BallView(Context context){
        super(context);
        // Initialize game elements
        ballBounds = new RectF();
        ballColor = new Paint();
        this.setFocusableInTouchMode(true);
    }

    public void onDraw(Canvas canvas){
        // Draw ball
        ballBounds.set(ballX - ballRadius, ballY - ballRadius, ballX + ballRadius, ballY + ballRadius);
        ballColor.setColor(Color.RED);
        canvas.drawOval(ballBounds, ballColor);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-01 19:15:07

根据对您的意图的描述,您可能应该使用OpenGL或像李氏体这样的框架,该框架内置了物理和碰撞检测。

但要回答你的问题,你可以试试这个简单的例子:

代码语言:javascript
复制
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;
import android.view.ViewTreeObserver;

public class BallView extends View{

    // Ball attributes
    private float ballRadius = 30;
    private float ballX = 50;
    private float ballY = 50;
    private float ballSpeed = 10.0f;
    private RectF ballBounds;
    private Paint ballColor;
    boolean doBallAnimation = false;
    // For game elements
    private int score = 0;
    ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
    long startTime, prevTime; // Used to track elapsed time for animations and fps

    public BallView(Context context){
        super(context);
        // Initialize game elements
        ballBounds = new RectF();
        ballColor = new Paint();
        this.setFocusableInTouchMode(true);

        getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener(){
                    @Override
                    public void onGlobalLayout(){
                        getViewTreeObserver().removeOnGlobalLayoutListener(this);

                        ballY = 0;
                        ballX = (getWidth()- ballRadius) / 2.0f;

                        doBallAnimation = true;
                        animator.start();
                    }
                }
        );

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
            @Override
            public void onAnimationUpdate(ValueAnimator arg0){
                long nowTime = System.currentTimeMillis();
                float secs = (float)(nowTime - prevTime) / 1000f;
                prevTime = nowTime;

                if((ballY + ballSpeed) > (getHeight() - (ballRadius)))
                {
                    animator.cancel();
                    return;
                }

                ballY += ballSpeed;

                // Force a redraw to see the ball in its new position
                invalidate();
            }
        });
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setDuration(3000);
    }

    public void onDraw(Canvas canvas){
        // Draw ball
        if(doBallAnimation)
        {
            ballBounds.set(ballX - ballRadius, ballY - ballRadius, ballX + ballRadius, ballY + ballRadius);
            ballColor.setColor(Color.RED);
            canvas.drawOval(ballBounds, ballColor);
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31764251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档