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

2020/10/29

my platforms are acting funny

1
2
MrSkyPanda MrSkyPanda

2020/10/29

#
sometimes they land where i want them to land, sometimes they land either slightly above or below
int vSpeed = 1;
    int accel = 0;
    
    private int yPos = 0;
    
    private float jumpTime = 0;
    
    public void act() 
    {
        checkFalling();
        fall();
        setLocation(getX(), getY());
        
        
            
        
        if(Greenfoot.isKeyDown("w"))
            {
                setLocation(getX(), getY() -18);
                
                
            }
        if(Greenfoot.isKeyDown("a")) {
            move(-5);
        }        
        if(Greenfoot.isKeyDown("d")) {
        move(5);
        }   // Add your action code here
        
    }  
    public void checkFalling() //this is gravity
    {
        if(!isTouching(Ground.class))
        {
            vSpeed++;
        }
        else
        {
            vSpeed = 0;
        }
    }
    public void fall()
    {
        
        setLocation(getX(), getY() + vSpeed);

    }
danpost danpost

2020/10/29

#
Change line 1 to:
int vSpeed = 0;
// or
int vSpeed;
As field declaration lines, there is no difference between the two. Remove lines 2, 4 and 6 -- none of them are being used. Remove line 12 -- it places the actor at its current location (essential not doing a thing). Insert a line of code next to line 39 to place the actor "on" (but above, just barely not touching) the platform (Ground object).
MrSkyPanda MrSkyPanda

2020/10/29

#
thank you. but now there is a weird bug where if any of the character touches the platform, they will slowly start to float to the top of the platform
danpost danpost

2020/10/30

#
MrSkyPanda wrote...
thank you. but now there is a weird bug where if any of the character touches the platform, they will slowly start to float to the top of the platform
Show revised codes.
MrSkyPanda MrSkyPanda

2020/10/30

#
int vSpeed = 0;
    
    
    
    
    public void act() 
    {
        checkFalling();
        fall();
        
        
        
            
        
        if(Greenfoot.isKeyDown("w"))
            {
                setLocation(getX(), getY() -18);
                
                
            }
        if(Greenfoot.isKeyDown("a")) {
            move(-5);
        }        
        if(Greenfoot.isKeyDown("d")) {
        move(5);
        }   
        
    }  
    public void checkFalling() //this is gravity
    {
        if(!isTouching(Ground.class))
        {
            vSpeed++;
        }
        else
        {
            vSpeed = 0;
            setLocation(getX(), getY() - 1);
        }
    }
    public void fall()
    {
        
        setLocation(getX(), getY() + vSpeed);

    }
danpost danpost

2020/10/30

#
Line 38 only moves the actor up one cell, but when falling, it could be going much faster than one. Use the platform's y-position and actor heights to set the actor "on" the grounnd.
MrSkyPanda MrSkyPanda

2020/10/30

#
whats the method for that i cant find it. I've seen you use myHeight plenty of times, but is there a method for actor height?
danpost danpost

2020/10/30

#
MrSkyPanda wrote...
whats the method for that i cant find it. I've seen you use myHeight plenty of times, but is there a method for actor height?
It is the height of the actor's image:
int myHeight = this.getImage().getHeight();
MrSkyPanda MrSkyPanda

2020/10/30

#
ok, i dont know if i did it right but he still sometimes goes through the floor a little when i jump high enough.
int vSpeed = 0;
     private boolean LEFT = false;
     private boolean RIGHT = true;
     private boolean imageDir = RIGHT;
   
    
    
    
    public void act() 
    {
        checkFalling();
        fall();
        
        
            if(Greenfoot.isKeyDown("up")) 
            {
                setLocation(getX(), getY()-20);
              
            }
        if(Greenfoot.isKeyDown("right"))
        {
            
              setLocation(getX()+5, getY());
              if(imageDir == LEFT)  // if the actor is facing left, mirror to the right
              {
                  GreenfootImage image = getImage();
                  image.mirrorHorizontally();
                  setImage(image);
                  imageDir = RIGHT; // change direction so condition only runs when needed
              }
            
        }
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX() - 5, getY());
            if(imageDir == RIGHT)  // if actor is facing right, mirror to the left
            {
                getImage().mirrorHorizontally(); // works the same as above
                imageDir = LEFT;
            }
        }
        
    }  
    public void checkFalling() //this is gravity
    {
        if(!isTouching(Wide.class))
        {
            vSpeed++;
        }
        else
        {
            vSpeed = 0; 
            int myHeight = this.getImage().getHeight();
        }
            
    }
    public void fall()
    {
        
        setLocation(getX(), getY() + vSpeed);

    }
danpost danpost

2020/10/30

#
That one line of code at line 53 only gets the height of the player. It alone will not set the location of the actor appropriately. You still need the platform's location and its height. With that information, you can then determine what y-coordinate the actor should be at and set its location properly.
MrSkyPanda MrSkyPanda

2020/10/30

#
i tried putting an argument into the getHeight one but it said that i wasn't allowed to put and argument in between the parenthesis. so where do i need to put the code? does it even need to be in my players code?
danpost danpost

2020/10/30

#
MrSkyPanda wrote...
i tried putting an argument into the getHeight one but it said that i wasn't allowed to put and argument in between the parenthesis. so where do i need to put the code? does it even need to be in my players code?
Yes, it needs to be in your players code -- right along with line 53. You will have lines of code that do the following steps: (1) get (a reference to) intersecting platform; (2) get player's image height; (line 53) (3) get platforms y-coordinate; (4) get platform's image height; (5) calculate what should be new y-coordinate of player; (6) set location of player at that new y-coordinate; The calculation should be: <y-coordinate of platform> - <half of platform image height> - <half of player image height>-1 The "-1" at the end may or may not be needed; but that will give the y-coordinate of where the player needs to be put at.
MrSkyPanda MrSkyPanda

2020/11/2

#
so basically i just need getYPosition(or some method that does that) a couple of times in the right order. What are the methods i need to do this? i'm sorry if i'm asking a lot from you. I do better using scratch but it's always good to learn a new language of code every once in a while.
danpost danpost

2020/11/2

#
MrSkyPanda wrote...
so basically i just need getYPosition(or some method that does that) a couple of times in the right order. What are the methods i need to do this?
You have used it before. In fact, it is somewhere in this discussion. See lines 17, 23, 35 and 60.
MrSkyPanda MrSkyPanda

2020/11/2

#
do you think you could share a simple example?
There are more replies on the next page.
1
2