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

2012/12/12

Help with Actor movement speed

Sergio Sergio

2012/12/12

#
Hey guys, I'm creating a game with 3 characters. 2 characters need to be slow and 1 needs to be fast. I'm almost done but I have a problem with making the third character run fast since I don't use the move() method to move the player. This is how I move:
if(Greenfoot.isKeyDown("right")){
            
            setRotation(90); 
            //Controleer of er rechts van de speler geen water is, als dit niet zo is loop naar rechts.
            if(dijkrechts != null || (genieY >=38 && genieX != 49)){
            
                setLocation(getX()+1, getY());
                setGenieImage();
                
            }
        
        }  
        else if(Greenfoot.isKeyDown("up")){
            
            setRotation(0);
            //Controleer of er boven de speler geen water is, als dit niet zo is loop naar boven.    
            if(dijkboven != null || genieY >=38){
                    
                setLocation(getX(), getY()-1);
                setGenieImage();
                
            }
            
        }
        else if(Greenfoot.isKeyDown("left")){
            
            setRotation(270);
            //Controleer of er links van de speler geen water is, als dit niet zo is loop naar links.
            if(dijklinks != null  || (genieY >= 38 && genieX != 0)){
    
            setLocation(getX()-1, getY());
            setGenieImage();
        
            }
                
        }
        else if(Greenfoot.isKeyDown("down")){
            
            setRotation(180);
            //Controleer of er onder de speler geen water is, als dit niet zo is loop naar beneden.
            if(dijkonder != null || (genieY >= 38 && genieY < 47)){
           
                setLocation(getX(), getY()+1);
                setGenieImage();
            
            }
          
    }
Is there a way to increase his movement speed? But not by doing something like: setLocation(getX()+2,getY()); because I don't want the actor to skip blocks.
danpost danpost

2012/12/12

#
You could do one of two things: (1) make the slower objects 'act' method not execute every other cycle by adding a 'frame' counter to the class(es)
// instance variable added
int frameCount=0;
// act method
public void act()
{
    frameCount++;
    if(frameCount==2)frameCount=0;
    if(frame==0)
    {
        // code within current act method
    }
}
(2) which I have not tried, but would be alot easier: force the 'act' method to execute for the faster object (for a second time) from the world class
// in world 'act' method
genie.act();
where genie is the name of your reference to the faster object in the world class. If you do not have a reference
((Genie) getObjects(Genie.class).get(0)).act();
Sergio Sergio

2012/12/12

#
Well I already though of the first option but I don't want the other 2 to be slower fot the 1st to appear faster, because the first 2 will be to slow. The second option makes the character hard to control, it skips 1 cell a lot. My game is a top view game where I need to pick up objects, I need to be on top of the objects but it's getting really hard to pick those up if it's skipping cells. :(
danpost danpost

2012/12/12

#
Can you use the first option and speed up the scenario with the slidebar? (you can also use the 'setSpeed' method within the scenario)
Sergio Sergio

2012/12/12

#
That involves creating the loop for every Actor and there are a lot of Actors...
Sergio Sergio

2012/12/12

#
It does make sense though, let me try it!
danpost danpost

2012/12/12

#
The only other option, I believe, is to use doubles to control the speed of the slower two actors. They will have to keep track of their locations and be set to the closest cell to the current calculated location. Effectually making their speeds some fraction of one cell per cycle, slowing them down. Is your scenario with a gridsize of 1 or celled?
Sergio Sergio

2012/12/13

#
Hey mate sorry totally forgot to react yesterday, your previous option worked thank you so much man! Again! You're being a really big help :)
You need to login to post a reply.