package com.yyy;
import android.util.Log;
public class BallThread extends Thread {
Movable movable;
boolean flag = false; //线程执行标志位
int sleepSpan = 40; //线程休眠时间
float g = 200f; //加速度
double currentTime; //当前时间
int bitmapWidth;
int windowWidth;
public BallThread(Movable movable,int bitmapWidth,int windowWidth) {
super();
this.movable = movable;
this.flag = true;
this.bitmapWidth = bitmapWidth;
this.windowWidth = windowWidth;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while(flag){
currentTime = System.nanoTime();
//水平方向走过的时间
double timeSpan_X = (double)(currentTime - movable.startTime_X)/1000/1000/1000;
/*=====================================================================================*/
//判断小球是否已弹到屏幕边缘,弹到右边缘后则改向左弹,弹到左边缘后则改向右弹
if(movable.moveRight){
if(movable.currentX+bitmapWidth>=windowWidth){
movable.moveRight = false;
movable.currentX = (int) (movable.startX-movable.currentVX*timeSpan_X);
}else{
movable.currentX = (int) (movable.startX+movable.currentVX*timeSpan_X);
}
}else{
if(movable.currentX<=0){
movable.moveRight = true;
movable.currentX = (int) (movable.startX+movable.currentVX*timeSpan_X);
}else{
movable.currentX = (int) (movable.startX-movable.currentVX*timeSpan_X);
}
}
/*=====================================================================================*/
//当前X坐标
//movable.currentX = (int) (movable.startX+movable.currentVX*timeSpan_X);
if(movable.bFall){ //球正在下落
//垂直方向走过的时间
double timeSpan_Y = (double)(currentTime-movable.startTime_Y)/1000/1000/1000;
//当前Y坐标,
movable.currentY = (int) (movable.startY+movable.startVY*timeSpan_Y+timeSpan_Y*timeSpan_Y*g/2);
//当前垂直方向速度
movable.currentVY = (float) (movable.startVY+timeSpan_Y*g);
//判断球是否达到最高点
if(movable.startVY<0 && Math.abs(movable.currentVY)<=BallView.UP_ZERO){
movable.startTime_Y = System.nanoTime();
movable.startVY = 0;
movable.currentVY = 0;
movable.startY = movable.currentY;
}
//判断小球是否撞地
if(movable.currentVY>0 && movable.currentY+movable.radius*2>=BallView.BOTTOM_LINE){
//衰减水平方向的速度
movable.currentVX = movable.currentVX*(1-movable.impactFactor);
//衰减垂直方向的速度并改变方向
movable.currentVY = 0 - movable.currentVY*(1-movable.impactFactor);
//判断撞地衰减后的速度,太小就停止
if(Math.abs(movable.currentVY) < BallView.DOWN_ZERO){
this.flag = false;
DrawThread.moveBallNum--;
}else{
movable.startVY = movable.currentVY;
movable.startY = movable.currentY;
movable.startTime_Y = System.nanoTime();
}
}
movable.startX = movable.currentX;
movable.startTime_X = System.nanoTime();
}else if((movable.currentX+movable.radius/2) >= BallView.WOOD_EDGE){
//说明球已移出挡板
movable.startTime_Y = System.nanoTime();
movable.bFall = true;
}
try {
Thread.sleep(sleepSpan);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android 使用SurfaceView实现小球高处落下并弹起的效果
共50个文件
png:22个
class:11个
java:6个
5星 · 超过95%的资源 需积分: 16 140 下载量 45 浏览量
2013-03-25
00:39:02
上传
评论 1
收藏 270KB RAR 举报
温馨提示
android 使用SurfaceView实现小球高处落下并弹起的效果 此程序是在《android游戏开发大全》第7章第2节案例的基础上做的改进
资源推荐
资源详情
资源评论
收起资源包目录
Sample_7_1_DropBall.rar (50个子文件)
Sample_7_1_DropBall
.project 855B
project.properties 360B
proguard.cfg 1KB
src
com
yyy
DrawThread.java 1KB
Movable.java 2KB
BallView.java 3KB
DropBallActivity.java 1KB
BallThread.java 3KB
AndroidManifest.xml 728B
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ball_green_small.png 2KB
ic_launcher.png 3KB
ball_red.png 5KB
ball_green.png 5KB
ball_purple.png 5KB
back.png 27KB
wood.png 5KB
ball_red_small.png 2KB
ball_purple_small.png 2KB
layout
main.xml 320B
values
strings.xml 193B
.settings
org.eclipse.jdt.core.prefs 208B
assets
gen
com
yyy
R.java 1KB
.classpath 364B
bin
resources.ap_ 62KB
classes
com
yyy
R$layout.class 361B
R$drawable.class 650B
R$attr.class 307B
DrawThread.class 2KB
BallThread.class 2KB
DropBallActivity.class 1KB
R$id.class 349B
R.class 454B
R$string.class 394B
BallView.class 4KB
Movable.class 2KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ball_green_small.png 2KB
ic_launcher.png 2KB
ball_red.png 5KB
ball_green.png 4KB
ball_purple.png 4KB
back.png 27KB
wood.png 2KB
ball_red_small.png 2KB
ball_purple_small.png 2KB
classes.dex 10KB
Sample_7_1_DropBall.apk 69KB
共 50 条
- 1
Central-Perk
- 粉丝: 474
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页