java的一个超级玛丽程序,跳的时候有问题

smallgill 2010-06-12 10:00:51
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;
}


}
...全文
286 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxg263 2010-06-13
  • 打赏
  • 举报
回复
什么呃,那么乱,格式化失败。。。。。
sxg263 2010-06-13
  • 打赏
  • 举报
回复
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;
图片就显示走两下,或跳一下就消失了
[code=Java]
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;
}


}
[/code] 我来Format楼主的代码。
  • 打赏
  • 举报
回复
没ide。看起来还是累……

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧