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

2017/1/23

Preventing collision of player with other classes

wym wym

2017/1/23

#
I'm making a top perspective shooter game and I'm trying to get my player class to not collide with the trees in game but it won't let me compile the Tree class now. This is what I wrote.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
 
/**
 * Write a description of class Tree here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Tree extends Design
{
    public Tree()
    {
        /* GreenfootImage image = getImage();
        image.scale(image.getWidth()/4, image.getHeight()/4 );
        setImage(image);*/
    }
 
    /**
     * Act - do whatever the Tree wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        noColl();
    }  
    public void noColl()
    {
        List players = getObjectsInRange(1000, Player.class);
        if (! players.isEmpty())
        {
            Actor player = (Actor) players.get(0);
            if (Greenfoot.isKeyDown("d"))
            {
                if (player.getX() == getX()-Player.hspeed)
                {
                    while(player.getX() != getX()-1)
                    {
                        player.move(1);
                    }
                    Player.hspeed=0;
                }
            }
            if (Greenfoot.isKeyDown("a"))
            {
                if (player.getX() == getX()+Player.hspeed )
                {
                    while(player.getX() != getX()+1)
                    {
                        player.move(1);
                    }
                    Player.hspeed=0;
                }
            }
        }
        if (! players.isEmpty())
        {
            Actor player = (Actor) players.get(0);
            if (Greenfoot.isKeyDown("w"))
            {
                if (player.getY() == getY()+Player.speed)
                {
                    while(player.getY() != getY()+1)
                    {
                        player.move(1);
                    }
                    Player.vspeed=0;
                }
            }
            if (Greenfoot.isKeyDown("s"))
            {
                if (player.getY() == getY()-Player.speed)
                {
                    while(player.getY() != getY()-1)
                    {
                        player.move(1);
                    }
                    Player.vspeed=0;
                }
            }
        }
    }
}
And this is the code I have for movement and rotation of the player.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public void keyCheck()
{   int dx=0, dy=0;
    if (Greenfoot.isKeyDown("d")){
        setRotation(0);
        dx++;
        /*move(speed);*/
    }
    if (Greenfoot.isKeyDown("a")){
        setRotation(180);
        dx--;
       /* move(speed);*/
    }
    if (Greenfoot.isKeyDown("w")){
        setRotation(-90);
        dy--;
        /*move(speed);*/
    }
    if (Greenfoot.isKeyDown("s")){
        setRotation(90);
        dy++;
        /*move(speed);*/
    }
    setLocation(getX()+hspeed*dx,getY()+vspeed*dy);
    /*        if(isTouching(Tree.class) != false)
    {
        setLocation(getX()-speed*dx,getY()-speed*dy);
    }*/
}
 
public void turnTowards()
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    if(mouse != null) {
        setRotation((int)(180*Math.atan2(mouse.getY()-getY(),mouse.getX()-getX())/Math.PI));
        if(mouse.getButton() == 1) {
            bulletControle.shoot(); //roep de shoot() methode aan van de bulletcontroler
        } else {
            if(Greenfoot.mouseClicked(null))
               {
                        bulletControle.shoot(); //roep de shoot() methode aan van de bulletcontroler
                }
        }
    }
im a noob and i only really have experience with Gamemaker so if someone could help me out a little?
danpost danpost

2017/1/23

#
If you mean you are trying to have the player stop when a tree is encountered (so that they do not intersect each other -- that kind of 'prevent collision'), then your Tree class should simply be this:
1
public class Tree extends Design {}
and you should have the player that moves check for trees and stay off of them.
You need to login to post a reply.