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: Prevent object going outside of screen?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Prevent object going outside of screen?

    I solved object going outside of screen by saying if the Y axis of paddle = top or bottom of screen then set the paddle's Y axis to the min/max so it can't go over.

    I have another query to do with sounds if anyone can help?

                    if ( LeftCollision == true ){
                        BallX_inc = 1*S; // reverse ball direction
                        String Filename = "hit.au";
                        InputStream in = new FileInputStream(Filename);
                        AudioStream as = new AudioStream(in);         
                        AudioPlayer.player.start(as);
                    }
                    if ( RightCollision == true ){
                        BallX_inc = -1*S; // reverse ball direction
                        String Filename = "hit.au";
                        InputStream in = new FileInputStream(Filename);
                        AudioStream as = new AudioStream(in);         
                        AudioPlayer.player.start(as);
                    }

    This code here plays a sound when the ball hits the paddle, however is there anyway I can shorten it or have the play sound as a function? Because as you see there is repeating data.

    I tried putting it into its own function however some error came up saying it needs a catch/try or something like that
    Last edited by JakkyD; May 7th, 2012 at 10:35 AM. Reason: Solved solution


  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Prevent object going outside of screen?

    As you can see here as well

                    // if the ball hits the left wall add to Player 2 score                
                    if ( BallXLoc <= 11 ){
                        Player2Score += 1;
                        BallXLoc = W/2;
                        BallYLoc = H/2;
                        String Filename = "miss.au";
                        InputStream in = new FileInputStream(Filename);
                        AudioStream as = new AudioStream(in);         
                        AudioPlayer.player.start(as);
                    }
     
                    // if the ball hits the right wall add to Player 1 score                
                    if ( BallXLoc >= W-11 ){
                        Player1Score += 1;
                        BallXLoc = W/2;
                        BallYLoc = H/2;
                        String Filename = "miss.au";
                        InputStream in = new FileInputStream(Filename);
                        AudioStream as = new AudioStream(in);         
                        AudioPlayer.player.start(as);
                    }

    the same sound code is repeating itself. How do I go about making this a function?

  3. #3
    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: Prevent object going outside of screen?

    You should not be reading the sound file in each time you play it. You only have to read it in once.

    Recommended reading: Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
    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. Replies: 1
    Last Post: May 5th, 2012, 05:00 PM
  2. testing weather one object is greater than another object
    By u-will-neva-no in forum Object Oriented Programming
    Replies: 1
    Last Post: February 19th, 2012, 07:02 PM
  3. Replies: 9
    Last Post: December 31st, 2011, 01:22 AM
  4. Replies: 3
    Last Post: December 21st, 2011, 03:46 PM
  5. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM