Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Jumping Issue

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Jumping Issue

    There is a problem in my 2D slide scrolling game where the character can't jump until he reaches the enemy, I cant find in the code where anything would cause it.

    The Boards class-
        package OurGame;
     
        import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
     
     
    import javax.swing.*;
     
    public class Board extends JPanel implements ActionListener, Runnable {
                            Dude p;
                public Image img;
                Timer time;
                static int v = 327;
                Thread animator;
        Enemy en;
        Enemy en2;
                    boolean lost = false;
     
                boolean a = false;
                boolean done2 = false;
     
                static Font font = new Font("SanSerif", Font.BOLD, 24);
                public Board() {
                        p = new Dude();
                        addKeyListener(new AL());
                        setFocusable(true);
                        ImageIcon i = new ImageIcon("C:/Users/brian_000/Desktop/The Background.png");
                        img = i.getImage();
                        time = new Timer(5, this);
                        time.start();
                        en = new Enemy(942, 270, "C:/Users/brian_000/Desktop/REnemy.png");
                        en2 = new Enemy(942, 270, "C:/Users/brian_000/Desktop/REnemy.png");
                }
     
                public void actionPerformed(ActionEvent e) {
                            checkCollisions();
                        System.out.println(p.x);
                        ArrayList bullets = Dude.getBullets();
                        for (int w = 0; w < bullets.size(); w++)
                        {
                                Bullet m = (Bullet) bullets.get(w);
                                if (m.getVisible() == true)
                                {
                                    m.move();
                        }
                                else
                                        bullets.remove(w);
                        }
     
                        p.move();
     
                        if (p.x > 400)
                                en.move(p.getdx(), p.getLeft());
                        if (p.x > 500)
                                en2.move(p.getdx(), p.getLeft());
     
     
                        repaint();
                }
     
     
     
                public void checkCollisions()
                {
                    Rectangle r1 = en.getBounds();
                    Rectangle r2 = en2.getBounds();
     
                     ArrayList bullets = Dude.getBullets();
                     for (int w = 0; w < bullets.size(); w++)
                     {
                             Bullet m = (Bullet) bullets.get(w);
                             Rectangle m1 = m.getBounds();
                             if (r1.intersects(m1) && en.Alive())
                             {
                                     en.isAlive = false;
                                     m.visible = false;
                             }
                             else if (r2.intersects(m1) && en2.Alive())
                             {
                                     en2.isAlive = false;
                                     m.visible = false;
                             }
                }
     
                     Rectangle d = p.getBounds();
                     if (d.intersects(r1) || d.intersects(r2))
                             lost = true;
     
                }
     
                public void paint(Graphics g) {
                    if (lost)
                        if (p.dy == 2 && done2 == false) {
                                done2 = true;
                                animator = new Thread(this);
                                animator.start();
                        }
     
                        super.paint(g);
                        Graphics2D g2d = (Graphics2D) g;
     
                        if ((p.getX() - 300) % 1884 == 0)// p.getX() == 590 || p.getX() == 2990)...
                                p.nx = 0;
                        if ((p.getX() - 1242) % 1884 == 0)// p.getX() == 1790 || p.getX() == 4190)...
                                p.nx2 = 0;
     
                        g2d.drawImage(img, 910 - p.getnX2(), 0, null);
                        if (p.getX() >= 300) {
                                g2d.drawImage(img, 910 - p.getnX(), 0, null);
                        }
                        g2d.drawImage(p.getImage(), p.left, v, null);
     
                        if (p.getdx() == -1) {
                                g2d.drawImage(img, 910 - p.getnX2(), 0, null);
                                g2d.drawImage(p.getImage(), p.left, v, null);
                        }
     
                        ArrayList bullets = Dude.getBullets();
                        for (int w = 0; w < bullets.size(); w++)
                        {
                                Bullet m = (Bullet) bullets.get(w);
                                g2d.drawImage(m.getImage(),m.getX(), m.getY(), null);
                        }
                        g2d.setFont(font);
                        g2d.setColor(Color.BLUE);
                        g2d.drawString("Ammo left: " + p.ammo, 700, 50);
                        if (p.x > 400)
                                if (en.Alive() == true)
                                        g2d.drawImage(en.getImage(), en.getX(), en.getY(), null);
                        if (p.x > 500)
                                if (en2.Alive() == true)
                                        g2d.drawImage(en2.getImage(), en2.getX(), en2.getY(), null);
                }
     
                private class AL extends KeyAdapter {
                        public void keyReleased(KeyEvent e) {
                                p.keyReleased(e);
                        }
     
                        public void keyPressed(KeyEvent e) {
                                p.keyPressed(e);
                        }
                }
     
                boolean h = false;
                boolean done = false;
     
                public void cycle() {
     
                        if (h == false)
                                v--;
                        if (v == 200)
                                h = true;
                        if (h == true && v <= 327) {
                                v++;
                                if (v == 327) {
                                        done = true;
                                }
                        }
                }
     
                public void run() {
     
                        long beforeTime, timeDiff, sleep;
     
                        beforeTime = System.currentTimeMillis();
     
                        while (done == false) {
     
                                cycle();
     
                                timeDiff = System.currentTimeMillis() - beforeTime;
                                sleep = 3 - timeDiff;
     
                                if (sleep < 0)
                                        sleep = 2;
                                try {
                                        Thread.sleep(sleep);
                                } catch (InterruptedException e) {
                                }
     
                                beforeTime = System.currentTimeMillis();
                        }
                        done = false;
                        h = false;
                        done2 = false;
                }
     
        }

    The Player class-
      package OurGame;
     
        import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.util.ArrayList;
     
     
    import javax.swing.ImageIcon;
     
        public class Dude {
                            int x, dx, y, nx, nx2, left, dy;
                Image mandude,mandudej,mandudeL,mandudeE;
     
                int ammo = 50;
     
                 static ArrayList bullets;//Holds number of bullets on screen
     
                ImageIcon s = new ImageIcon("C:/Users/brian_000/Desktop/mandude.png");
                ImageIcon j= new ImageIcon("C:/Users/brian_000/Desktop/mandudeJ.png");
                ImageIcon l = new ImageIcon("C:/Users/brian_000/Desktop/mandudeL.png");
                ImageIcon g = new ImageIcon("C:/Users/brian_000/Desktop/mandudeE.png");
     
     
                public Rectangle getBounds()
                {
                       return new Rectangle(left, y, 104, 120);
                }
     
                public static ArrayList getBullets()
                {
                        return bullets;
                }
                public Dude() {
                        x = 320;
                        left = 150;
                        nx = 0;
                        nx2= 910;
                        y = 327;
                        mandude = s.getImage();
                        bullets = new ArrayList();//j
     
                }
     
     
                public void fire()//Method to run when when fired
                {
     
                        System.out.println(bullets.size() + " " + ammo);
                        if (ammo > 0){
                                ammo--;
                        //Create a new bullet object and add it to
                        //array "list" of all bullets on screen.
                                Bullet z = new Bullet((left + 104), (Board.v + 60/2));              
                                bullets.add(z);}
                }
     
                public void move() {
                        if (dx >= -1){
                                if (left + dx <= 150)
                                        left+=dx;
     
                                else{
                        x = x + dx;
     
                        nx2= nx2 + dx;
                                nx = nx + dx;
     
                }}
                        else
                {
                        if (left + dx > 0)
                        left = left + dx;
                }
                        }
     
                public int getX() {
                        return x;
                }
     
                public int getnX() {
                        return nx;
                }
     
                public int getnX2() {
                        return nx2;
                }
     
                public int getdx() {
                        return dx;
                }
     
                public int getLeft() {
                        return left;
                }
                public Image getImage() {
                        return mandude;
                }
     
                public void keyPressed(KeyEvent e) {
                        int key = e.getKeyCode();
                        if (key == KeyEvent.VK_LEFT)
                        {               dx = -2;
                        mandude = l.getImage();        
                        }
     
                        if (key == KeyEvent.VK_RIGHT)
                                {dx = 2;
                        mandude = s.getImage();  
                                }
     
                        if (key == KeyEvent.VK_SPACE)
                        {
                             fire();
                             mandude = g.getImage();
                        }
     
                        if (key == KeyEvent.VK_UP)
                                {dy = 2;
                                mandude = j.getImage();
     
                                }                    }
     
                public void keyReleased(KeyEvent e) {
                        int key = e.getKeyCode();
     
                        if (key == KeyEvent.VK_LEFT)
                                dx = 0;
     
                        if (key == KeyEvent.VK_RIGHT)
                                dx = 0;
     
                        if (key == KeyEvent.VK_SPACE)
                            mandude = s.getImage();
     
                        if (key == KeyEvent.VK_UP)
                                {
                                    dy = 0;
                                mandude = s.getImage();
                                }
                            }
                }

    The Enemy class-
        package OurGame;
     
        import java.awt.*;
     
    import javax.swing.ImageIcon;
     
        public class Enemy {
     
                private static final String l = null;
                            Image img;
                int x, y;
                boolean isAlive = true;
     
                //Other attributes to consider:
                // int health = 100;???
                // int attackPower = 5;???
     
                public Enemy(int startX, int startY, String location)
                {
                        x = startX;
                        y = startY;
                        ImageIcon l = new ImageIcon(location);
                        img = l.getImage();
                }
     
                public int getX()
                {
                        return x;
                }
                public int getY()
                {
                        return y;
                }
                public boolean Alive()
                {
                        return isAlive;
                }
                public Image getImage()
                {
                        return img;
                }
     
                public void move(int dx, int left)            {
                    if (dx == 2 && !((left + dx )< 150))//Added this to only move enemy when character moves forward (not back)
                        x = x - dx;
                }
                public Rectangle getBounds()
                {
                       return new Rectangle(x, y, 140, 180);
                }
        }

    thanks if you can help
    Last edited by MiningManCoder; March 31st, 2014 at 06:07 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Jumping Issue

    Post the code you are having problems with here on the forum and ask your questions about it. Be sure to wrap the code in code tags.

    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Jumping Issue

    can you just copy and paste the links I gave to the address bar, its too much code on one page?

    EDIT: ok, i put the code in
    Last edited by MiningManCoder; March 31st, 2014 at 06:08 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Jumping Issue

    the character can't jump until he reaches the enemy
    Can you explain what the problem is?
    Is the character supposed to jump any time but is restricted to when it reaches the enemy?

    How can the code be executed for testing? I don't see a main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Jumping Issue

    yes the character is supposed to jump at any time but when when collision detection was added, something was switched up and now he can only jump after the enemies position.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Jumping Issue

    How can the code be executed for testing? I don't see a main() method.

    The code has no comments to describe its logic.
    Where is the section that is causing the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Jumping Issue

    I shouldn't have to show you the frame class, something in those sections of classes are messed up to cause the problem in the game.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Jumping Issue

    How can the code be tested then?

    Can you add some comments to the code saying what it is supposed to do and where it is supposed to do it?
    For example: why should the character jump? What tells it to jump? What controls when and where it jumps?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Jumping Ship
    By dalesgent in forum The Cafe
    Replies: 3
    Last Post: August 9th, 2013, 06:20 AM
  2. testing is player is jumping or not jumping. falling or not falling.
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 11th, 2012, 06:07 PM
  3. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  4. Frog jumping code (AP comp science help!)
    By CompScienceStudent1 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 21st, 2011, 05:42 PM
  5. Cant get my code to stop jumping
    By Mob31 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 07:03 AM