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: shooting fire ball 2d

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default shooting fire ball 2d

    i trying to make my player shoot fire ball when ever it user hit space bar.

    but the problem is that
    i have 3 class
    Main.java
    Player.java
    Shoot.java


    ----------------------Main.java
    In my Main.java class i have run method where i am calling "PLaYER_SHOOT_MOVE" method from Shoot.java class
    public void run()
    while(true){
        shoot_class.PLAYER_SHOOT_MOVE(this, player_class, shoot_class);  //player shoot bullet moves
        }
    }


    than i am setting up method when user hit space it will run a method
    [B]if(keys == KeyEvent.VK_SPACE){
       shoot_class.hitSHOOT(); 
    }[/B]



    --------------------------Shoot.java
    private boolean hitSPACE = false;  //user hit space bar
    private boolean dead = true;       //no bullet on screen (bullet is dead)
    ...

    hitSHOOT method - which is getting called in Main.java
    if user hit space bar than set hitSPACE to tru
    public void hitSHOOT()  //user hit spacebar{
           hitSPACE = true;
    }


    move the bullet forward
    public void PLAYER_SHOOT_MOVE(Main m, Player p, Shoot s){	
           x =  p.getX();
           y =  p.getY();
     
    		if(hitSPACE == true)
    		{
    			x += dx;
    		}	
    }


    //draw image if user hit spavebar and there are no bullet on screen
    public void paint(Graphics g, Player p) 
    	{
     
    		if(hitSPACE == true && dead == true)
    		{
    			System.out.println(x);
    			g.drawImage(this.getSIMAGE(), x, y, width, height, null); 
    		}
    	}


    problem is that:
    when i set space bar it show firball but it doesnt go forward.
    i belive problem is in thiese statment
    public void PLAYER_SHOOT_MOVE(Main m, Player p, Shoot s){	
           x =  p.getX();
           y =  p.getY();

    i think i should some how move getX() at outside of method so it change x every time player move.


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

    Default Re: shooting fire ball 2d

    I recommend putting together an SSCCE that demonstrates exactly what you're doing.

    And you should probably use a Timer instead of that infinite loop.
    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!

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: shooting fire ball 2d

    In addition to what Kevin said

    public void run()
    while(true){
    shoot_class.PLAYER_SHOOT_MOVE(this, player_class, shoot_class); //player shoot bullet moves
    }
    }
    if this is the loop that keeps the game in motion then, this method gets called, (over and over at as fast as the computer can, which is a bad idea):
    public void PLAYER_SHOOT_MOVE(Main m, Player p, Shoot s){
    x = p.getX();
    y = p.getY();

    if(hitSPACE == true)
    {
    x += dx;
    }
    }
    I think am following what you are trying to show in these unorganized snippets. But it is difficult to follow. Next time just post the entire thing in order so it is much easier to follow.


    public void PLAYER_SHOOT_MOVE(Main m, Player p, Shoot s){
    x = p.getX();
    y = p.getY();
    Is x and y some global variable? What do they mean? Things like this make it hard, if not impossible, to help you, but with the whole code posted we can look at what we need to see. This is the reason why you should post a working example.

  4. The Following User Says Thank You to jps For This Useful Post:

    hwoarang69 (October 23rd, 2012)

Similar Threads

  1. How to connect and fire command to SSH server using Gyanmed SSH2 API.
    By Shanul in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 4th, 2012, 08:57 AM
  2. Has the ball hit my paddle?
    By JakkyD in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 3rd, 2012, 02:16 PM
  3. [SOLVED] Simple Spaceship Shooting Game
    By bj12marion in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 20th, 2012, 09:17 AM
  4. 2D Ball Animation
    By Deidelus in forum AWT / Java Swing
    Replies: 6
    Last Post: November 20th, 2011, 05:07 PM
  5. Need help with a third ball in game.
    By vlan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 12th, 2010, 04:35 PM