首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在不创建另一个类的情况下将鼠标按下的方法实现到动画游戏中?

如何在不创建另一个类的情况下将鼠标按下的方法实现到动画游戏中?
EN

Stack Overflow用户
提问于 2015-01-08 10:14:33
回答 1查看 136关注 0票数 0

这是flappy bird的代码。所有这些都在一个类中,我不知道这是不是一个问题,我该如何解决它?鼠标侦听器的实现可能是错误的。

代码语言:javascript
复制
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;


public class Flappy extends JFrame implements MouseListener {


public static Image loadImage (String name)  //Loads image from file
{
    BufferedImage img = null;
    try
    {
        img = ImageIO.read (new File (name));
    }
    catch (IOException e)
    {
    }
    return img;
}

/**
 * 
 */
private static final long serialVersionUID = 1L;
Image back,ground, bird, upper, lower;
 int y=200,vy=1;//speed and acceleration respectively
 int xPipe1=600,yPipe1=(int)(Math.random()*300)+250;//random numbers for pipes
int xPipe2=900,yPipe2=(int)(Math.random()*300)+250;
 int xPipe3=1200,yPipe3=(int)(Math.random()*300)+250;
public Flappy ()
{
    this.setTitle("Flappy Bird");//the jframe
    this.setSize(400,700);
    this.setResizable(false);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    this.addMouseListener(this);
//Here is the problem, adding the mouse listener to the jframe
}

public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub

    new Flappy();



}
public void paint (Graphics g) {

    //loop that draws the images
    Graphics2D g2d = (Graphics2D) g;
    ImageIcon i=new ImageIcon("image/background.jpg");
    back=i.getImage();
    ImageIcon j=new ImageIcon("image/ground.png");
    ground=j.getImage();
    ImageIcon k=new ImageIcon("image/wingup.png");
    bird=k.getImage();
    ImageIcon l=new ImageIcon("image/pipe-up.png");
    lower=l.getImage();
    ImageIcon m=new ImageIcon("image/pipe-down.png");
    upper=m.getImage();


    for (int backPos=0;backPos>-1200;backPos--)
    {
        try{
            Thread.sleep(30);
        }
        catch(InterruptedException ie)
        {
            ie.printStackTrace();
        }
        g2d.drawImage(back, backPos, 0, null);



        g2d.drawImage(lower,xPipe1,yPipe1,null);
        g2d.drawImage(upper,xPipe1,yPipe1-600,null);
        xPipe1-=5;
        g2d.drawImage(lower,xPipe2,yPipe2,null);
        g2d.drawImage(upper,xPipe2,yPipe2-600,null);
        xPipe2-=5;
        g2d.drawImage(lower,xPipe3,yPipe3,null);
        g2d.drawImage(upper,xPipe3,yPipe3-600,null);
        xPipe3-=5;
        if(xPipe1==-80)
        {
            xPipe1=800;
            yPipe1=(int)(Math.random()*300)+250;
        }


        if(xPipe2==-80)
        {
            xPipe2=800;
            yPipe2=(int)(Math.random()*300)+250;
        }
        if(xPipe3==-80)
        {
            xPipe3=800;
            yPipe3=(int)(Math.random()*300)+250;
        }

        g2d.drawImage(ground,0,129,null);



        g2d.drawImage(bird,150,y,null);
        if (y<590)
        {
        vy+=3;
        y+=vy;
        }
        else
        {
            y=590;

        }
        if(backPos==-1100)
        {
            backPos=0;
        }
    }

}
@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub


}
@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
    vy=-100;//The bird is supposed to flap here, but there is no input
}
@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
}
EN

回答 1

Stack Overflow用户

发布于 2015-01-08 10:57:44

尝试一下:在构造函数中定义东西时,创建一个新的mouseListener对象,而不是为整个类实现一个mouseListener。例如:

代码语言:javascript
复制
public flappy(){
    //define stuff here
    this.addMouseListener(new MouseListener(){
        public void onClick(MouseEvent e){
            //action goes here
        }
        //you will get an error to include other voids. Just click on "add unimplemented methods".
    });
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27831917

复制
相关文章

相似问题

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