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

2017/6/24

How to move the actor up and down

Hi, I use this scenario for a scrollingWorld. http://www.greenfoot.org/scenarios/5806 What do I have to do, to make the actor move up or down if the key up or down is pressed Moving forward works perfect. That's my code of the actor's act method
   
if(Greenfoot.isKeyDown("up")){
            getWorld().setCameraDirection(0);
            setRotation(0);
            
            getWorld().turnCamera(-45); 
            turn(-45);
            getWorld().moveCamera(5);
            
        }
       
        if(Greenfoot.isKeyDown("down")){
           getWorld().setCameraDirection(0);
           setRotation(0);
            
            getWorld().turnCamera(45); 
            turn(45);
          getWorld().moveCamera(10);
        
        }

       
        if(Greenfoot.isKeyDown("right")){
            setRotation(0);
          getWorld().moveCamera(5);

        }
Thanks for your help
Super_Hippo Super_Hippo

2017/6/24

#
I think you have to remove lines 5, 6, 15 and 16, change the 0 in lines 2 and 3 to 270 and the 0 in lines 12 and 13 to 90. You may also have to add a setCameraDirection(0) before moving right.
Thanks for your help, but it doesn't work Has anyone else an idea, maybe the editor of the scenario??
What I actually would like to have is this,but with the camera movement:
if(Greenfoot.isKeyDown("up")){
          
            setRotation(0);
            turn(-45);
           move(5);
             
        }
        
        if(Greenfoot.isKeyDown("down")){
           setRotation(0);
            turn(45);
          move(5);
         
        }
 
        
        if(Greenfoot.isKeyDown("right")){
            setRotation(0);
      move(5);
 
        }
For moving the camera I have to use the moveCamera method, but how do I turn the camera?
Super_Hippo Super_Hippo

2017/6/25

#
In this scenario, there is a "camera" and a bug which is a camera follower (note how it is added to the world). "Turning the camera" does not mean that the screen will turn around, it will only give the direction for the camera movement when using 'moveCamera'.
Yeah, I've got a solution now. You have to set the fullheight value at a high value. so you can move the actor up and down, but I would like to have it fixed in the y-direction and only scrolling in the x- direction Is this possible, but also the up and down movement should be possible
Super_Hippo Super_Hippo

2017/6/25

#
Yes, the fullHeight parameter should be higher than the height parameter or it can't scroll. If I understood you correctly, then you want the actor to move with the camera in the x-direction, but when moving in y-direction, the camera shouldn't follow? If you use "move" or "setLocation", only the actor will move, not the camera, if you use 'moveCamera', then the camera will move and the actor (CameraFollower) follows. So you could move x and y independently (getting the dx and dy which depends on the rotation by using cosinus and sinus and use the different methods (setLocation/move vs. moveCamera) for both directions.
danpost danpost

2017/6/25

#
Basically, what Hippo is saying is to use setCameraDirection, setRotation and moveCamera for movement along the one axis and use just setRotation and move for the other. You will probably need to limit the vertical movement so that the actor does not leave the visible world.
Thanks @hippo and @danpost, your suggestions sound good, but how do I limit the vertical movement? Now I have the problem that the actor is leaving the visible world horizontal, because the camera doesn't move with it. Do you have an idea, how to fix this issue? I have now this code:
if(Greenfoot.isKeyDown("up"))
            setRotation(0);
            turn(-45);
            move(5);
            
        }
       
        if(Greenfoot.isKeyDown("down")){
          setRotation(0);
          turn(45);
          move(5);
        
        }

       
        if(Greenfoot.isKeyDown("right")){
            setRotation(0);
           getWorld().setCameraDirection(0);
          getWorld().moveCamera(5);

        }
Thanks
danpost danpost

2017/6/25

#
After moving, check the changed coordinate by comparing it to whatever limit you set for the direction moved. If passed the limit, move the actor back to that limit (using 'setLocation'). For example, to limit movement to the right so the actor does not go off the visible world:
// after moving right
if (getX() > getWorld().getWidth()-1) setLocation(getWorld().getWidth()-1, getY());
You can change the '1's to a larger value to put more of the actor in the world. Using 'getImage().getWidth()/2' instead of the '1's will place the actor at the edge and totally visible (no part of the actor passed the edge).
You need to login to post a reply.