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

2017/10/3

RPG Collision

Yoran Yoran

2017/10/3

#
so, im trying to make undertale in greenfoot. what i have sofar is a startscreen, the first level, a walking character (he can walk in all 4 directions). but i'm struggeling with making the walls solid and some objects. how do i make it so i cant walk through the walls and some objects like a sign board or a rock?
Super_Hippo Super_Hippo

2017/10/3

#
Move your character. Check if it is touching an obstacle. If so, move it back.
Yoran Yoran

2017/10/3

#
Super_Hippo wrote...
Move your character. Check if it is touching an obstacle. If so, move it back.
i managed to get a horizontal collision but not a vertical one yet
1
2
if (getOneIntersectingObject(flowey.class) != null) move(-5);
  if (getOneIntersectingObject(flowey.class) != null) move(+10);
Super_Hippo Super_Hippo

2017/10/3

#
I am not sure why you have the second line. The way how you have to do it depends on how you move the character. You can move back to the position where it was before moving. You can also check horizontal and vertical movement independently.
Yoran Yoran

2017/10/3

#
Super_Hippo wrote...
I am not sure why you have the second line. The way how you have to do it depends on how you move the character. You can move back to the position where it was before moving. You can also check horizontal and vertical movement independently.
oh. well this is how i move my character
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public void MoveUp()
    {
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-4);
        }
    }  
    public void MoveDown()
    {  
       if(Greenfoot.isKeyDown("down"))
        {
           setLocation(getX(), getY()+4);
        }
    }   
 public void MoveRight()
{
    if(Greenfoot.isKeyDown("right"))
        {
           setLocation(getX()+4, getY());
        }
}
public void MoveLeft()
{   if(Greenfoot.isKeyDown("left"))
        {
           setLocation(getX()-4, getY());
        }
}
im not really sure how to move back the position. by the way, I put in 2 lines because i was not sure how to put it into 1 line
Super_Hippo Super_Hippo

2017/10/3

#
That's the easy way without checking horizontal and vertical movement individually.
1
2
3
4
5
6
7
8
9
10
11
12
13
int dx=0, dy=0;
if (Greenfoot.isKeyDown("right")) dx++;
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("up")) dy--;
if (Greenfoot.isKeyDown("down")) dy++;
if (dx != 0 || dy != 0)
{
    setLocation(getX()+4*dx, getY()+4*dy);
    if (isTouching(flowey.class))
    {
        setLocation(getX()-4*dx, getY()-4*dy);
    }
}
Yoran Yoran

2017/10/4

#
Super_Hippo wrote...
That's the easy way without checking horizontal and vertical movement individually.
1
2
3
4
5
6
7
8
9
10
11
12
13
int dx=0, dy=0;
if (Greenfoot.isKeyDown("right")) dx++;
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("up")) dy--;
if (Greenfoot.isKeyDown("down")) dy++;
if (dx != 0 || dy != 0)
{
    setLocation(getX()+4*dx, getY()+4*dy);
    if (isTouching(flowey.class))
    {
        setLocation(getX()-4*dx, getY()-4*dy);
    }
}
thanks Alot! this really helped me :D
You need to login to post a reply.