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

2013/1/16

Need help, I´m a noob at Greenfoot :(

herzi789 herzi789

2013/1/16

#
Hey everyone! Sorry for my bad english, because I´m from Germany. I have a problem with Greenfoot, because I don´t really know much about this but must create a game for a school project. I have a world with a cat and a dog. I solved the problem that I can make the cat move with the arrows on the keyboard. But the new problem is that I don´t know how I can let the dog move at a special place from one side of the world to the other, turn around and so on. So the cat must avoid the dog. Hope you understand the problem and can help me! :(
danpost danpost

2013/1/16

#
For an actor to move horizontally back and forth across the world, you only need track the current direction of movement and have a check determining if the actor is at either edge of the world. Using an integer for the direction is helpful in that the movement can be based on the value (just multiply the speed by its value). Simply put (in pseudo-code):
move
if (at either left or right edge of the world)
{
    change direction
    change image
}
herzi789 herzi789

2013/1/16

#
okay thanks! Now I understand what I have to do, but whats the code for "change direction" and "change image" ?
danpost danpost

2013/1/16

#
You need to keep track of the direction the actor is moving horizontally. The easiest type of field to use will be an 'int' because it can be used in the movement code. The field can simply be called 'direction' and will be declared in the class with:
private int direction = 1;
The allowable values for the direction are 1 and -1, for moving right and moving left. The movement code would simply be:
setLocation(getX()+direction, getY());
For changing direction, just change the sign of 'direction' (or negate it)
direction = -direction;
For changing the image, the 'mirrorHorizontally' method of the GreenfootImage class will come in handy:
getImage().mirrorHorizontally();
herzi789 herzi789

2013/2/13

#
I´m so sorry, but I don´t get it.. :(
Draymothisk Draymothisk

2013/2/13

#
You could check your dog's x or y position and move according to that. The code would be simple. Assuming your world's size is 600 pixels wide, you can tell your dog the following. If your X position is greater than or equal to 590 (10 pixels less than your world's total width), move left (a negative number). If your X position is less than or equal to 10 (10 pixels more than your world's minimum width), move right (a positive number). The following code is exactly what is written here, except I created a variable named speed to hold our positive/negative number. With just this code, your image will not change, but it is important to get this first. This is the code to the entire "dog.class". import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class dog extends Actor { int speed = 3; public void act() { move(); checkEdges(); } public void move() { move(speed); } public void checkEdges() { if(getX() >= 590) { speed = -3; } if(getX() <= 10) { speed = 3; } } }
You need to login to post a reply.