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

2014/2/25

Making impassable Objects...

MomoLad MomoLad

2014/2/25

#
I'm making a maze game, and I've been unsuccessfully trying to make the walls of the maze barriers that the player cannot pass. I've tried multiple ways, yet nothing seems to work. The player just passes through the walls like they're not even there. The class the "Player" class cant pass is the "MazeLines" class. I just need some guidance on where to start with this, as getOneIntersectingObject has failed every time I've tried it.
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
84
85
86
87
88
89
90
91
92
93
94
public class Player extends Animal
{
    private int coinsCollected = 0;
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
     
    private int deltaX;
    private int deltaY;
      
    static final int CHANGE_RATE =3;
    int changeImgNum =1;
    int imageNum=1;
    String[]images={"Walking1.png", "Walking2.png"};
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        image1=new GreenfootImage("stickMan.png");
        image2= new GreenfootImage("Walking1.png");
        image3= new GreenfootImage("Walking2.png");
        setImage(image1);
        move();
        checkWalls();
        coinsCollected=0;
        getCoin();
        Finish();
    }    
    public void move()
    {
       //make WASD move the player.
        if(Greenfoot.isKeyDown("w"))
        {
            setImage(image2);
            animateWalk();
            setRotation(270);
            move(2);
        }
        if(Greenfoot.isKeyDown("a"))
        {
            setImage(image2);
            animateWalk();
            setRotation(180);
            move(2);
        }
        if(Greenfoot.isKeyDown("s"))
        {
            setImage(image2);
            animateWalk();
            setRotation(90);
            move(2);
        }
        if(Greenfoot.isKeyDown("d"))
        {
            setImage(image2);
            animateWalk();
            setRotation(0);
            move(2);
        }
    }
    public void animateWalk() //animation only shows walking2.png for a second, needs to be longer
    {
        changeImgNum--;
        if(changeImgNum==0)
        {
            changeImgNum = CHANGE_RATE;
            imageNum=(imageNum+1)%2;
            setImage(images[imageNum]);
        }
    }
    public void getCoin() //Collect coins
    {
        if(canSee(Coin.class))
        {
            eat(Coin.class);
            Greenfoot.playSound("Coin.wav");
            coinsCollected++;
        }
    }
    public void Finish()
    {
        if(canSee(Finish.class))
        {
            eat(Finish.class);
            Greenfoot.playSound("Clear.wav");
            Greenfoot.stop();
        }
    }
    public void checkWalls()
    {
    }
}
danpost danpost

2014/2/26

#
Have you tried:
1
2
3
4
public void checkWalls()
{
    if (!getIntersectingObjects(MazeLines.class).isEmpty()) move(-2);
}
MomoLad MomoLad

2014/2/26

#
When I put that in, the Player just constantly moves -2, even when not touching MazeLines. He still just goes right through them too...
MomoLad MomoLad

2014/2/26

#
Oh, I see whats going on. The MazeLines image I'm using has a transparent background, as a PNG. I thought it wouldn't count the transparent part at part of the image, but it does. Is there some way to make the transparent parts of the image NOT count as part of it?
danpost danpost

2014/2/26

#
Download my Image Transparency Adder scenario and use it to remove the unwanted transparency. It not only removes unwanted white space around an image, but also reduces the size to minimize the amount of transparency left around an image. BTW, I presume that the image of the MazeLines object is basically a single-directional slender rod of an object.
MomoLad MomoLad

2014/2/26

#
As another Option, since ,unfortunately, downloading additional scenarios is out of the question, in the Greep program, it seems like they get the color of each pixel in the background image, then declare the blue pixels water. The greeps then cannot pass the blue areas on the background.. The code used look like this.
1
2
3
4
5
public boolean isWater(int x, int y)
{
    Color col = map.getColorAt(x, y);
    return col.getBlue() > (col.getRed() * 2);
}
This was another way I was thinking I could do me project. The maze lines are the only black objects in my program, yet this didn't work when I tried it in my program... Could you explain how this works? It seems so simple, yet it doesnt want to work with me.
danpost danpost

2014/2/26

#
No. The greeps program uses color detection on the world background; not on the images of actors within the world. You could try: Code omitted You will have to supply the value to replace 'halfLineWidth' with (half of however wide the actual image of the mazeline is). I believe that I coded that properly. I will double-check and confirm. Sorry, that did not work. I will try and come up with something that will.
MomoLad MomoLad

2014/2/26

#
Thanks for helping. I really appreciate that you take time to help those with Greenfoot Problem. I'm just gonna go with plan B, which is to spawn the whole maze piece by piece with coordinates. Not the fastest or the most professional way, but it will work. Thanks again for trying to help! Hopefully I'll understand as much as you someday.
You need to login to post a reply.