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

2013/6/8

How do i make my actor move slower?

dodo dodo

2013/6/8

#
Here is my actor code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class turtle here. * * @author (your name) * @version (a version number or a date) */ public class Ash extends Actor { private GreenfootImage image1; private GreenfootImage image2; private GreenfootImage image3; private GreenfootImage image4; private GreenfootImage image5; private GreenfootImage image6; private GreenfootImage image7; private GreenfootImage image8; public Ash() { image1 = new GreenfootImage("down1.png"); image2 = new GreenfootImage("down2.png"); image3 = new GreenfootImage("left1.png"); image4 = new GreenfootImage("left2.png"); image5 = new GreenfootImage("right1.png"); image6 = new GreenfootImage("right2.png"); image7 = new GreenfootImage("up1.png"); image8 = new GreenfootImage("up2.png"); } /** * Act - do whatever the turtle wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(); checkCollision(); } public void move() { if( Greenfoot.isKeyDown("down")) { moveDown(); } if( Greenfoot.isKeyDown("up")) { moveUp(); } if( Greenfoot.isKeyDown("left")) { moveLeft(); } if( Greenfoot.isKeyDown("right")) { moveRight(); } } public void checkCollision() { Actor a = getOneIntersectingObject( Wall.class ); if ( a != null ) { if( Greenfoot.isKeyDown("down")) { wallDown(); } if( Greenfoot.isKeyDown("up")) { wallUp(); } if( Greenfoot.isKeyDown("left")) { wallLeft(); } if( Greenfoot.isKeyDown("right")) { wallRight(); } } } public void moveDown() { int x = (getX()); int y = (getY() + 1); setLocation(x, y); if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } public void moveUp() { int x = (getX()); int y = (getY() - 1); setLocation(x, y); if (getImage() == image8) { setImage(image7); } else { setImage(image8); } } public void moveLeft() { int x = (getX() - 1); int y = (getY()); setLocation(x, y); if (getImage() == image4) { setImage(image3); } else { setImage(image4); } } public void moveRight() { int x = (getX() + 1); int y = (getY()); setLocation(x, y); if (getImage() == image6) { setImage(image5); } else { setImage(image6); } } public void wallDown() { int x = (getX()); int y = (getY() - 1); setLocation(x, y); } public void wallUp() { int x = (getX()); int y = (getY() + 1); setLocation(x, y); } public void wallLeft() { int x = (getX() + 1); int y = (getY()); setLocation(x, y); } public void wallRight() { int x = (getX() - 1); int y = (getY()); setLocation(x, y); } }
RUMMAKER RUMMAKER

2013/6/8

#
im new with programing so this might not be the most efficient methud but, if you make a timer EX: int timer act() { if(timer > 2)//will move one pixel every 2 acts instead of every 1 act { move() timer = 0;//resets timer } timer ++;//timer counts up }
dodo dodo

2013/6/8

#
no sorry that didn't work at all, any other ideas?
welleid welleid

2013/6/8

#
You can import the SmoothMover method directly from Greenfoot, go menu, edit, import class. That should make it, you will be able to put getY() + 0.1, or getX() + 0.1, or even + 0.00001 if you want^^
Zamoht Zamoht

2013/6/8

#
Declare two private floats floatingX and floatingY. Your move up would look like this instead.
public void moveUp() 
{ 
     floatingY -= 0.1f; 
     setLocation((int)floatingX, (int)floatingY); 
     if (getImage() == image8)
     { 
          setImage(image7); 
     } else { setImage(image8); } 
}
This should work if you set it up right. Remember to sync the floatingX and floatingY with getX() and getY() when you init your object. Right now it should move 10 times slower, but you can just change the 0.1f to something else.
dodo dodo

2013/6/9

#
how do I set up floatingX and Y ? I tried getX() = floatingX and getY() = floatingY but that didnt work.
K_wow K_wow

2013/6/9

#
You just do it the other way around, like this: floatingX = getX(); and: floatingY = getY();
Zamoht Zamoht

2013/6/9

#
in public Ash() you put floatingX = getX(); and: floatingY = getY();
dodo dodo

2013/6/12

#
I put it in public ash except it is saying it can't find the symbol floatingX
Zamoht Zamoht

2013/6/13

#
That's because you have to put: private float floatingX; private float floatingY; under: private GreenfootImage image8;
dodo dodo

2013/6/13

#
I keep getting a greenfoot terminal window popping up saying: java.lang.IllegalStateException: The actor has not been inserted into a world so it has no location yet. You might want to look at the method addedToWorld on the Actor class. at greenfoot.Actor.failIfNotInWorld(Actor.java:659) at greenfoot.Actor.getX(Actor.java:157) at Ash.<init>(Ash.java:37) at Level.<init>(Level.java:37) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at greenfoot.core.Simulation.newInstance(Simulation.java:511) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:406) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:402) at greenfoot.core.Simulation.maybePause(Simulation.java:262) at greenfoot.core.Simulation.runContent(Simulation.java:194) at greenfoot.core.Simulation.run(Simulation.java:187) java.lang.IllegalStateException: The actor has not been inserted into a world so it has no location yet. You might want to look at the method addedToWorld on the Actor class. at greenfoot.Actor.failIfNotInWorld(Actor.java:659) at greenfoot.Actor.getX(Actor.java:157) at Ash.<init>(Ash.java:37) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at greenfoot.core.Simulation.newInstance(Simulation.java:511) at greenfoot.localdebugger.LocalDebugger$QueuedInstantiation.run(LocalDebugger.java:148) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:402) at greenfoot.core.Simulation.maybePause(Simulation.java:262) at greenfoot.core.Simulation.runContent(Simulation.java:194) at greenfoot.core.Simulation.run(Simulation.java:187) java.lang.IllegalStateException: The actor has not been inserted into a world so it has no location yet. You might want to look at the method addedToWorld on the Actor class. at greenfoot.Actor.failIfNotInWorld(Actor.java:659) at greenfoot.Actor.getX(Actor.java:157) at Ash.<init>(Ash.java:37) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at greenfoot.core.Simulation.newInstance(Simulation.java:511) at greenfoot.localdebugger.LocalDebugger$QueuedInstantiation.run(LocalDebugger.java:148) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:402) at greenfoot.core.Simulation.maybePause(Simulation.java:262) at greenfoot.core.Simulation.runContent(Simulation.java:194) at greenfoot.core.Simulation.run(Simulation.java:187) java.lang.IllegalStateException: The actor has not been inserted into a world so it has no location yet. You might want to look at the method addedToWorld on the Actor class. at greenfoot.Actor.failIfNotInWorld(Actor.java:659) at greenfoot.Actor.getX(Actor.java:157) at Ash.<init>(Ash.java:37) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at greenfoot.core.Simulation.newInstance(Simulation.java:511) at greenfoot.localdebugger.LocalDebugger$QueuedInstantiation.run(LocalDebugger.java:148) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:402) at greenfoot.core.Simulation.maybePause(Simulation.java:262) at greenfoot.core.Simulation.runContent(Simulation.java:194) at greenfoot.core.Simulation.run(Simulation.java:187) java.lang.IllegalStateException: The actor has not been inserted into a world so it has no location yet. You might want to look at the method addedToWorld on the Actor class. at greenfoot.Actor.failIfNotInWorld(Actor.java:659) at greenfoot.Actor.getX(Actor.java:157) at Ash.<init>(Ash.java:37) at Level.<init>(Level.java:33) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at greenfoot.core.Simulation.newInstance(Simulation.java:511) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:406) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:402) at greenfoot.core.Simulation.maybePause(Simulation.java:262) at greenfoot.core.Simulation.runContent(Simulation.java:194) at greenfoot.core.Simulation.run(Simulation.java:187) java.lang.IllegalStateException: The actor has not been inserted into a world so it has no location yet. You might want to look at the method addedToWorld on the Actor class. at greenfoot.Actor.failIfNotInWorld(Actor.java:659) at greenfoot.Actor.getX(Actor.java:157) at Ash.<init>(Ash.java:37) at Level.<init>(Level.java:33) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at greenfoot.core.Simulation.newInstance(Simulation.java:511) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:406) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:402) at greenfoot.core.Simulation.maybePause(Simulation.java:262) at greenfoot.core.Simulation.runContent(Simulation.java:194) at greenfoot.core.Simulation.run(Simulation.java:187)
danpost danpost

2013/6/13

#
You cannot use 'getX' or 'getY' or any method that requires that the object being created be in the world within the constructor of that object. Please post your 'Ash' class constructor (the code-block beginning with 'public Ash()').
dodo dodo

2013/6/13

#
public Ash() { image1 = new GreenfootImage("down1.png"); image2 = new GreenfootImage("down2.png"); image3 = new GreenfootImage("left1.png"); image4 = new GreenfootImage("left2.png"); image5 = new GreenfootImage("right1.png"); image6 = new GreenfootImage("right2.png"); image7 = new GreenfootImage("up1.png"); image8 = new GreenfootImage("up2.png"); private float floatingX; private float floatingY; }
danpost danpost

2013/6/13

#
You better post the whole class. Although the error message points to this location, it does not appear to be in the part you posted. By the whole class, I mean select all the code and copy/paste it using the 'code' tag below the inputbox.
You need to login to post a reply.