Android 在边缘反弹

Android 在边缘反弹,android,animation,android-animation,android-canvas,onfling,Android,Animation,Android Animation,Android Canvas,Onfling,我想做一个小游戏,你可以在屏幕上扔硬币。当我搜索那种动画时,我找到了这个页面 我设法根据我的目标定制事件,但我无法在屏幕上设置边界,以便硬币(位图)在边缘反弹 我尝试了几次计算,但最终硬币的移动是基于边缘的接触点的 有人能帮我一下吗?根据网站上的工作代码,下面是我的乒乓球游戏的示例代码。每个对象都有位置(x,y)及其速度。运动是应用于位置的速度。一个额外的信息是屏幕坐标系从左上角开始。Y轴下降但增加。由于屏幕坐标从0开始,因此边界变为screen.width-1和screen.height-1

我想做一个小游戏,你可以在屏幕上扔硬币。当我搜索那种动画时,我找到了这个页面

我设法根据我的目标定制事件,但我无法在屏幕上设置边界,以便硬币(位图)在边缘反弹

我尝试了几次计算,但最终硬币的移动是基于边缘的接触点的


有人能帮我一下吗?根据网站上的工作代码,下面是我的乒乓球游戏的示例代码。每个对象都有位置(x,y)及其速度。运动是应用于位置的速度。一个额外的信息是屏幕坐标系从左上角开始。Y轴下降但增加。由于屏幕坐标从0开始,因此边界变为screen.width-1和screen.height-1

@Override
public void onDraw(Canvas c){

    p.setStyle(Style.FILL_AND_STROKE);
    p.setColor(0xff00ff00);




            // If ball's current x location plus the ball diameter is greater than screen width - 1, then make speed on x axis negative. Because you hit the right side and will bounce back to left.
        if((Px+ballDiameter) > width - 1){ 

            Vx = -Vx;
        }
            // If ball's current y location plus the ball diameter is greater than screen height -1, then make speed on y axis negative. Because you hit the bottom and you will go back up.
        if((Py + ballDiameter) > height - 1){

            Vy = -Vy;
        }
            // If current x location of the ball minus ball diameter is less than 1, then make the speed on x axis nagative. Because you hit to the left side of the screen and the ball would bounce back to the right side
        if((Px - ballDiameter) < 1){

            Vx = -Vx;
        }
            // If current y location of the ball minus ball diameter is less than 1, then make the speed on y axis negative. Because the ball hit the top of the screen and it should go down.
        if((Py - ballDiameter ) <1){
            Dy = 1;
            Vy = -Vy;
        }




          Px += Vx; // increase x position by speed
          Py += Vy; // increase y position by speed

        c.drawCircle(Px, Py, ballDiameter, p);



        invalidate();

}
@覆盖
公共空白onDraw(画布c){
p、 设置样式(样式、填充和冲程);
p、 设置颜色(0xff00ff00);
//如果球的当前x位置加上球的直径大于屏幕宽度-1,则将x轴上的速度设为负值。因为你击中了右侧,会反弹回左侧。
如果((Px+球径)>宽度-1){
Vx=-Vx;
}
//如果球的当前y位置加上球的直径大于屏幕高度-1,则y轴上的速度为负值。因为你击中底部,你将返回。
如果((Py+球径)>高度-1){
Vy=-Vy;
}
//如果球的当前x位置减去球直径小于1,则使x轴上的速度为负值。因为你击中屏幕的左侧,球会反弹回右侧
如果((Px-球径)<1){
Vx=-Vx;
}
//如果球的当前y位置减去球直径小于1,则将y轴上的速度设为负值。因为球撞击屏幕顶部,应该向下移动。
如果((Py-球径)