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 3 of 3

Thread: Ayudame! :confused:

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [SOLVED] whats wrong with my if statement?

    Hi,

    I am trying to make a Chain Rxn clone and I have ran into an impass!

    Here's the original game: Chain Rxn by Zwigglers.

    So the problem that I have is with the hit detection. If you go to the contactIsMadeWith in the Dot class it should return true in the play method of the Grid class, but when I run the program the current dot immediately explodes despite the condition that should prevent it from exploding.

     
        public boolean contactIsMadeWith(Dot other) {
     
    Rectangle r1 = new Rectangle();
    r1.setBounds(this.myPoint.x, this.myPoint.y, sizeX, sizeY);
     
    Rectangle r2 = new Rectangle();
    r2.setBounds(other.myPoint.x, other.myPoint.y, other.sizeX, other.sizeY);
     
    return r1.intersects(r2);
     
        }

    So I know that contactIsMadeWith (above^^) is returning false yet in the code below it is returning true and exploding the dot when I don't want it to.

     
                        if (contact && !dotsOnScreen.get(i).hasExploded());
                        {
                            dotsOnScreen.get(i).explode();
     
                        }


    Please ask me any questions if I am not being clear!












    This is the Dot class code:

    /*
     * Alan Reeve
     *
     * dots are the main actor in this game
     * each dot has: sound, color, direction, movement, action, value, and time
     *
     */
    package grid;
     
    import java.awt.Color;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Random;
    import sun.audio.AudioData;
    import sun.audio.AudioDataStream;
    import sun.audio.AudioPlayer;
    import sun.audio.AudioStream;
     
    public class Dot {
     
        private int myDirection, myValue;
        public int sizeX, sizeY;
        private final int NE = 1;
        private final int SE = 2;
        private final int SW = 3;
        private final int NW = 4;
        private Color myColor;
        public Point myPoint;
        public boolean exploded = false;
        public boolean thereIsContact = false;
        private Timer timer = new Timer();
        private Random gen = new Random();
        private int[] dirs = {NE, SE, SW, NW};
        private Color[] colors = {new Color(60, 179, 113), new Color(64, 224, 208),
            new Color(244, 164, 96), new Color(132, 112, 255), new Color(238, 220, 130)};
        static AudioPlayer MGP = AudioPlayer.player;
        static AudioStream SFX, BGM;
        static AudioData MD, MD1;
        static AudioDataStream once = null;
     
        public Dot() {
            myDirection = dirs[gen.nextInt(4)];
            myPoint = new Point(gen.nextInt(600), gen.nextInt(400));
            myColor = colors[gen.nextInt(5)];
            sizeX = 5;
            sizeY = 5;
        }
     
        public Dot(int x, int y) {
            myPoint = new Point(x, y);
            sizeX = 40;
            sizeY = 40;
        }
     
        public void act() {
            if (timer.getSeconds() > 5) {
                timer.Stop();
                remove();
            }
        }
     
        public void explode() {
            System.out.println("wtf it was false");
            //timer.Start();
            sizeX = 40;
            sizeY = 40;
            exploded = true;
     
            for (int i = 0; i < colors.length; i++) {
                if (colors[i] == myColor) {
                    playSound("tone" + i);
                }
            }
        }
     
        public void move() {
     
    //        System.out.println("move begin");
            if (exploded == false) {
     
                if (myPoint.x <= 0 || myPoint.x >= (585) || myPoint.y <= 0 || myPoint.y >= (360)) {
                    turn();
                }
     
                switch (myDirection) {
                    case 1:
    //                System.out.println("northeast");
                        myPoint.x++;
                        myPoint.y--;
                        break;
                    case 2:
    //                System.out.println("southeast");
                        myPoint.x++;
                        myPoint.y++;
                        break;
                    case 3:
    //                System.out.println("southwest");
                        myPoint.x--;
                        myPoint.y++;
                        break;
                    case 4:
    //                System.out.println("northwest");
                        myPoint.x--;
                        myPoint.y--;
                        break;
                }
    //        System.out.println("move end");
            }
        }
     
        public void remove() {
            myPoint = null;
        }
     
        public Color getColor() {
            return myColor;
        }
     
        public int getWidth() {
            return sizeX;
        }
     
        public int getHeight() {
            return sizeY;
     
        }
     
        public int getX() {
            return myPoint.x;
        }
     
        public int getY() {
            return myPoint.y;
        }
     
        public boolean contactIsMadeWith(Dot other) {
     
    Rectangle r1 = new Rectangle();
    r1.setBounds(this.myPoint.x, this.myPoint.y, sizeX, sizeY);
     
    Rectangle r2 = new Rectangle();
    r2.setBounds(other.myPoint.x, other.myPoint.y, other.sizeX, other.sizeY);
     
    return r1.intersects(r2);
     
        }
     
        public boolean hasExploded() {
     
            System.out.println("EXPLODED: " + exploded);
     
            return exploded;
     
        }
     
    // -private methods-//
        private void turn() {
    //        System.out.println("turn begin");
     
            switch (myDirection) {
                case 1:
                    if (myPoint.y <= 0) {
                        myDirection = SE;
                    } else {
                        myDirection = NW;
                    }
                    break;
                case 2:
                    if (myPoint.x <= 585) {
                        myDirection = SW;
                    } else {
                        myDirection = NW;
                    }
                    break;
                case 3:
                    if (myPoint.x <= 0) {
                        myDirection = SE;
     
                    } else {
                        myDirection = NW;
                    }
                    break;
                case 4:
                    if (myPoint.y <= 0) {
                        myDirection = SW;
                    } else {
                        myDirection = NE;
                    }
                    break;
     
            }
     
     
        }
     
        private void playSound(String soundFile) {
     
            try {
     
                SFX = new AudioStream(new FileInputStream("src/sounds/" + soundFile + ".wav"));
                MD = SFX.getData();
                once = new AudioDataStream(MD);
            } catch (IOException error) {
                System.out.println("File not found!");
            }
     
            MGP.start(once);
            System.out.println("play sound " + soundFile);
     
        }
    }


    This the Grid class code:

    /*
     * Alan Reeve
     *
     * Create a jpanel that acts as the grid
     *
     */
    package grid;
     
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.*;
    import javax.swing.*;
     
    public class Grid extends JPanel {
     
        public int width = 600;
        public int height = 400;
        private int mouseX, mouseY;
        public int diffScreenX, diffScreenY;
        private int triX = 500;
        private int triN = 520;
        private int tri2X = 525;
        private int tri2N = 545;
        private ArrayList<Dot> dotsOnScreen = new ArrayList<Dot>();
        private boolean[] dotsExploded;
        public int numDots = 1;
        private boolean threadSuspended, userPressed;
        private boolean contact = false;
        private boolean notYetPressed = true;
        private boolean firstLevel = true;
        private boolean startScreen = true;
        private boolean goingRight = false;
        private Dot userDot;
     
        public Grid() {
        }
     
        public void init() {
            System.out.println("init begin");
     
     
     
     
     
            addMouseListener(new MouseListener() {
     
                public void mousePressed(MouseEvent e) {
                    if (!startScreen) {
     
                        if (notYetPressed == true) {
                            mouseX = e.getX();
                            mouseY = e.getY();
                            userDot = new Dot(mouseX, mouseY);
                            userPressed = true;
                            notYetPressed = false;
                            repaint();
                        }
                    }
                    if (startScreen) {
                        startScreen = false;
                        repaint();
                    }
                }
     
                public void mouseClicked(MouseEvent e) {
                }
     
                public void mouseReleased(MouseEvent e) {
                }
     
                public void mouseEntered(MouseEvent e) {
                }
     
                public void mouseExited(MouseEvent e) {
                }
            });
     
     
            for (int i = 0; i < numDots; i++) {
                dotsOnScreen.add(i, new Dot());
     
     
            }
     
            play();
     
            System.out.println("init end");
        }
     
        public void stop() {
            threadSuspended = true;
        }
     
        @SuppressWarnings("empty-statement")
        public void play() {
            System.out.println("play begin");
            while (true) {
     
     
                //------------------------------------------------------------------------//
                if (startScreen) {
                    if (goingRight) {
                        triX++;
                        triN++;
                        tri2X++;
                        tri2N++;
     
                        if (triX >= 510) {
                            goingRight = false;
     
                        }
                    } else if (!goingRight) {
     
                        triX--;
                        triN--;
                        tri2X--;
                        tri2N--;
     
                        if (triX <= 500) {
                            goingRight = true;
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                ;
                            }
                        }
                    }
                }
                //------------------------------------------------------------------------//
                for (int i = 0; i < dotsOnScreen.size(); i++) {
     
                    if (notYetPressed == false) {
                        contact = dotsOnScreen.get(i).contactIsMadeWith(userDot);
     
                        if (contact && !dotsOnScreen.get(i).hasExploded());
                        {
                            dotsOnScreen.get(i).explode();
     
                        }
                    }
                    dotsOnScreen.get(i).move();
                }
     
     
                //------------------------------------------------------------------------//
     
                repaint();
     
     
     
     
                try {
                    Thread.sleep(15);
                } catch (InterruptedException e) {
                    ;
                }
            }
        }
     
    //    public boolean contactMADE(int index) {
    //        System.out.println("CONTACT ::::::::::: " + dotsOnScreen.get(index).contactIsMadeWith(userDot));
    //
    //        return dotsOnScreen.get(index).contactIsMadeWith(userDot);
    //
    //
    //
    //    }
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
     
            //------------------------------------------------------------------------//
            if (startScreen) {
                g.setColor(Color.white);
                g.fillOval(70, 90, 60, 60);
     
                g.setColor(new Color(132, 112, 255));
                g.fillOval(50, 115, 50, 50);
     
                g.setColor(Color.yellow);
                g.fillOval(85, 135, 35, 35);
     
                g.setColor(Color.white);
                Font font = new Font("Arial", Font.BOLD, 70);
                g.setFont(font);
                g.drawString("Chain Rxn", 140, 200);
     
                Polygon tri = new Polygon();
                tri.addPoint(triX, 160);
                tri.addPoint(triX, 200);
                tri.addPoint(triN, 180);
                Polygon tri2 = new Polygon();
                tri2.addPoint(tri2X, 160);
                tri2.addPoint(tri2X, 200);
                tri2.addPoint(tri2N, 180);
                g.fillPolygon(tri);
                g.fillPolygon(tri2);
            }
            //------------------------------------------------------------------------//
            if (!startScreen) {
                if (firstLevel) {
                    g.setFont(null);
                    g.setColor(Color.white);
                    g.drawString("Explode 1 ball", 10, 20);
                }
     
     
                for (int i = 0; i < dotsOnScreen.size(); i++) {
                    g.setColor(dotsOnScreen.get(i).getColor());
                    g.fillOval(dotsOnScreen.get(i).myPoint.x, dotsOnScreen.get(i).myPoint.y, dotsOnScreen.get(i).getWidth(), dotsOnScreen.get(i).getHeight());
                }
                // following mouse
                if (notYetPressed == true) {
                    g.setColor(Color.gray);
                    g.fillOval(MouseInfo.getPointerInfo().getLocation().x - diffScreenX, MouseInfo.getPointerInfo().getLocation().y - diffScreenY, 40, 40);
                }
                // when mosue is pressed
                if (userPressed) {
                    g.setColor(Color.gray);
                    g.fillOval(mouseX - 20, mouseY - 20, 40, 40);
                }
            }
        }
    }
    Last edited by wasaki; May 26th, 2010 at 10:12 AM.


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Ayudame! :confused:

    Your 'if' statement isn't doing what you think it is - the explode method will always get called because it isn't inside the 'if' block (there's a stray semicolon):
     if (contact && !dotsOnScreen.get(i).hasExploded())[B][COLOR="Red"];[/COLOR][/B]
                        {
                            dotsOnScreen.get(i).explode();
    This is one reason I prefer the opening brace of the block to be at the end of the controlling statement line:
    if (contact && !dotsOnScreen.get(i).hasExploded()) {
        dotsOnScreen.get(i).explode();
    }

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ayudame!

    Wow...... haha I can't believe it was just a stray semicolon, after all this time. Thankyou dlorde!

Similar Threads

  1. Confusion about Java development IDES
    By neo_2010 in forum The Cafe
    Replies: 4
    Last Post: July 7th, 2009, 03:14 PM
  2. [SOLVED] Prime number generator program in Java
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 27th, 2009, 12:08 PM
  3. Replies: 3
    Last Post: February 26th, 2009, 02:04 AM
  4. Confusion with C/C++
    By Eric in forum Java Applets
    Replies: 0
    Last Post: December 22nd, 2008, 01:18 PM