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

2018/7/7

problem with my code

tosteck tosteck

2018/7/7

#
I want to make a character be able to make a wall and then select a direction to put it in. There are no errors, but when I run the code it freezes. I think there is something wrong with my code. String key = Greenfoot.getKey(); if (key != null && key.equals("space")) { Wall wall = new Wall(); getWorld().addObject(wall, getX(), getY()); } if(Greenfoot.isKeyDown("space")) { int WallX = getX(); int WallY = getY(); if(Greenfoot.isKeyDown("D")) { WallX += 50; } else if(Greenfoot.isKeyDown("W")) { WallY -= 50; } else if(Greenfoot.isKeyDown("S")) { WallY += 50; } else if(Greenfoot.isKeyDown("A")) { WallX -= 50; } Actor wall; wall=getOneObjectAtOffset(0,0, Wall.class); wall.setLocation(WallX,WallY);
danpost danpost

2018/7/7

#
tosteck wrote...
I want to make a character be able to make a wall and then select a direction to put it in. There are no errors, but when I run the code it freezes. I think there is something wrong with my code. << Code Omitted >>
There is nothing in your code that, as far as I can tell, would cause it to freeze. I would, however, suggest that you refrain from using Greenfoor.getKey() for anything other than text input or menu shortcut keys.
tosteck tosteck

2018/7/7

#
How should I change it? Right now I am using it because when I use Greenfoot.isKeyDown, it will create more than one wall.
danpost danpost

2018/7/7

#
tosteck wrote...
How should I change it? Right now I am using it because when I use Greenfoot.isKeyDown, it will create more than one wall.
Add a boolean field to track the state of the key. Normally, this is done as follows:
// add field
private boolean spaceDown;

// in method
if (spaceDown != Greenfoot.isKeyDown("space")) // if state of key changed
{
    spaceDown = !spaceDown; // new state
    if (spaceDown) // key pressed
    {
        // action code here
    }
}
In your case, however, the two checks need to be in separate blocks and you will probably need another field for the wall:
// fields
private Actor wall;
private boolean spaceDown;

// in method
if (spaceDown != Greenfoot.isKeyDown("space"))
{
    spaceDown = !spaceDown;
    if (spaceDown) wall = new Wall(); else wall = null;
}
if (spaceDown && wall != null)
{
    int dx = 0, dy = 0;
    if (Greenfoot.isKeyDown("W")) dy--;
    if (Greenfoot.isKeyDown("A")) dx--;
    if (Greenfoot.isKeyDown("S")) dy++;
    if (Greenfoot.isKeyDown("D")) dx++;
    if (dx*dy == 0 && dx+dy != 0)
    {
        getWorld().addObject(wall, getX()+50*dx, getY()+50*dy);
        wall = null;
    }
}
More condition may need to be added to line 18 to make sure wall are not placed where they should not go (like on top of another wall or outside world bounds).
tosteck tosteck

2018/7/8

#
Thanks! the code worked really well!
tosteck tosteck

2018/7/8

#
I have another question. I want to change the image of the wall by using setImage when it is placed left or right, but I am not sure how to set the image of another actor. I want to put the code into here.
            if (Greenfoot.isKeyDown("left")) 
            {
                dx--;
            }
danpost danpost

2018/7/8

#
tosteck wrote...
I have another question. I want to change the image of the wall by using setImage when it is placed left or right, but I am not sure how to set the image of another actor. I want to put the code into here. << Code Omitted >>
Better would be in the final if block. What are the names of your image files?
tosteck tosteck

2018/7/9

#
image file names are Wall.png for normal wall Wallside.png for sideways wall
danpost danpost

2018/7/9

#
tosteck wrote...
image file names are Wall.png for normal wall << Image Omitted >> Wallside.png for sideways wall << Image Omitted >>
Add the following line inside the last if block (before line 21 as shown above):
wall.setImage(dy == 0 ? "Wallside.png" : "Wall.png");
This is the same as:
if (dy == 0)
{
    wall.setImage("Wallside.png");
}
else
{
    wall.setImage("Wall.png");
}
You need to login to post a reply.