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

2013/8/20

need refrences

asiftahir asiftahir

2013/8/20

#
i am very new to the greenfoot i want some to teach me and help me in this if possible can show me his or her scenario just for references .......thanks in advance
Gevater_Tod4711 Gevater_Tod4711

2013/8/20

#
If you want to learn something about references you should read this greenfoot tutorial or have a look at this scenario.
danpost danpost

2013/8/20

#
When you download Greenfoot, you also get several sample scenarios you can look at. Also, any scenario on this site that has a large green 'Open in Greenfoot' button to the upper-right of its window can be downloaded and the code looked at.
asiftahir asiftahir

2013/8/21

#
thanks all
asiftahir asiftahir

2013/8/21

#
i dont want move continuously...i want that only when i press the key it moves what can be the codes for it......????
asiftahir asiftahir

2013/8/21

#
public void move() { //movement using the wasd keys if (Greenfoot.isKeyDown("a")) { moveLeft(); } if (Greenfoot.isKeyDown("d")) { moveRight(); } if (Greenfoot.isKeyDown("w")) { moveUp(); } if (Greenfoot.isKeyDown("s")) { moveDown(); } } i wrote all this and called the move() in the public void act()...... but the error is coming that cannot find the symbol...method moveleft().........
Gevater_Tod4711 Gevater_Tod4711

2013/8/21

#
The problem is that you try to call the method moveLeft but this method doesn't exist. You need to declare it:
public void moveLeft() {
    setLocation(getX() - 1, getY());
}

public void moveRight() {
    setLocation(getX() + 1, getY());
}

public void moveUp() {
    setLocation(getX() , getY() - 1);
}

public void moveDown() {
    setLocation(getX(), getY() + 1);
}
If you add this methods to your class it should work.
asiftahir asiftahir

2013/8/21

#
thanks bro.....i have one more question if i want tht moveLeft to work how i make make it work and declare ,,,,,,,,,,,,,, and ur code is working fine thanks for tht codes....
8bitcarrotjuice 8bitcarrotjuice

2013/8/21

#
You can also alter Tod's code so you can select what speed you want it to move at, like so:
public void moveLeft(int left) {  
    setLocation(getX() - left, getY());  
}  
  
public void moveRight(int right) {  
    setLocation(getX() + right, getY());  
}  
  
public void moveUp(int up) {  
    setLocation(getX() , getY() - up);  
}  
  
public void moveDown(int down) {  
    setLocation(getX(), getY() + down);  
}
and to use the code use:
public void move()
    {
        //movement using the wasd keys
        if (Greenfoot.isKeyDown("a"))
        {
            moveLeft(2);//change number to whatever you like
        }
        if (Greenfoot.isKeyDown("d"))
        {
            moveRight(2);//change number to whatever you like
        }
        if (Greenfoot.isKeyDown("w"))
        {
            moveUp(2);//change number to whatever you like
        }
        if (Greenfoot.isKeyDown("s"))
        {
            moveDown(2);//change number to whatever you like
        }
    }
danpost danpost

2013/8/21

#
I prefer determining the direction to move before moving:
public void move()
{
    int dx = 0, dy = 0;
    if (Greenfoot.isKeyDown("right")) dx++;
    if (Greenfoot.isKeyDown("left")) dx--;
    if (Greenfoot.isKeyDown("down")) dy++;
    if (Greenfoot.isKeyDown("up")) dy--;
    if (dx*dy == 0 && dx+dy != 0) setLocation(getX()+dx, getY()+dy);
}
The above will allow four-way movement only. To allow eight-way movement (allowing diagonal motion), do the following:
// change the last line to
if (dx !=0 || dy != 0) setLocation(getX()+dx, getY()+dy);
You can continue the code in the method to perform intersection checking as follows:
if (!getIntersectingObjects(null).isEmpty()) setLocation(getX()-dx, getY()-dy);
// or
if (!getIntersectingObjects(Some.class).isEmpty() ||
    !getIntersectingObjects(Another.class).isEmpty() ||
    !getIntersectingObjects(Etc.class).isEmpty())
        setLocation(getX()-dx, getY()-dy);
asiftahir asiftahir

2013/8/22

#
thnks ..very useful info..for me...
asiftahir asiftahir

2013/8/22

#
how i can increase the speed of the actors as the time increase//////?
Gevater_Tod4711 Gevater_Tod4711

2013/8/22

#
If you are using the code 8bitcarrotjuice gave you you can expand this code with some variables for the speed and incrase them after a while:
private int movingSpeed = 1;
private int actCounter = 0;

public void act() {
    if (Greenfoot.isKeyDown("up")) {
       moveUp(movingSpeed);
    }
    //...
    actCounter++;
    if (actCounter >= 1000) {
        actCounter = 0;
        movingSpeed++;
    }
}
This will make your actor move faster every 1000 acts.
8bitcarrotjuice 8bitcarrotjuice

2013/8/22

#
And you can also update Tod's code further by merging all the mehods into one, like so:
private int movingSpeed=1;
private int actCounter=0;

public void Move(int speed,String direction) {    
    if(direction=="up")
        setLocation(getX(), getY()-speed); 
    else if(direction=="down")
        setLocation(getX(), getY()+speed); 
    else if(direction=="left")
        setLocation(getX()-speed, getY());
    else if(direction=="right")
        setLocation(getX()+speed, getY());
    else
        //throw new Exception("Incorrect Direction Entered.");
}    
 
public void act()  
    {  
        if (Greenfoot.isKeyDown("a"))    
            Move(movingSpeed,left); 
        if (Greenfoot.isKeyDown("d"))    
            Move(movingSpeed,right);    
        if (Greenfoot.isKeyDown("w"))    
            Move(movingSpeed,up);    
        if (Greenfoot.isKeyDown("s"))    
            Move(movingSpeed,down);    
        actCounter++;
        if(actCounter>=1000) {
            actCounter=0;
            movingSpeed++;
        }
    }
or you can use Dan's 'move()' method! Not sure how to initilize an exception... Help?
You need to login to post a reply.