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

2012/5/13

How do i get an actor to move in the y axis?

stateoaks stateoaks

2012/5/13

#
I want to make a little RPG game, kinda old style zelda kinda thing. I have gotten the actor to move on the x axis, but how do i get it to move up and down via pressing the up and down arrow keys? This is the code i have so far: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Charforward here. * * @author (oaks) * @version (0.1) */ public class Charforward extends Actor { /** * Act - do whatever the Charforward wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.isKeyDown ("left")) {move(-4); } if (Greenfoot.isKeyDown ("right")) {move(4); } if (Greenfoot.isKeyDown ("up")) { move ();}}} I've only started using Greenfoot about 2 days ago, so i'm probably missing something really obvious. Thanks,
danpost danpost

2012/5/13

#
The move(int) command moves the actor in the direction it is facing (default is right; so a positive 'int' moves right and a negative'int' moves left). If you do not mind rotating the actor (turning it on its side, visually), then you can use the setRotation() method to adjust the direction. If you would rather keep the image of the moving object as is, then instead of the move(int) method, you are better off using the setLocation(int, int) method.
stateoaks stateoaks

2012/5/13

#
Hi, i tried what you said, and it moves the actor to a certain co-ordinate. Is there a way that i could get it to move on the y axis without making it jump to a specific co-ordinate? As in , to make the actor move the same way it would on the x axis with a move command? Sorry if i'm being a pain, i really just have no idea D: Thanks,
davmac davmac

2012/5/13

#
For example: setLocation(getX(), getY() + 4);
danpost danpost

2012/5/13

#
Or
setRotation(90);
if (Greenfoot.isKeyDown("up"))
  { move(-4); }
if (Greenfoot.isKeyDown("down"))
  { move(4); }
setRotation(0);
stateoaks stateoaks

2012/11/22

#
Hi guys- just realized i never said thank you ( how rude of me!) So, thanks a bunch :3
You need to login to post a reply.