This site requires JavaScript, please enable it in your browser!
Greenfoot back
hugtvy
hugtvy wrote ...

2014/5/23

Help with firing

hugtvy hugtvy

2014/5/23

#
I need help with a game ive made, basically i need to make it so that when i hold down the mouse i fire except i cant figure it out aha, any help? heres my code:
public class AssaultClass extends Character
{
    
    private int counter = 0;
    private int counter1 = 0;
    private int counter2 = 10;
    private int shoot1 = 0;
    private int health = 60;
    private int health1 = 0;
    private int reload = 0;
    
    
    /**
     * Act - do whatever the Survivor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    

    public void act() 
    {
        shoot();
        reload();
        move();
        Menu();
        
        Helper();
        Healer();
        shooter();
        
        HealthKit();
        ZombieHit();
        SurvivorZombieHit();
        checkHealth();
        onGround();
        
        MouseInfo m = Greenfoot.getMouseInfo();  
            if(m != null)
            turnTowards(m.getX(), m.getY());
       
             
    }
    
    private void bumpCounter2(int amount)
    {
        ZombieLand world;
        world = (ZombieLand)getWorld();
        Counter2 counter;
        counter = (Counter2)world.getObjects(Counter2.class).get(0);
        counter.add(amount);
    }
            
            
    public void shoot()
    {
        counter1++;
        if (Greenfoot.mousePressed(null) && (shoot1 == 0) && (counter1 > 5) )
        {
          
           getWorld().addObject(new Bullet(getRotation()), getX(), getY());
           reload++;
           bumpCounter2(-1);
           counter1 = 0
        }
                
    }
    
    
    
    
    public void reload()
    {
        if (reload == 30)
    {
       shoot1++;
       getWorld().addObject(new Reloading(), 91, 785);
    }
        if (shoot1 == 300)
    {
        getWorld().removeObjects(getWorld().getObjects(Reloading.class));
        shoot1 = 0;
        reload = 0;
        bumpCounter2(30);
    }
        }
danpost danpost

2014/5/23

#
What is the problem with this code. Are no shots at all being fired? or just one? or is there a lot of lag (which I would think there might be)? I say that because it would appear that you will have 300 Reloading objects in the world before reloading is completed (see line 75). 'reload' will have the value of '30' for all values of 'shoot1' from '0' to '300' before it is turned back to zero.
hugtvy hugtvy

2014/5/23

#
no dont worry about that i just made it so that the Reloading class deletes itself as soon as it spawns so that there are only a maximum of 2 in the game at one time. yer the gun only fires once, i need it to fire continuesly to a maximum of 30 times before it has to "reload" the reload function works it just shoots one at a time, cheers for replying so quick btw
danpost danpost

2014/5/23

#
I think it may be because you are using 'mousePressed'. I bet if you press the mouse button continuously, that every sixth (I think) time, a shot will be fired. Try adding '|| Greenfoot.mouseDragged(null)' to that condition:
if ( (Greenfoot.mousePressed(null) || Greenfoot.mouseDragged(null)) && shoot1 == 0 && counter1 > 5)
hugtvy hugtvy

2014/5/23

#
can see where your coming there and strangely it still hasnt worked, now it just lags out when firing and only fires on some clicks, not sure. any other ideas?
danpost danpost

2014/5/23

#
Yeah. Add a boolean field to track mouse button release.
// add boolean field
private boolean mouseDown;
// in 'shoot' method
if (mouseDown && Greenfoot.mouseClicked(null)) mouseDown = false;
if ((mouseDown || (Greenfoot.mousePressed(null)) && shoot1 = 0 && counter1 > 5)
{
    // etc.
    mouseDown = true;
}
hugtvy hugtvy

2014/5/23

#
like this?
  public void shoot()
    {
        counter1++;
        if (mouseDown && Greenfoot.mouseClicked(null)) mouseDown = false;
        if ((mouseDown || (Greenfoot.mousePressed(null)) && shoot1 = 0 && counter1 > 5)
        {
          getWorld().addObject(new Bullet(getRotation()), getX(), getY());
          reload++;
          bumpCounter2(-1);
          counter1 = 0;
          mouseDown = true;
        }    
                
    }
danpost danpost

2014/5/23

#
Like that.
hugtvy hugtvy

2014/5/23

#
you forgot the second = after shoot1 on line 5:
public void shoot()
    {
        counter1++;
        if (mouseDown && Greenfoot.mouseClicked(null)) mouseDown = false;
        if ((mouseDown || (Greenfoot.mousePressed(null)) && (shoot1 ==0 ) && (counter1 > 5)))
        {
          getWorld().addObject(new Bullet(getRotation()), getX(), getY());
          reload++;
          bumpCounter2(-1);
          counter1 = 0;
          mouseDown = true;
        }    
                
    }
thats what ive written except now it only shoots when i click not hold and it doesnt obey the reload factor: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class Survivor here. * * @author (your name) * @version (a version number or a date) */ public class AssaultClass extends Character { private int counter = 0; private int counter1 = 0; private int counter2 = 10; private int shoot1 = 0; private int health = 60; private int health1 = 0; private int reload = 0; private boolean mouseDown; /** * Act - do whatever the Survivor wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.mousePressed(null))shoot(); reload(); move(); Menu(); Helper(); Healer(); shooter(); HealthKit(); ZombieHit(); SurvivorZombieHit(); checkHealth(); onGround(); MouseInfo m = Greenfoot.getMouseInfo(); if(m != null) turnTowards(m.getX(), m.getY()); } private void bumpCounter2(int amount) { ZombieLand world; world = (ZombieLand)getWorld(); Counter2 counter; counter = (Counter2)world.getObjects(Counter2.class).get(0); counter.add(amount); } public void shoot() { counter1++; if (mouseDown && Greenfoot.mouseClicked(null)) mouseDown = false; if ((mouseDown || (Greenfoot.mousePressed(null)) && (shoot1 ==0 ) && (counter1 > 5))) { getWorld().addObject(new Bullet(getRotation()), getX(), getY()); reload++; bumpCounter2(-1); counter1 = 0; mouseDown = true; } } public void reload() { if (reload == 30) { shoot1++; getWorld().addObject(new Reloading(), 91, 785); } if (shoot1 == 300) { getWorld().removeObjects(getWorld().getObjects(Reloading.class)); shoot1 = 0; reload = 0; bumpCounter2(30); } }
danpost danpost

2014/5/23

#
Change the first line in your act method back to 'shoot();' (I do not know why you changed that!)
hugtvy hugtvy

2014/5/23

#
yer that makes sense, me niether think i was looking at another turtorial and forgot to change it back, thats great except i can only do it once.. once i let go it no longer works when i reclick?? also it shoots way to fast i tried changing the (counter > X) but it doesnt effect it aha
danpost danpost

2014/5/23

#
Change the beginning of your 'reload' method to this:
public void reload()
{
    if (reload == 30)
    {
       if (shoot1 == 0) getWorld().addObject(new Reloading(), 91, 785);
       shoot1++;
    }
    // etc.
I am not saying that will fix everything (it will fix a lag problem).
danpost danpost

2014/5/24

#
Try fixing this line again (I had one two many parenthesis and you incorrectly balanced it).
if ((mouseDown || Greenfoot.mousePressed(null)) && shoot1 = 0 && counter1 > 5)
Actually, I revised the beginning of the 'shoot' method to this:
public void shoot()
{
    if (counter1 < 6) counter1++;
    if (mouseDown && Greenfoot.mouseClicked(null)) mouseDown = false;
    if (!mouseDown && Greenfoot.mousePressed(null)) mouseDown = true;
    if (mouseDown && shoot1 == 0 && counter1 == 6)
    {
        reload++;
        // etc.
}
Also, I changed the value that 'counter2' starts at (when declared near the top of the class) to '30'.
hugtvy hugtvy

2014/5/24

#
alright cheers, that means to have fixed it! just one last thing, ive got a menu at the bottom of my game that includes game info like how many bullets left in your gun, the gun ur using etc. but the character is not allowed to go into the menu, ive got this code but it always glitches, do you know of another?
public boolean onGround()
   {
       int spriteHeight = getImage().getHeight();
       int yDistance = (int) (spriteHeight / 2 + 5);
            
       Actor ground = getOneObjectAtOffset( 0, getImage().getHeight()/2, Menu.class);
        if (ground == null)
        {
             return false;
        }
        else
        { 
             moveToGround(ground);
             return true;
        }
            
   }
       
   public void moveToGround(Actor ground)
   {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
            
        setLocation(getX(), newY);
   }
danpost danpost

2014/5/24

#
hugtvy wrote...
ive got a menu at the bottom of my game that includes game info like how many bullets left in your gun, the gun ur using etc. but the character is not allowed to go into the menu, ive got this code but it always glitches, do you know of another? < Code Omitted >
How is it glitching? How are you moving your sprite (include act method code, methods used in movement and how you declare any fields used in the movement; if using a support super class, its code may be relevant also)? Every little bit of information you can give that could possibly help should be given. This helps in eliminating a lot of the back-n-forth communications and resolves your issues at a much faster turn-over rate.
You need to login to post a reply.