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

2013/11/4

AddObject by mouseclicking

1
2
danpost danpost

2013/11/4

#
You probably have the 'mousePressed' and 'mouseClicked' lines within an 'if' block (not including its own). These statements should be executed directly by the act method before even working with the shootDelay field.
Tioma7 Tioma7

2013/11/4

#
danpost wrote...
You probably have the 'mousePressed' and 'mouseClicked' lines within an 'if' block (not including its own). These statements should be executed directly by the act method before even working with the shootDelay field.
i deleted all delay field in last try
danpost danpost

2013/11/4

#
Please show the code you are using (showing the entire class may help). Oh. Please use the 'code' tag below the 'Post a reply' box to insert your code with.
Tioma7 Tioma7

2013/11/4

#
danpost wrote...
Please show the code you are using (showing the entire class may help). Oh. Please use the 'code' tag below the 'Post a reply' box to insert your code with.
public class Player extends Actor
{
    private int speed = 2;
    public static int health = 100;
    public static int angle;
    
    public void act() 
    {
        movement();
        rotation();
        shoot();
    }
    
    public void movement(){
        int x = 0;
        int y = 0;
        
        if(Greenfoot.isKeyDown("w")){
            y -= speed;
            health++;
        }
        
        if(Greenfoot.isKeyDown("s")){
            y += speed;
            health--;
        }
        
        if(Greenfoot.isKeyDown("a")){
            x -= speed;
        }
        
        if(Greenfoot.isKeyDown("d")){
            x += speed;
        }
        
        setLocation(getX() + x, getY());
        
        if(getOneIntersectingObject(Wall.class) != null){
            if(x > 0){
                setLocation(getX() - x - 2, getY());
            }
            else{
                setLocation(getX() - x + 2, getY());
            }
        }
        
        setLocation(getX(), getY() + y);
        
        if(getOneIntersectingObject(Wall.class) != null){
            if(y > 0){
                setLocation(getX(), getY() - y - 2);
            }
            else{
                setLocation(getX(), getY() - y + 2);
            }
        }
    }
    
    public void rotation(){
        if(Greenfoot.getMouseInfo() != null){
            turnTowards(Greenfoot.getMouseInfo().getX(),Greenfoot.getMouseInfo().getY());
        }
        
        angle = getRotation();
    }
    
    public void shoot(){
        int buttonsDown;
        if(Greenfoot.mousePressed(null)){
            buttonsDown++;
        }
        if(Greenfoot.mouseClicked(null)){
            buttonsDown--;
        }
        if(buttonsDown != 0){
            getWorld().addObject(new Bullet(),getX(),getY());
        }
    }
}
danpost danpost

2013/11/5

#
The main problem is that line 68 should be up with line 3. Where it is now, it will not retain a value between method calls. Outside the method, it will retain its value. I have tried to create code for what you want to do, but have not as yet been able to do so with full control of the value of the field. I think the problem has to do with the click counting within the MouseInfo class. The fact that there is not always a one-to-one ratio in the detection of presses and clicks. The best I have been able to come to so far is using the following:
private void shoot(){
    MouseInfo mi = Greenfoot.getMouseInfo();
    if (buttonsDown == 0 && Greenfoot.mousePressed(null) && mi.getButton() == 1) buttonsDown = 1;
    if (buttonsDown == 1 && Greenfoot.mouseClicked(null) && mi.getButton() == 1) buttonsDown = 0;
    if (buttonsDown == 1) getWorld().addObject(new Bullet(), getX(), getY());
}
It is glitchy in that bullets will stream when they should not; but, pressing and releasing a mouse button will restore it.
Tioma7 Tioma7

2013/11/5

#
danpost wrote...
The main problem is that line 68 should be up with line 3. Where it is now, it will not retain a value between method calls. Outside the method, it will retain its value. I have tried to create code for what you want to do, but have not as yet been able to do so with full control of the value of the field. I think the problem has to do with the click counting within the MouseInfo class. The fact that there is not always a one-to-one ratio in the detection of presses and clicks. The best I have been able to come to so far is using the following:
private void shoot(){
    MouseInfo mi = Greenfoot.getMouseInfo();
    if (buttonsDown == 0 && Greenfoot.mousePressed(null) && mi.getButton() == 1) buttonsDown = 1;
    if (buttonsDown == 1 && Greenfoot.mouseClicked(null) && mi.getButton() == 1) buttonsDown = 0;
    if (buttonsDown == 1) getWorld().addObject(new Bullet(), getX(), getY());
}
It is glitchy in that bullets will stream when they should not; but, pressing and releasing a mouse button will restore it.
they still shoot only 1 bullet, not a rapid fire
You need to login to post a reply.
1
2