if(onLand&&upTime==0){
if(this.status.indexOf("left")!=-1){
this.status="left--standing";
}else{
this.status="right--standing";
}
}else{
//为上升状态
if(upTime!=0){
upTime--;
}else{
this.down();
}
}
//如果加上
y+=ymove;
图片就显示走两下,或跳一下就消失了
package mario;
import java.awt.image.BufferedImage;
public class Mario implements Runnable{
//速度
private int xmove=0;
private int ymove=0;
//定义一个场景对象,保存mario当前场景;
private BackGround bg;
//坐标
private int x;
private int y;
//加入线程
private Thread t=null;
//状态
private String status;
//显示图片
private BufferedImage shoeImage;
//生命数和分数
private int score;
private int live;
//当前移动中显示的图片索引、
private int moving=0;
//下落的时间
private int upTime=0;
//构造方法
public Mario(int x,int y){
this.x=x;
this.y=y;
//初始化Mario图片
this.shoeImage=StaticValue.allMarioImage.get(0);
this.score=0;
this.live=3;
t=new Thread(this);
t.start();
this.status="right--standing";
}
public void leftMove(){
//改变速度
xmove=-10;
//改变状态
status="left--moving";
}
public void rightMove(){
//改变速度
this.xmove=10;
//改变状态
status="right--moving";
}
public void leftStop(){
this.xmove=0;
this.status="left--standing";
}
public void rightStop(){
this.xmove=0;
this.status="right--standing";
}
public void jump(){
if(this.status.indexOf("jumping")==-1){
if(this.status.indexOf("left")!=-1){
this.status="left--jumping";
}else{
this.status="right--jumping";
}
ymove=-5;
upTime=35;
}
}
//加入下落的方法
public void down(){
if(this.status.indexOf("left")!=-1){
this.status="left--jumping";
}else{
this.status="right--jumping";
}
ymove=5;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public BufferedImage getShoeImage() {
return shoeImage;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
//判断当前mario是否与障碍物碰撞
//定义标记
boolean canLeft=true;
boolean canRight=true;
boolean onLand=false;
for(int i=0;i<this.bg.getAllObstruction().size();i++){
Obstruction ob=this.bg.getAllObstruction().get(i);
//不能继续向右移动
if(ob.getX()==this.x+60&&(ob.getY()+60>this.y&&ob.getY()-60<this.y)){
canRight=false;
}
//不能继续向左移动
if(ob.getX()==this.x-60&&(ob.getY()+60>this.y&&ob.getY()-60<this.y)){
canLeft=false;
}
//可以跳跃,在陆地上
if(ob.getY()==this.y+60&&(ob.getX()+60>this.x&&ob.getX()-60<this.x)){
onLand=true;
}
if(onLand&&upTime==0){
if(this.status.indexOf("left")!=-1){
this.status="left--standing";
}else{
this.status="right--standing";
}
}else{
//为上升状态
if(upTime!=0){
upTime--;
}else{
this.down();
}
}
}
if(canLeft&&xmove<0||(canRight&&xmove>0)){
x+=xmove;
}
//定义一个图片取得的初始索引数
int temp=0;
//当前为面向左
if(this.status.indexOf("left")!=-1){
temp+=5;
}
//当前是否移动
if(this.status.indexOf("moving")!=-1){
temp+=this.moving;
moving++;
if(moving==4){
moving=0;
}
}
//改变显示图片
this.shoeImage=StaticValue.allMarioImage.get(temp);
try {
Thread.sleep(25);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void setBg(BackGround bg) {
this.bg = bg;
}
}