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

2016/12/18

Moving an actor by 1 pixel with right animation

HelpMeGer HelpMeGer

2016/12/18

#
Hello, my English is not very good and I am a beginner with Greenfoot. I have an actor with different Images (to simulate real moving) who can move 1 pixel on a map (circa 640x640 pixels). But moving only 1 pixel is too slow. When I set the speed up the Images (to simulate real moving) are changing to fast. The actor must move only 1 pixel because he must stop shortly before a barrier (not 2,3,4,5,... pixels before the barrier). This is my code:
    private GreenfootImage ao1 = new GreenfootImage("ao1.png");
    private GreenfootImage ao2 = new GreenfootImage("ao2.png");
    private GreenfootImage ao3 = new GreenfootImage("ao3.png");
    private GreenfootImage ao4 = new GreenfootImage("ao4.png");
    private GreenfootImage ao5 = new GreenfootImage("ao5.png");
    private GreenfootImage ao6 = new GreenfootImage("ao6.png");
    private int frameArR = 1;

public boolean wandRechts()
{
 return getOneObjectAtOffset(35, 0,Stein.class) != null;
}
 
public void RechtsBewegung()
{
 if(frameArR == 1)
{
 setImage(ao1);
}
else if(frameArR == 2)
{
 setImage(ao2);
}
else if(frameArR == 3)
{
 setImage(ao3);   
}
else if(frameArR < 4)
{
 setImage(ao4);   
}
else if(frameArR < 5)
{
 setImage(ao5);   
}
else if(frameArR == 6)
{
 setImage(ao6);
 if(frameArR == 6)
 {
  frameArR = 1;
 }
 return;
}
frameArR = frameArR + 1;

public void act()
{
 if(Greenfoot.isKeyDown("right"))
 {
  RechtsBewegung();
  if(!wandRechts())
  {
   move(5);
  }
 }
}
I hope you can help me.
Nosson1459 Nosson1459

2016/12/18

#
HelpMeGer wrote...
But moving only 1 pixel is too slow. When I set the speed up the Images (to simulate real moving) are changing to fast. The actor must move only 1 pixel because he must stop shortly before a barrier (not 2,3,4,5,... pixels before the barrier).
Is the problem moving too slow, or animating too fast (and you should probably show the full class code, from first to last line)?
danpost danpost

2016/12/19

#
You can slow the animation down by inserting the following line at line 16 above:
int frame = frameArR/2;
and then changing all occurrences of 'frameArR' from lines 16 to 40 with 'frame'. Just increase the value '2' to slow it down even more.
HelpMeGer HelpMeGer

2016/12/19

#
@Nosson I mean that the animating is too fast when the actor has the right movementspeed. And only these lines are important, I think :-). @danpost Sorry, I dont know what you mean. Could you copy my code and fill in what to do, pls?
danpost danpost

2016/12/19

#
I mean like this:
public void RechtsBewegung()
{
    frameArR = frameArR + 1;
    int frame = frameArR/2; // increasing '2' slows down animation
    if (frame == 1)
    {
        setImage(ao1);
    }
    else if (frame == 2)
    {
        setImage(ao2);
    }
    else if (frame == 3)
    {
        setImage(ao3);   
    }
    else if (frame < 4) // why '<' ?
    {
        setImage(ao4);   
    }
    else if( frame < 5) // why '<' ?
    {
        setImage(ao5);   
    }
    else if (frame == 6)
    {
        setImage(ao6);
        frameArR = 1;
    }
}
I questioned some of your 'if' conditions. Line 21, for example, will only be true if the value of 'frame' was '4'. With line 17, the condition will only be true if the value was less than '1'. With those, together, nothing would happen if the value was '5'.
HelpMeGer HelpMeGer

2016/12/20

#
Okay, thanks a lot. I will try this. And with "<" you are right. It is only there because i tried something.
You need to login to post a reply.