English 中文(简体)
im correctly correctly
原标题:Getting animation to work correctly with Java KeyEvent

我 st忙执行2个估算框架(链接——2号,随后是链接——1号),然后将<代码>d加以修改。 它只是执行一个估算框架(链接_frame_2_face_right.png)。

Here s the code:

import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import acm.graphics.GImage;
import acm.graphics.GPoint;
import acm.program.GraphicsProgram;


public class LinkGame extends GraphicsProgram {

    public void run(){
        setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
        addLink();
        addKeyListeners();
        addMouseListeners();
    }
    private void addLink(){
        linkCharacter = new GImage("link sprites/link_frame_1_face_right.png");
            add(linkCharacter,link_Location_XCoord,link_Location_YCoord);
    }
    public void keyPressed(KeyEvent e){ 
        char linkMoveRightKey = e.getKeyChar();
        if(linkMoveRightKey ==  d ){
            // y should not change so goku moves in a straight line
                link_Location_YCoord = 0;
                linkCharacter.move(link_Location_XCoord,link_Location_YCoord);
                    set_Link_Anim_Frame_2_face_left();
            }
    }
    public void set_Link_Anim_Frame_2_face_left(){
        linkCharacter.setImage("link sprites/link_frame_2_face_right.png");         
    }
    public void set_Link_Anim_Frame_1_face_left(){  
        linkCharacter.setImage("link sprites/link_frame_1_face_right.png");
    }
    private GImage linkCharacter;
    private  int link_Location_XCoord = 50;
    private  int link_Location_YCoord = 50 ;
    private final int APPLICATION_WIDTH = 600;
    private final int APPLICATION_HEIGHT = 600;
}
问题回答

此时此刻,在你动向时,没有更新显示的内容。 如果你使用一系列图像和 lo,那么你就能够得出任何数字。 我在以下法典中提供了最新情况,以便你能够尝试。 我增加了一种速度,而不是利用你最初的立场,以便你能够独立改变这两个价值观。 进展速度缓慢也使你看到框架变化更为容易。

import java.awt.event.KeyEvent;

import acm.graphics.GImage;
import acm.program.GraphicsProgram;

public class LinkGame extends GraphicsProgram {

    public void run(){
        setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
        addLink();
        addKeyListeners();
        addMouseListeners();
    }

    private void addLink(){
        linkCharacter = new GImage("link sprites/link_frame_1_face_right.png");
            add(linkCharacter,link_Location_XCoord,link_Location_YCoord);
    }

    public void keyPressed(KeyEvent e){ 
        char linkMoveRightKey = e.getKeyChar();
        if(linkMoveRightKey ==  d ){
                linkCharacter.move(xSpeed,ySpeed);
                linkCharacter.setImage(images[frame]);
                frame++;
                if(frame>=images.length){
                    frame = 0;
                }
            }
    }
    private GImage linkCharacter;
    private  int link_Location_XCoord = 50;
    private  int link_Location_YCoord = 50 ;
    private final int APPLICATION_WIDTH = 600;
    private final int APPLICATION_HEIGHT = 600;
    private String[] images = {"link sprites/link_frame_1_face_right.png","link sprites/link_frame_2_face_right.png"}; //Add in as many images as you want for your animation
    private int frame = 0;
    private int xSpeed = 1; //the number of pixels to move in x
    private int ySpeed = 0; //0 so you only move horizontally
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签