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

2013/6/25

Moving Hero with platform

davemib123 davemib123

2013/6/25

#
I have two floating platforms. The first is horizontally, the second is vertically. For the horizontal platform how do I keep the Hero moving with the platform. For the vertical, the Hero initiates the fall command - how do I stop that from happening? My scenario is located here: http://www.greenfoot.org/scenarios/8881
danpost danpost

2013/6/25

#
In both cases, when moving the platform, move the actor (if the actor is standing on it).
davemib123 davemib123

2013/6/25

#
So do I create a variable in the Hero class to get the X and Y location. Then create an if-statement in the moving platform classes to check if the hero is standing on it or not?
danpost danpost

2013/6/25

#
No. Just check for the Hero in the Platform class in the method that moves the platform and move the hero the same distance in the same direction as the platform moves.
davemib123 davemib123

2013/6/25

#
I have this at the moment, what am I missing:
public void act() 
    {
        FPlatformCounter++;
        moveHFPlatform();
        if (FPlatformCounter <=100 && getOneIntersectingObject(Mario.class)  != null)
        {
            setLocation(Mario.MarioX - speed, getY());
        }
        if(FPlatformCounter > 101 && FPlatformCounter <=200 && getOneIntersectingObject(Mario.class)  != null)
        {
            setLocation(Mario.MarioX + speed, getY());
        }
    }  
Zamoht Zamoht

2013/6/25

#
Add this method to VFPlatform. This will work with the version of your scenario that is uploaded to the side, so if you have made a lot of changes since that I can't promise that it'll work.
public void moveDown()
    {
        setLocation(getX(), getY() - 1); //Moves the platform up to check for Mario.
        Actor mario = getOneIntersectingObject(Mario.class);
        setLocation(getX(), getY() + 1 + speed);
        if (mario != null)
        {
            mario.setLocation(mario.getX(), mario.getY() + speed);
        }
    }
This will fix one of your problems and maybe you can use it to fix the other problems as well.
davemib123 davemib123

2013/6/26

#
Thanks Zamoht, it worked great. I've tried these two combinations for horizontal platform, but it doesnt work: Combination 1:
public void moveHFPlatform()
    {
        if(FPlatformCounter <=100 )
        {
            Actor mario = getOneIntersectingObject(Mario.class);  
            moveLeft();
           if (mario != null)  
            {  
                mario.setLocation(mario.getX() - speed, mario.getY() );  
            } 
            FPlatformCounter++;
        }
        if(FPlatformCounter > 101 && FPlatformCounter <=200  )
        {
            Actor mario = getOneIntersectingObject(Mario.class);
            moveRight();
            if (mario != null)  
            {  
                mario.setLocation(mario.getX() + speed, mario.getY() );  
            } 
            FPlatformCounter++;
        }
        if(FPlatformCounter >=  201)
        {
            FPlatformCounter = 0;
        }
    }
Combination 2:
  public void moveHFPlatform()
    {
        if(FPlatformCounter <=100 )
        {
            setLocation(getX() + 1, getY() ); 
            Actor mario = getOneIntersectingObject(Mario.class);  
            setLocation(getX() - 1 - speed, getY() );  
           if (mario != null)  
            {  
                mario.setLocation(mario.getX() - speed, mario.getY() );  
            } 
            FPlatformCounter++;
        }
        if(FPlatformCounter > 101 && FPlatformCounter <=200  )
        {
               setLocation(getX() - 1, getY() ); 
            Actor mario = getOneIntersectingObject(Mario.class);  
            setLocation(getX() + 1 + speed, getY() );  
            if (mario != null)  
            {  
                mario.setLocation(mario.getX() + speed, mario.getY() );  
            } 
            FPlatformCounter++;
        }
        if(FPlatformCounter >=  201)
        {
            FPlatformCounter = 0;
        }
    }
Any ideas?
Zamoht Zamoht

2013/6/26

#
Okay when I said that you could use my method for solving the other problems this was the outcome I was hoping to see: Add these methods to HFPlatform
    /**
     *Moving an object to the right 
     */
    public void moveRight()
    {
        Actor mario = checkForMario();
        if (mario != null)
        {
            mario.setLocation(mario.getX() + speed, mario.getY());
        }
        super.moveRight();
    }

    /**
     *Moving an object to the left 
     */
    public void moveLeft()
    {
        Actor mario = checkForMario();
        if (mario != null)
        {
            mario.setLocation(mario.getX() - speed, mario.getY());
        }
        super.moveLeft();
    }
    
    public Actor checkForMario()
    {
        setLocation(getX(), getY() - 1); //Moves the platform up to check for Mario.  
        Actor mario = getOneIntersectingObject(Mario.class);  
        setLocation(getX(), getY() + 1);
        return mario;
    }
It does the same as before, first it checks for mario by moving up, and then it moves mario with it's own speed. Though you were close with your combination 2. The problem is that you move the platforms right or left and then checks for mario, which will return null since mario is on top of the platform. So if you don't want to change your moveLeft and moveRight methods like I did you can fix your combination 2 like this:
public void moveHFPlatform()
    {  
        if(FPlatformCounter <=100 )  
        {  
            setLocation(getX(), getY() - 1);   
            Actor mario = getOneIntersectingObject(Mario.class);   
            setLocation(getX(), getY() + 1);
            moveLeft();  
            if (mario != null)    
            {    
                mario.setLocation(mario.getX() - speed, mario.getY() );    
            }   
            FPlatformCounter++;  
        }  
        if(FPlatformCounter > 101 && FPlatformCounter <=200  )  
        {  
            setLocation(getX(), getY() - 1);   
            Actor mario = getOneIntersectingObject(Mario.class);   
            setLocation(getX(), getY() + 1);    
            moveRight();  
            if (mario != null)    
            {    
                mario.setLocation(mario.getX() + speed, mario.getY() );    
            }   
            FPlatformCounter++;  
        }  
        if(FPlatformCounter >=  201)  
        {  
            FPlatformCounter = 0;  
        }  
    } 
davemib123 davemib123

2013/6/26

#
Thanx Zamoht it works fantastically.
Zamoht Zamoht

2013/6/26

#
No problem, I really like your scenario.
You need to login to post a reply.