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

2016/4/21

Using getX & getY in world?

2
3
4
5
6
7
8
danpost danpost

2016/5/3

#
As far as the wall and collision: it is difficult to say why without seeing what collision code you have in the class of the survivors. However, I cannot imagine why you would have walls that large (same size as world).
idk1234 idk1234

2016/5/4

#
Well the image size is the same as the world, but not the walls (it's a transparent file) as far as the button issue goes, I think it's working (I can't see properly because they are mushed up in corner) The only issue that persists is the survivors won't spawn in correct location (shoved up in a corner). Do I need to make the walls individual pieces? The way I did it was I put it all into 1 transparent image so it would be easier
    public void wallDetect()
    {
        {
            if (getOneIntersectingObject(Collision.class) != null)
            {
                  setLocation(x,y);
            }
            else
            {
                x = getX();
                y = getY();
                
            }
        }
    }
}
Heres the code for collision but even if I do not call the method the issue still persists.
danpost danpost

2016/5/4

#
idk1234 wrote...
Well the image size is the same as the world, but not the walls (it's a transparent file) as far as the button issue goes, I think it's working (I can't see properly because they are mushed up in corner) The only issue that persists is the survivors won't spawn in correct location (shoved up in a corner). Do I need to make the walls individual pieces? The way I did it was I put it all into 1 transparent image so it would be easier < Code Omitted >
The collision methods do not care about the individual pixels of an image -- whether they are transparent or not. All pixels are part of the image and are detected with those methods (location and size are what are accounted for. So, for example, the 'getOneIntersectingObject' method will return an object of the class given if any part of an image of one intersects any part of the image of the actor calling the method. If the image of your Wall object covers the entire world window, then a Wall object will be detected everywhere within that window, regardless of the transparency of the pixel at which is being checked.
danpost danpost

2016/5/4

#
idk1234 wrote...
Do I need to make the walls individual pieces? The way I did it was I put it all into 1 transparent image so it would be easier.
It would be easier for collision checking if you did make individual pieces for the walls. There are a couple ways of doing this. You could have one standard piece repeated multiple times for each wall or you could programmatically create the wall images and scale them to the appropriate lengths.
idk1234 idk1234

2016/5/6

#
Thanks for the help, it works now! But how would I repeat an if statement within 1 method but with another object? Code:
    private void walls() //Left, higher = right | Right, lower = up
    {
        if (canAddWalls()) addObject(new Wall1(), 928, 44);
        if (canAddWalls()) addObject(new Wall2(), 100, 44);
    }
    public boolean canAddWalls()
    {
        // in the world class, use this line
        return getObjects(Collision.class).size() < 1;
    }
 if (canAddWalls()) addObject(new Wall2(), 100, 44);
Doesn't work because there is already an if above it, any way to add it? The canAddWalls fixed a previous problem in which the image would repeat 5 million times, resulting in lag
SPower SPower

2016/5/6

#
Having two if-statements should work fine, only I wonder why you have two if-statements with the same condition. It's better to merge them into one if-statement:
if (canAddWalls()) {
    addObject(new Wall1(), 928, 44);
    addObject(new Wall2(), 100, 44);
}
idk1234 idk1234

2016/5/6

#
Oh, I'm stupid. Thanks for the help!
idk1234 idk1234

2016/5/7

#
So how do I set the rotation of an Image because animating works perfectly except when I go up, it animates for going down. I tried doing a separate method with the image rotated but no difference. Is there a certain rotation is defaults to? Here's the code I got:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Survivor is trying to survive the infection.
 * 
 * Gurveer Mand 
 * @version 0.01 - 15/04/2016
 */
public class SurvivorWASD extends Survivor
{
    private GreenfootImage survivor1r = new GreenfootImage("survivor1r.png");
    private GreenfootImage survivor2r = new GreenfootImage("survivor2r.png");
    private GreenfootImage survivor3r = new GreenfootImage("survivor3r.png");
    private GreenfootImage survivor4r = new GreenfootImage("survivor4r.png");
    private GreenfootImage survivor1up = new GreenfootImage("survivor1up.png");
    private GreenfootImage survivor2up = new GreenfootImage("survivor2up.png");
    private GreenfootImage survivor3up = new GreenfootImage("survivor3up.png");
    private GreenfootImage survivor4up = new GreenfootImage("survivor4up.png");
    private int frame = 1;
    private int animationCounter = 0;
    /**
     * Act - do whatever the Survivor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkKeys();
        wallDetect();
        animationCounter ++;
    } 

    /**
     * Check whether the control keys are being pressed, and turn if they are.
     */
    public void checkKeys()
    {
        if(Greenfoot.isKeyDown("A"))
        {
            setRotation(180);            
            move(2);            
            if(animationCounter % 10 == 0)
                animate();
        } 
        if(Greenfoot.isKeyDown("D"))
        {
            setRotation(0);            
            move(2);
            if(animationCounter % 10 == 0)
                animate();
        } 
        if(Greenfoot.isKeyDown("W"))
        {
            setRotation(90);           
            move(-2);
            if(animationCounter % 10 == 0)
                animate();        
        } 
        if(Greenfoot.isKeyDown("S"))
        {
            setRotation(90);            
            move(2);
            if(animationCounter % 10 == 0)
                animate();
        } 
    }

    public void animate()
    {
        if(frame == 1)
        {
            setImage (survivor1r);
        }
        else if(frame == 2)
        {
            setImage (survivor2r);
        }
        else if(frame == 3)
        {
            setImage (survivor3r);
        }
        else if(frame == 4)
        {
            setImage (survivor4r);
            frame =1;
            return;
        }
        frame ++;
    }

    public void animateUp()
    {
        if(frame == 1)
        {
            setImage (survivor1r);
        }
        else if(frame == 2)
        {
            setImage (survivor2r);
        }
        else if(frame == 3)
        {
            setImage (survivor3r);
        }
        else if(frame == 4)
        {
            setImage (survivor4r);
            frame =1;
            return;
        }
        frame ++;
    }
}
SPower SPower

2016/5/7

#
You have to enter a negative rotation to rotate the other direction:
setRotation(-90);
Alternatively, you could also rotate 180 degrees more:
setRotation(270);
Since -90 degrees has the same end result as rotating 270 degrees.
idk1234 idk1234

2016/5/7

#
Once applying that, the image rotation is fine but the character goes down instead of up
SPower SPower

2016/5/7

#
Have you also change the move command underneath to have a positive value, then?
move(2);
idk1234 idk1234

2016/5/7

#
haha I can't believe I overlooked that, thanks! :)
SPower SPower

2016/5/7

#
No problem. Sometimes, one of the greatest causes of bugs is overlooking stuff. I find explaining my code to someone else (or myself, if needed), helps me to notice those kinds of things.
idk1234 idk1234

2016/5/7

#
Welp I have another problem. This time I was following a greenfoot tutorial on shooting. But I ran into a problem. It was working until the tutorial said to change the code. Before when it was working:
        if(Greenfoot.isKeyDown("V"))
        {
            shoot();
        }
    }
After, not working
        if("V".equals(Greenfoot.getKey()))
        {
            shoot();
        }
    }
Other code:
    /**
     * Shoots a lazer.
     */
    private void shoot()
    {
        Lazer lazer = new Lazer();
        getWorld().addObject(lazer, getX(), getY());
        lazer.setRotation(getRotation());
    }
SPower SPower

2016/5/7

#
Could you specify what you mean by "not working"? If by that you mean it only shoots once after pressing the v key, then that's exactly what the getKey method should do, so no bugs there. If it doesn't shoot at all, you're calling getKey somewhere else, which makes this one return null, and so not satisfying the condition in the if-statement.
There are more replies on the next page.
2
3
4
5
6
7
8