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

Thread: how to create bullets in java

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default how to create bullets in java

    i created a player and i want him to shoot bullet. i understand the logic but i need some help with it. my bullet will just be rect in paint method.

    logic:
    create bullet class
    store in arraylist
    print in paint method

    in bullet class i set the variables:
    Bullet.java
    x                                     //x postion
    y                                     //y postion
    width                                //width of bullet
    height = 10;                      //height of bullet
    dx = 5;                             //speed of bullet
    boolean hitSpace = false;  //if user hit space buttom
    boolean visable = false;     //cant see the bullet 
    int bullet_limit = 50;          //set bullet limit. so after 50 no more bullets
    int bullet_range = 200;     //how far can bullet go
    private ArrayList<Bullet> store_bullets = new ArrayList<Bullet>();  //store bullets


    //if user hit space bar than set variables (calling this method in key method in main.)
    public void hitSPACE()
    	{
    		hitSpace = true;
    		dead = false;
    	}
     
     
     
    	public void MOVE()
    	{
    		if(hitSpace == true)                      //if user hit space bar
    		{
    			Bullet  b = new shoot(x, y);   //create bullet
    			store_bullets.add(b);           //store in arraylist
     
    			x+= dx;        //change speed
     
    			if(x > bullet_range)     //if it goes above range than dont viable
    			{
    				visable = false;  //cant see the bullet
    			}
    		}
    	}
     
     
    //draw bullets from arraylist
    public void paint(Graphics g)
    	{
    		if(visable == true)     //if its visable                    
    		{
    			for(int w = 0; w < store_bullets.size(); w++)// print from array list
    			{
    				g.fillRect(x, y, width, height);
    			}
    		}
    	}//end of paint method





    this is the code where i need help. how to print bullets from arraylist.
    for(int w = 0; w < store_bullets.size(); w++)       // print from array list
    			{
    				g.fillRect(x, y, width, height);
    			}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: how to create bullets in java

    If x,y, width,height in the fillRect() method are the locations of the bullet, the code needs to get those values from the Bullet objects in the arraylist.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: how to create bullets in java

    i printed bullet arraylist in player.class

    for(int i = 0; i < store_bullets.size(); i++)
    	{
    	g.drawImage(shoot_class.getImage(), bullet_class.getX(), bullet_class.getY(), 
    bullet_class.getWIDTH(), bullet_class.getHEIGHT(), null);
    	}

    now the problem is that when i hit space bullet shows up but it is taking player x, and y postion. and it move with my player. so if i move my player right than bullet moves right.

    but my public void SHOOT_MOVE() not seem to be working right. i added a println statment in that function and it does print. so it is going in that function but not moving the buttlet right.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: how to create bullets in java

    Sounds like you have some logic problems. You'll have to post code that compiles, executes and shows the problem.

    Also try debugging the code by adding println() statements to show the values of variables as they are used and changed to see what the computer sees so you can understand what the program is doing and why it is doing it.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: how to create bullets in java

    i think i found the problem. in player class
    bc SHOOT_MOVE() is in if statment. its only going to add dx to bullet x postion only onces. and if i hit space bar again than it will create a new bullet and add dx only onces. if that make scene?


    public void PLAYER_KEYS(main m, levels level_class)
    {
    ...
    if(space)
    {
         bullet_class = new shoot(x, y, m);
         store_bullets.add(shoot_class);
         bullet_class.SHOOT_MOVE();
    }
    ...
    }

    so its adding bullet adding dx onces. than createing another bullet adding dx onces. so all bulletx are on top of each other.

Similar Threads

  1. Create my own Java installation.
    By JosPhantasmE in forum Java Theory & Questions
    Replies: 3
    Last Post: March 7th, 2013, 05:52 PM
  2. java game bullets hit player 1 but not player 2
    By ajakking789 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 22nd, 2011, 08:19 AM
  3. can i create the project in java ?
    By naved in forum Java Theory & Questions
    Replies: 1
    Last Post: June 29th, 2011, 08:43 AM
  4. create file in java
    By pokuri in forum Object Oriented Programming
    Replies: 2
    Last Post: January 19th, 2011, 10:42 AM
  5. How I can create those shapes with Java?
    By Learner in forum AWT / Java Swing
    Replies: 3
    Last Post: November 18th, 2010, 02:10 AM