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

2013/3/15

New Game

1
2
geekykid2013 geekykid2013

2013/3/15

#
I would like to make an object move left to right using the arrow and mouse keys as controls how can I do this?
Gevater_Tod4711 Gevater_Tod4711

2013/3/15

#
To check whether a key is pressed at the moment you can use Greenfoot.isKeyDown(String keyname). You also find this in the Greenfoot API. If you want your actor to move in the direction of the arrow keys you can use this method:
public void move() {
    if (Greenfoot.isKeyDown("up")) {
        setLocation(getX(), getY()-1);
    }
    if (Greenfoot.isKeyDown("down")) {
        setLocation(getX(), getY()+1);
    }
    if (Greenfoot.isKeyDown("left")) {
        setLocation(getX()-1, getY());
    }
    if (Greenfoot.isKeyDown("right")) {
        setLocation(getX()+1, getY());
    }
}
You have to call this method from your act method to use it.
geekykid2013 geekykid2013

2013/3/15

#
Thanks Gevater_Todd4711...really recommend greefoot its awesome. only one problem i'm getting an error in line 8. My code sysntax is correct but I keep hetting an error : illegal start type what does this mean?
Gevater_Tod4711 Gevater_Tod4711

2013/3/15

#
Im not shure what this means but maybe I can help you if you show me your code.
geekykid2013 geekykid2013

2013/3/15

#
public void move() { if (Greenfoot.isKeyDown("up")){ setLocation(getX(), getY()-1); } if (Greenfoot.isKeyDown("down")){ setLocation(getX(), getY()+1); } if (Greenfoot.isKeyDown("left")){ setLocation(getX()-1, getY()); } if (Greenfoot.isKeyDown("right")){ setLocation(getX()+1, getY()); } } I can't see what I'm doing wrong...maybe iits my syntax coding...what do you think?
Gevater_Tod4711 Gevater_Tod4711

2013/3/15

#
To me this method seems to be ok. Can you post your whole code? maybe there is a failur somewhere else. And if you post code you should use the code funktion of greenfoot. You find it under the textfield.
geekykid2013 geekykid2013

2013/3/15

#
i noticed an error and made a correction. i forgot to include the act (); sorry here is my code. it now works public void move() { //System.out.println("move funtion called"); /*if (Greenfoot.isKeyDown("up")){ setLocation(getX(), getY()-1); } if (Greenfoot.isKeyDown("down")){ setLocation(getX(), getY()+1); }*/ if (Greenfoot.isKeyDown("left")){ //System.out.println("left key pressed"); setLocation(getX()-1, getY()); } if (Greenfoot.isKeyDown("right")){ setLocation(getX()+1, getY()); //System.out.println( "right key pressed"); } } public void act() { move(); } }
geekykid2013 geekykid2013

2013/3/15

#
i would like my object to stop moving when it hits the edge of the world. what can i do?
Gevater_Tod4711 Gevater_Tod4711

2013/3/15

#
With this method you can check whether the object is at the edge of the world:
public boolean atWorldEdge() {
            if (getX() < 20 || getX() > getWorld().getWidth() - 20) {
                return true;
            }
            if (getY() < 20 || getY() > getWorld().getHeight() - 20) {
                return true;
            }
        }
        return false;
    }
geekykid2013 geekykid2013

2013/3/15

#
what does the error: illegal start type mean at line 9? I'm confused public boolean atWorldEdge() { if (getX() < 20 || getX() > getWorld().getWidth() - 20) { return true; } if (getY() < 20 || getY() > getWorld().getHeight() - 20) { return true; } } return false; }
Gevater_Tod4711 Gevater_Tod4711

2013/3/15

#
I again can't find anything that is wrong in this code. May the error be somewhere else?
danpost danpost

2013/3/15

#
The 'return false;' statement should be moved up inside the preceding closing bracket.
geekykid2013 geekykid2013

2013/3/15

#
i too cannot see where the error, here is the full code for a better look import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class basket here. * * @author (your name) * @version (a version number or a date) */ public class basket extends Actor { /** * Act - do whatever the basket wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void move() { //System.out.println("move funtion called"); /*if (Greenfoot.isKeyDown("up")){ setLocation(getX(), getY()-1); } if (Greenfoot.isKeyDown("down")){ setLocation(getX(), getY()+1); }*/ if (Greenfoot.isKeyDown("left")){ //System.out.println("left key pressed"); setLocation(getX()-1, getY()); } if (Greenfoot.isKeyDown("right")){ setLocation(getX()+1, getY()); //System.out.println( "right key pressed"); } } public void act() { move(); } public boolean atWorldEdge() { // object detects edge of world then stops if (getX() < 20 || getX() > getWorld().getWidth() - 20) { return true; } if (getY() < 20 || getY() > getWorld().getHeight() - 20) { return true; } } return false; } }
danpost danpost

2013/3/15

#
bump (my last post on this discussion).
geekykid2013 geekykid2013

2013/3/16

#
thanks danpot, that seems to do the trick. ur a genius
There are more replies on the next page.
1
2