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

2014/10/9

Character won't jump properly to the left

sunjay001 sunjay001

2014/10/9

#
Can someone help me please with this problem? I've made a character which can shoot bullets when pressing the spacebar. it can jump to the right (right+up) and shoot at the same time, but it wont shoot when I jump to the left (left+up) If you need more code, please ask me.
    public void move()
    {
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX() + speed, getY());
            facingLeft = false;
            animation();
        } else if (Greenfoot.isKeyDown("left")) 
        {  
            setLocation(getX()- speed, getY());
            facingLeft = true;
            animation();
        }
        if(Greenfoot.isKeyDown("up") && isJumping == false) 
        {
            vSpeed = jumpStrength;
            fall();
        }
    }
public void shoot() {
        // player looks to the left
        if (facingLeft == true) {
            if (Greenfoot.isKeyDown("space")) {
                getWorld().addObject(new Bullet("a"), getX(), getY());
            }
        } 

        // player looks to the right
        if (facingLeft == false) {
            if (Greenfoot.isKeyDown("space")) {
                getWorld().addObject(new Bullet("b"), getX(), getY());
            }
        } 
    }
To make the shooting code understandable, when the player is looking to the right, the bullets should travel to the right and when facing to the left, i made it so it should
danpost danpost

2014/10/9

#
davmac wrote...
... keyboards can only handle a limited number of keys pressed at the same time. After a certain point, they won't register new keypresses. Certain key combinations will work, and others won't. Better keyboards can handle a larger number.
I quoted the above from another discussion thread about the same type issue.
davmac davmac

2014/10/9

#
It's probably a hardware limitation of your keyboard (not recognizing up, left, and space at the same time). Try using "shift" for shooting instead. Btw the conventional way to write "facingLeft == true" is just "facingLeft", i.e:
    if (facingLeft) {  
Likewise for "facingLeft == false", just:
    if (! facingLeft) {  
You could also combine these:
        if (facingLeft) {  
            if (Greenfoot.isKeyDown("shift")) {  
                getWorld().addObject(new Bullet("a"), getX(), getY());  
            }  
        }   
        else {  
            // player looks to the right  
            if (Greenfoot.isKeyDown("shift")) {  
                getWorld().addObject(new Bullet("b"), getX(), getY());  
            }  
        }   
Furthermore I suspect your Bullet doesn't really a String parameter and could take a boolean instead. Then you could have just one case for shooting in either direction.
sunjay001 sunjay001

2014/10/9

#
It is indeed a hardware limitation, shift worked, thanks! @davmac
You need to login to post a reply.