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

2016/6/14

Stop walking ffs

MrPancakes MrPancakes

2016/6/14

#
Im making a fake pokemon with a couple of friends of me, and my charachter wont stop moving at 2 certain objects. A tree and a house. I dont know how to make a code of that. Below here is the code.
public void act() 
    {
        if (Greenfoot.isKeyDown ("up") || Greenfoot.isKeyDown ("w"))
        {
            setLocation(getExactX(), getExactY()-moveSpeed);
            setImage("roodkapjeachter.gif");
            if (isTouching(Tree.class))
            {
                move(0);
            } 
        }
        if (Greenfoot.isKeyDown ("down") || Greenfoot.isKeyDown ("s"))
        {
            setLocation(getExactX(), getExactY()+moveSpeed);
            setImage("roodkapjevoor.gif");
            if (isTouching(Obstacle.class))
            {
                move(0);
            } 
        }    
        if (Greenfoot.isKeyDown ("left") || Greenfoot.isKeyDown ("a"))
        {
            setLocation(getExactX()-moveSpeed, getExactY());
            setImage("roodkapjelinks.gif");
            if (isTouching(Tree.class))
            {
                
            } 
        }    
        if (Greenfoot.isKeyDown ("right") || Greenfoot.isKeyDown ("d"))
        {
            setLocation(getExactX()+moveSpeed, getExactY());
            setImage("roodkapjerechts.gif"); 
            if (isTouching(Tree.class))
            {
                move(0);
            }    
        }
    }
I know that i should use the 'move(int)' code but it doesnt work for me somehow.
SPower SPower

2016/6/14

#
You do not need to use the move function. What you do not seem to understand is that "move(0)" does absolutely nothing. It says: "move myself forward 0 cells", which of course, will result in nothing happening. You could negate the changing of the location you've done previously, e.g.:
setLocation(getExactX(), getExactY() -moveSpeed);
// stuff
if (isTouching(Tree.class)) {
    setLocation(getExactX(), getExactY() +moveSpeed);
}
Or you could look at the size of the object being touched, and move the actor back the appropriate amount.
// stuff
if (isTouching(Tree.class)) {
    Actor tree = getOneIntersectingObject(Tree.class);
    int heightTree = tree.getImage().getHeight();
    setLocation(getExactX(), getExactY() +heightTree/2);
}
You'd have to change the positive/negative signs for the different directions (i.e., if you've previously added something to Y, you need to subtract from it to undo the movement), and add/subtract the amount on the x or y direction (also, with x, you'd need to use the width instead of the height of the object). And one simple note: the code I showed (at least, the second snippet) assumed you have a cell size of 1x1 pixel; if that's not the case, you need to divide the width/height of the image by getWorld().getCellSize().
MrPancakes MrPancakes

2016/6/14

#
I fixed it. Thank you. and sorry if I didnt understand it.
SPower SPower

2016/6/14

#
You don't need to apologise for not understanding something; I simply wanted to tell you where I think the issue was (at least partially).
MrPancakes MrPancakes

2016/6/20

#
I also have another problem. I want to change the world from one to another. I know how that works, but the problem is somewhere else. I want to change the world if my character touches something. Can you maybe help me?
danpost danpost

2016/6/20

#
MrPancakes wrote...
I also have another problem. I want to change the world from one to another. I know how that works, but the problem is somewhere else. I want to change the world if my character touches something. Can you maybe help me?
if (isTouching(WhateverActor.class)) Greenfoot.setWorld(new WhateverWorldClass());
MrPancakes MrPancakes

2016/6/20

#
@danpost Do I have to put in the world or in the actor editor?
MrPancakes MrPancakes

2016/6/20

#
oh nevermind. i figured it out. Thanks for the code ;)
You need to login to post a reply.