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

2016/4/30

Getting Actor to stop at Wall?

BOBBIE123 BOBBIE123

2016/4/30

#
Hello! So far, my actor rebounds from a wall if he JUMPS INTO it, however if he WALKS INTO it then he goes right through? Would you be able to help me to get him to rebound if he WALKS INTO it? Thanks! Here's the code having to deal with moving and jumping:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
ublic void act()
    {
        if(inTheAir)                //If we're in the air                
        {                          
            fall();                 //we'll fall
        } else                      //If we're not
        {                          
            if(Greenfoot.isKeyDown("left"))         //If they press the left key
            {
                deltaX = -5;                        //deltaX will move us to left
            } else if(Greenfoot.isKeyDown("right")) //If they press the right key
            {
                deltaX = 5;                         //deltaX will move us right
            } else                                  //If neither key is down
            {                              
                deltaX=0;                           //Don't move at all
            
 
            if(Greenfoot.isKeyDown("up"))           //If we press the up key
            {                                      
                deltaY+=5;                         //Add 15 to deltaY to jump
            }                                      
        }                          
        move();
    }
And the other one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public void move()
    {
        int newX = getX() + deltaX;     // Set the new x and y coordinates by adding or subtracting the deltaX
 
        int newY = getY() - deltaY;     // and deltaY. newX and newY may change if we're on the ground or a platform.
        Actor platformBelow = getOneObjectAtOffset(0, groundHeight+5, Platform.class);  //Look for a platform below us
        Actor platformAbove = getOneObjectAtOffset(0, -groundHeight-5, Platform.class); //Look for a platform above us
        Actor platformToRight = getOneObjectAtOffset(sideWidth+5, 0, Platform.class); //Look for a platform to right of us
        Actor platformToLeft = getOneObjectAtOffset(-(sideWidth+5), 0, Platform.class); //Look for a platform to left of us
        if(platformBelow!=null)         //If we find a platform below us
        {                              
            if(deltaY<0)                //If we are falling
            {                          
                deltaY=0;               //Stop falling
                inTheAir = false;       //Declare we're on the ground
                GreenfootImage platformImage = platformBelow.getImage();                //Get the image of the platform
                int topOfPlatform = platformBelow.getY()-platformImage.getHeight()/2;   //Find the top of platform
                newY = topOfPlatform - groundHeight;                                    //Put our feet at the top of the platform
            }            
        } else if(getY() >= worldHeight - groundHeight) {   //If we hit the ground...
            if(deltaY<0)                                    //and if we're falling...
            {
                deltaY=0;                                   //Stop falling
                inTheAir = false;                           //Declare we're on the ground
                newY = worldHeight - groundHeight;          //Put bottom of actor right on ground
            }
        else                          //If there's no platform below
        {                              
            inTheAir = true;            //Then we will be in the air
        }
        if (platformAbove!=null)        //If there's a platform above us
        {                              
            if(deltaY>0)                //If we're going up
            {                          
                deltaY=0;               //Stop going up
                GreenfootImage platformImage = platformAbove.getImage();                //Get the impage of the platform
                int bottomOfPlatform = platformAbove.getY()+platformImage.getHeight()/2;//Find the bottom of the platform
                newY = bottomOfPlatform + groundHeight;                                 //Put the top of our head at the bottom
            }                          
        }
        if(getX()<=sideWidth)
        {
            deltaX=Math.abs(deltaX);
        }
        if(getX()>=worldWidth-sideWidth)
        {
            deltaX=Math.abs(deltaX)*-1;
        }
        if (platformToRight != null)                            //If there's a platform to the right
        {                                                      
            deltaX = Math.abs(deltaX)* -1;                      //Bounce to the left    
        }                                                      
        if (platformToLeft != null)                             //If there is a platform to the right
        {                                                      
            deltaX = Math.abs(deltaX);                          //Bounce to the right
        }                                                      
        
danpost danpost

2016/4/30

#
Lines 10 through 30 deal with moving upward. Lines 31 through 40 deal with downward. There is no code to deal with moving left or right as yet. They would be coded somewhat similar to the upward, hitting platform code.
You need to login to post a reply.