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

2013/10/5

help with movement

manish123 manish123

2013/10/5

#
hello, I am currently working on a game where the objective of the game is to collect as many points as you can. However im finding it challenging to move the player because it is not just a simple "move()" command. If anyone has played the tron light bike game that would be very helpful because I want my character to move like that, this is my code so far: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class blueBlock here. * * @author (your name) * @version (a version number or a date) */ public class blueBlock extends PlatformMover { /** * Act - do whatever the blueBlock wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public blueBlock() { setDirection(EAST); } public void act() { Control(); } public void Control() { if(Greenfoot.isKeyDown("up")) { turn(getRotation() + 90); } if(Greenfoot.isKeyDown("down")) { turn(getRotation() - 90); } if(Greenfoot.isKeyDown("left")) { move(-3); } if(Greenfoot.isKeyDown("right")) { move(3); } } And in my platform mover class i have : import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class PlatformMover here. * * @author (your name) * @version (a version number or a date) */ public class PlatformMover extends Actor { public static final int EAST = 0; public static final int WEST = 1; public static final int NORTH = 2; public static final int SOUTH = 3; /** * Act - do whatever the PlatfromMover wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public int direction; public void act() { } public void setDirection(int direction) { this.direction = direction; switch(direction) { case SOUTH: setRotation(90); break; case EAST: setRotation(0); break; case NORTH : setRotation(270); break; default : break; } } } All my actor does right now is go up and down and turn up and down
danpost danpost

2013/10/5

#
One thing is that the 'turn' method takes the current rotation of the actor into consideration. So, you should be using 'turn(90)' for 'right' and 'turn(-90)' for 'left'; otherwise you could use 'setRotation(getRotation()+90)' for 'right' and 'setRotation(getRotation()-90)' for 'left'.
danpost danpost

2013/10/5

#
Something that will make things easier is by assigning your directions in a more useful manner. By using the following:
public static final EAST = 0,
                    SOUTH = 1,
                    WEST = 2,
                    NORTH = 3;
you could then use:
public void setDirection(int direction)
{
    setRotation(90*direction);
}
and:
direction = getRotation()/90;
With this, you will then find that you do not need a 'direction' field at all because just by getting the rotation of the object you can determine the direction (this would save quite a bit on the amount of coding).
manish123 manish123

2013/10/5

#
Thanks danpost! Yea that would have worked also but i solved the problem by making 2 variables "x" and "y" and then minus 1 from y if it moved up and etc.... then i did setLocation(getX() + x, getY() + y) :D
You need to login to post a reply.