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

Thread: two animated sprites collide

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default two animated sprites collide

    Hi I need to edited this code so that when the ball and blackhole sprite collide the two sprites destroy each other (meaning they both disappear from the screen)

    import java.awt.*;
    import java.applet.*;
    import java.util.*;
    import java.awt.image.*;
    import java.net.*;
     
    public class AnimationClass extends Applet  implements Runnable {
        static int SCREENWIDTH = 640;
        static int SCREENHEIGHT = 480;
        Thread gameloop;
        Random rand = new Random();
     
        //double buffer objects
        BufferedImage backbuffer;
        Graphics2D g2d;
     
        Image background;
     
        //sprite variables
        AnimatedSprite ball;
        AnimatedSprite blackhole;
     
        private URL getURL(String filename) {
             URL url = null;
             try {
                 url = this.getClass().getResource(filename);
             }
             catch (Exception e) {}
             return url;
        }
     
        public void init() {
            //create the back buffer for smooth graphics
            backbuffer = new BufferedImage(SCREENWIDTH, SCREENHEIGHT,
                BufferedImage.TYPE_INT_RGB);
            g2d = backbuffer.createGraphics();
     
            //load the background image
            Toolkit tk = Toolkit.getDefaultToolkit();
            background = tk.getImage(getURL("woodgrain.png"));
     
            //load the ball animation strip
            ball = new AnimatedSprite(this, g2d);
            ball.load("xball.png", 8, 8, 64, 64);
            ball.setPosition(new Point2D(300,200));
            ball.setFrameDelay(1);
            ball.setVelocity(new Point2D(1,1));
            ball.setRotationRate(1.0);
     
            blackhole = new AnimatedSprite(this, g2d);
            blackhole.load("blackhole64x64x16.png", 8, 8, 64, 64);
            blackhole.setPosition(new Point2D(100,200));
            blackhole.setFrameDelay(2);
            blackhole.setVelocity(new Point2D(1,1));
            blackhole.setRotationRate(1.0);
     
        }
     
        public void start() {
            gameloop = new Thread(this);
            gameloop.start();
        }
     
        public void stop() {
            gameloop = null;
        }
     
        public void run() {
            Thread t = Thread.currentThread();
            while (t == gameloop) {
                try {
                    Thread.sleep(5);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                gameUpdate();
                repaint();
            }
        }
     
        public void gameUpdate() {
            //see if it's time to animate
            ball.updateAnimation();
            blackhole.updateAnimation();
     
            //update the ball position
            ball.updatePosition();
            if ((ball.position().X() < 0) || (ball.position().X() > SCREENWIDTH - 64)) {
                double x = ball.velocity().X() * -1;
                ball.setVelocity(new Point2D(x, ball.velocity().Y()));
                x = ball.position().X();
                ball.setPosition(new Point2D(x, ball.position().Y()));
            }
            if ((ball.position().Y() < 0) || (ball.position().Y() > SCREENHEIGHT - 64)) {
                double y = ball.velocity().Y() * -1;
                ball.setVelocity(new Point2D(ball.velocity().X(), y));
                y = ball.position().Y() + ball.velocity().Y();
                ball.setPosition(new Point2D(ball.position().X(), y));
            }
     
     
    			blackhole.updatePosition();
            if ((blackhole.position().X() < 0) || (blackhole.position().X() > SCREENWIDTH - 64)) {
    		            double x = blackhole.velocity().X() * -1;
    		            blackhole.setVelocity(new Point2D(x, blackhole.velocity().Y()));
    		            x = blackhole.position().X();
    		            blackhole.setPosition(new Point2D(x, blackhole.position().Y()));
    		        }
    		        if ((blackhole.position().Y() < 0) || (blackhole.position().Y() > SCREENHEIGHT - 64)) {
    		            double y = blackhole.velocity().Y() * -1;
    		            blackhole.setVelocity(new Point2D(blackhole.velocity().X(), y));
    		            y = blackhole.position().Y() + blackhole.velocity().Y();
    		            blackhole.setPosition(new Point2D(blackhole.position().X(), y));
            }
     
            //lastly, update the rotation
            ball.updateRotation();
            blackhole.updateRotation();
        }
     
        public void update(Graphics g) {
            //draw the background
            g2d.drawImage(background, 0, 0, SCREENWIDTH-1, SCREENHEIGHT-1, this);
     
            //draw the current frame of animation
            ball.draw();
            blackhole.draw();
     
            g2d.setColor(Color.BLACK);
            g2d.drawString("Position: " + ball.position().X() + "," +
                           ball.position().Y(), 5, 10);
            g2d.drawString("Velocity: " + ball.velocity().X() + "," +
                           ball.velocity().Y(), 5, 25);
            g2d.drawString("Animation: " + ball.currentFrame(), 5, 40);
     
     
     
            g2d.drawString("Position: " + blackhole.position().X() + "," +
    		                ball.position().Y(), 5,450 );
    		g2d.drawString("Velocity: " + blackhole.velocity().X() + "," +
    		                ball.velocity().Y(), 5, 465);
            g2d.drawString("Animation: " + blackhole.currentFrame(), 5, 480);
     
     
     
     
     
            paint(g);
        }
     
     
        public void paint(Graphics g) {
            //draw the back buffer to the screen
            g.drawImage(backbuffer, 0, 0, this);
        }
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: two animated sprites collide

    And what have you tried? Where are you stuck? What don't you understand?

    This isn't a codeservice, and we don't have the time (nor the desire) to do your work for you. Ask a specific question, post an SSCCE, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Sprites
    By saturisk in forum Java Theory & Questions
    Replies: 1
    Last Post: March 13th, 2011, 10:48 AM
  2. How to animate pictures using JCreator?
    By bil_Imma in forum AWT / Java Swing
    Replies: 6
    Last Post: February 13th, 2009, 07:00 AM