如何使Java中的精灵跳?(How to make sprite jump in java?)

2019-09-02 14:12发布

我的KeyEvents为移动左,右,上,下一个精灵。 我只是插科打诨,并提前思考在我想精灵跳到另一个项目。 它不必是完全现实的,因为我才刚刚开始。 我所拥有的是当按下空格键,这将导致精灵跳,让说,“DY = -3”。 于是我对的keyReleased该KeyEvent,它会下跌,“DY = -2”。 这不起作用的精灵只是继续下跌......有人可以照一些轻?

整个代码:包碰撞;

    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;

    import java.util.ArrayList;

    import javax.swing.ImageIcon;

    public class Craft {

    private String craft = "pelican.png";

    private int dx;
    private int dy;
    private int x;
    private int y;
    private int width;
    private int height;
    private boolean visible;
    private Image image;
    private ArrayList missiles;


    public Craft() {
    ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
    image = ii.getImage();
    width = image.getWidth(null);
    height = image.getHeight(null);
    missiles = new ArrayList();
    visible = true;
    x = 100;
    y = 300;
    }


    public void move() {

    x += dx;
    y += dy;

    if (x < 1) {
        x = 1;
    }

    if (y < 1) {
        y = 1;
    }

    }

    public int getX() {
    return x;
    }

    public int getY() {
    return y;
    }

    public Image getImage() {
    return image;
    }

    public ArrayList getMissiles() {
    return missiles;
    }

    public void setVisible(boolean visible) {
    this.visible = visible;
    }

    public boolean isVisible() {
    return visible;
    }

     public Rectangle getBounds() {
    return new Rectangle(x, y, width, height);
    }

    public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_SPACE) {

    }
    if (key == KeyEvent.VK_V){
        dx = 6;
    }

    if (key == KeyEvent.VK_LEFT) {
        dx = -1;
    }

    if (key == KeyEvent.VK_RIGHT) {
        dx = 2;
    }

    if (key == KeyEvent.VK_UP) {
        dy = -1;
    }

    if (key == KeyEvent.VK_DOWN) {
        dy = 1;
    }
    }

    public void fire() {
    missiles.add(new Missile(x + width, y + height/2));
    }

    public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {
        dx = 0;
    }
    if (key == KeyEvent.VK_SPACE) {

    }

    if (key == KeyEvent.VK_RIGHT) {
        dx = 0;
    }

    if (key == KeyEvent.VK_UP) {
        dy = 0;
    }

    if (key == KeyEvent.VK_DOWN) {
        dy = 0;
    }
    }
    }

正如你可能已经注意到林新到Java以及游戏编程。 我想要的是精灵上去,然后再回来了。 它会一直保持静止是否有帮助。 精灵只是不断跳跃,直到他被一个打过来的障碍。 我知道有其他动作的代码,但一旦我开始对下一个精灵那些将被删除。

Answer 1:

这是基本的概念。 您的实现将取决于你的引擎的实现而改变。

其基本思路是玩家有一个垂直的增量是由重力随时间变化。 这影响了精灵垂直速度。

这种实现也有一个重新绑定三角洲,这使得精灵重新绑定,而在“串门”突然。 再结合的由重新结合的降解,从而降低量进行重新结合在每个重新结合。

这是模拟游戏角色,所以你需要打空间来启动它反弹...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class JumpingSprite {

    public static void main(String[] args) {
        new JumpingSprite();
    }

    public JumpingSprite() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        protected static final int SPRITE_HEIGHT = 10;
        protected static final int SPRITE_WIDTH = 10;
        private float vDelta; // The vertical detla...
        private float rbDelta; // Rebound delta...
        private float rbDegDelta; // The amount the rebound is degradation...
        private int yPos; // The vertical position...
        private float gDelta; // Gravity, how much the vDelta will be reduced by over time...
        private Timer engine;
        private boolean bounce = false;

        public TestPane() {

            yPos = getPreferredSize().height - SPRITE_HEIGHT;
            vDelta = 0;
            gDelta = 0.25f;
            // This is how much the re-bound will degrade on each cycle...
            rbDegDelta = 2.5f;

            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "jump");
            am.put("jump", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Can only bound when we're actually on the ground...
                    // You might want to add fudge factor here so that the 
                    // sprite can be within a given number of pixels in order to
                    // jump again...
                    if (yPos + SPRITE_HEIGHT == getHeight()) {
                        vDelta = -8;
                        rbDelta = vDelta;
                        bounce = true;
                    }
                }
            });

            engine = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int height = getHeight();
                    // No point if we've not been sized...
                    if (height > 0) {
                        // Are we bouncing...
                        if (bounce) {
                            // Add the vDelta to the yPos
                            // vDelta may be postive or negative, allowing
                            // for both up and down movement...
                            yPos += vDelta;
                            // Add the gravity to the vDelta, this will slow down
                            // the upward movement and speed up the downward movement...
                            // You may wish to place a max speed to this
                            vDelta += gDelta;
                            // If the sprite is not on the ground...
                            if (yPos + SPRITE_HEIGHT >= height) {
                                // Seat the sprite on the ground
                                yPos = height - SPRITE_HEIGHT;
                                // If the re-bound delta is 0 or more then we've stopped
                                // bouncing...
                                if (rbDelta >= 0) {
                                    // Stop bouncing...
                                    bounce = false;
                                } else {
                                    // Add the re-bound degregation delta to the re-bound delta
                                    rbDelta += rbDegDelta;
                                    // Set the vDelta...
                                    vDelta = rbDelta;
                                }
                            }
                        }
                    }
                    repaint();
                }
            });
            engine.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int width = getWidth() - 1;
            int xPos = (width - SPRITE_WIDTH) / 2;
            g2d.drawOval(xPos, yPos, SPRITE_WIDTH, SPRITE_HEIGHT);
            g2d.dispose();
        }
    }
}


Answer 2:

嗯,我能想到的一种方式。 它涉及数学(抛物线)的一些复杂的数额。 所以我将提供一个非常简单的答案。

int y = 0;

并且,对于空格键测试方法...

if (y !< 1){
    if (y < 30){

   y += 1;

   }

   if (y > 30){

    y -= 1;


    }
}

我没有测试出来呢,但它应该在理论工作....但它不会动画任何东西,这个代码只是要带精灵Y值,并使其上升。 这只是约了可能存在的最简单的方法跳跃....



文章来源: How to make sprite jump in java?
标签: java sprite