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

2014/12/1

Help with slowmotion timer!

gamer98 gamer98

2014/12/1

#
Im just a beginner, so sorry if its a stupid question being asked. I want the slowmotion to be activated when the slowmo object collides with the player object. This is my code for this. When the 2 objects collide the the slowmo object gets removed and the game slows down but after a certain time the game is not getting back to the original speed. Any help is appreciated. Thanks in Advance
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SlowMotion here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SlowMotion extends Actor
{
    public int slowtime = 5;
    boolean slowmotion = false;
    /**
     * Act - do whatever the SlowMotion wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkForSlowmo();
        slowmotionspeed();
    }
    
    public void checkForSlomo()
    {
        if (getOneIntersectingObject(Player.class) != null)
        {
            slowmotion = true;
            getWorld().removeObject(this);
            slowtime--;
            if (slow < 0)
            {
                slowmotion = false;
                slow = 50;
                
            
            }
            
        }
    }
    
    public void slowmotionspeed()
    {
        if (slowmotion = true)
        {
            Greenfoot.setSpeed(40);
        }else 
        {
            Greenfoot.setSpeed(50);
        
        }
    }
   
    
}
gamer98 gamer98

2014/12/1

#
Line 33 is meant to say slowtime = 5
danpost danpost

2014/12/1

#
It appears you are removing the actor from the world when the slowmotion starts. It cannot act if it is not in the world, and therefore, the code to deactivate the slowmotion will never be executed.
gamer98 gamer98

2014/12/1

#
Could you show me how to correct this? Sorry i am only learning and understanding the concepts and just a beginner. Thanks
danpost danpost

2014/12/1

#
It appears this is a 'powerdown' (as opposed to a 'powerup'. Once this has been contacted by the player, it will be removed from the world. It stands to reason, then, that the slowmotion code cannot be placed in this class. If it is to influence every actor in your world, then the code should probably be placed in your world class. If is to influence only the actor that contacted it, then the slowmotion code should probably be in the class of that actor.
Super_Hippo Super_Hippo

2014/12/1

#
As it is right now (even if it would not be removed), it wouldn't work as I think you want it. If the Player touches the SlowMotion object, the slow-motion would start. If you move away from the SlowMotion object before 5 act cycles, the slow-motion will never stop. If you stay on the SlowMotion, it won't stop either. I will be set to default speed, but in the next act cycle (if the player still touches the SlowMotion), it is set to 40 again. Probably the easiest way to achieve what you want is to change a Boolean in the world class.
public class SlowMotion extends Actor
{
    public void act()
    {
        if (getOneIntersectingObject(Player.class) != null)
        {
            ((/*WorldClassName*/)getWorld()).activateSlowmotion();
            getWorld().removeObject(this);
        }
    }
}
//in the world class

private boolean slow=false;
private int t_slow=0;

public void act()
{
    if (slow)
    {
        t_slow--;
        if (t_slow<0)
        {
            deactivateSlowmotion();
        }
    }
}            

public void activateSlowmotion()
{
    slow = true;
    t_slow = 100; //I am not sure if 5 was a good idea
    Greenfoot.setSpeed(40);
}

public void deactivateSlowmotion()
{
    slow = false;
    Greenfoot.setSpeed(50);
}
gamer98 gamer98

2014/12/1

#
Thank you soo much, it fixed the problem.
You need to login to post a reply.