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

2020/6/3

Help with boomerang code

mshep mshep

2020/6/3

#
Hi Im trying to make an object move like a boomerang. If the boomerange is a set distance away from other actor I want it to move back. The thrown boolean is used in the actor so only when "thrown" is false the player can press a button to throw the boomerang. This doesn't work, and the boomerang just appears in front of the actor and doesn't move. Can I have help with this?
    
public void act(){//this is the code in "throwkirby" that creates the boomerang
        if(thrown==false&&Greenfoot.isKeyDown("space")){
            thrown=true;
            throwobject();
        }
    }
    public void throwobject(){
            setImage("throwing.png");
            getWorld().addObject(new kirbyobject(), getX()+100,getY());
    }
//this is a method in "kirbyobject" which is the boomerang that is supposed to add the boomerang effect
public void moveback(){
        boolean distanceFromKirby=false; 
        if(getX()==throwkirby.xPos+800){//if 800 units away from kirby, return true
            distanceFromKirby=true;
        }
        if (distanceFromKirby=false){//if not thrown  
            xSpeed++; // travels foward, with increasing speed
            setLocation(getX()+xSpeed, getY());
        }
        else
        { if(distanceFromKirby==true){//if  distance from throwkirby 800 ( i know this if statement isnt 
 //necessary, but i just put it to clarify)
                xSpeed = -15; // add travel back speed  
                setLocation(getX()+xSpeed, getY());
        }}
        setLocation(getX()+xSpeed, getY());

        if(getX() == throwkirby.xPos){//if boomerang at throwkirby Xpos, not thrown
        throwkirby.thrown = false;}
    }
danpost danpost

2020/6/3

#
Line 17 looks troublesome. No comparison is done there. It's just a (false) boolean value, as is.
You need to login to post a reply.