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

2019/5/31

Movement issues

BilalSLSS BilalSLSS

2019/5/31

#
basically if my character turns his back against a wall it just makes him walk out of the world unless i click another movement key which stops but i cannot figure out how to make him stop moving back if his back is against the wall. please help
import greenfoot.*;
public class Zee extends Actor {
    private int timer;
    public static boolean stabbing = false;
    public boolean d = true;
    boolean death = false; 
    
    //Level Progresser 
    private Door door;
    public Zee(Door door) {
        this.door = door;
    }
    
    //Character
    public Zee() {
        GreenfootImage image = getImage();
        image.scale(80, 80);
    }
    
    public void act() {
        //Controls
        if (Greenfoot.isKeyDown("left") || (Greenfoot.isKeyDown("a"))){
            setRotation(180);
            move(3);
        } else if (Greenfoot.isKeyDown("up") || (Greenfoot.isKeyDown("w"))){
            setRotation(-90);
            move(3);
        } else if (Greenfoot.isKeyDown("right") || (Greenfoot.isKeyDown("d"))){
            setRotation(360);
            move(3);
        } else if (Greenfoot.isKeyDown("down") || (Greenfoot.isKeyDown("s"))) {
            setRotation(90);
            move(3);
        }
        
        //Stabing
        if (Greenfoot.isKeyDown("e") || (Greenfoot.isKeyDown("shift"))){
            setImage("zeestab4.png");
            stabbing = true;
            timer = 6;
        }
        if (timer > 0 && --timer == 0) setImage("newzee.png"); 
        
        //Restrictions and More
        if (isTouching(Wall.class)) {
            move(-3);
        }
        if (isTouching(Coins.class)) {
            removeTouching(Coins.class);
            int score = 1;
        }
        if (isTouching(Emerald.class)) {
            removeTouching(Emerald.class);
            getWorld().removeObject(this.door);   
        }
        if (isTouching (Door.class)) {
            getWorld().removeObject(getOneIntersectingObject(Zee.class));
            death = true;
        }
    }
    
    //Stabbing Animation
    public void Stab() {
        GreenfootImage zeestab2 = getImage();
        zeestab2.scale(80, 80);
    }
}
danpost danpost

2019/6/1

#
I cannot find anything that would cause what you suggest. Let us rid the class of anything not needed or used. Remove lines 4 thru 6, line 39, line 58 and lines 61 thru 66. Now fix line 57 to this:
getWorld().removeObject(this);
To explain, the stabbing field was removed because "if (timer > 0)" basically asks if the actor is stabbing. Also, the stabbing field should not be a static field as each instance of the class will stab at its own time. I would understand a static stabbing field if you had a bunch of Zee objects all doing synchronized stabbing together, lol. The death field is not needed either because a check for the actor not being in the world can suffice for that. Try scaling the images to an odd number (79, 79) or (81, 81). Maybe the 1-pixel difference along an edge is causing unwanted intersecting with the wall when changing directions.
You need to login to post a reply.