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

2022/6/12

i want to increase the speed of an actor every time it spawns

KCee KCee

2022/6/12

#
I have a fishing game where theres a bubble image moving towards you and when you reel in your fishing rod while touching the bubble, you catch a fish and the bubble respawns at its initial spawnpoint. To increase the difficulty I want the bubble to move slightly faster each time its respawned. How would I do this? relevant code for fisherman class:
    boolean reeling()
    {
        if (currAnim == reelLeft || currAnim == reelRight)
        {
            setImage();
            if(animTimer == 0)
            {
                setAnimation(currAnim == reelLeft ? walkLeft : walkRight);
                if (isTouching(Bubbles.class))
                {
                    ((MyWorld)getWorld()).getFish();
                    removeTouching(Bubbles.class);
                    MyWorld world = (MyWorld) getWorld();
                    world.spawnBubbles();
                }
            }
            else
            {
                return true;
            }
        }
relevant code for world:
public void spawnBubbles()
    {
        Bubbles b = new Bubbles();
        int x = 500;
        int y = 290;
        addObject (b, x, y);
    }
Bubble class:
public class Bubbles extends Actor
{
    /**
     * Act - do whatever the bubbles wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    GreenfootImage bubble = getImage();
    int speed = 10;
    int timer;
    
    public void Bubbles()
    {
        bubble.scale(1, 1);
    }
    
    public void act()
    {
        int x = getX();
        int y = getY();
        if(++timer % speed == 0)
        {
            setLocation(x - 5, y);
        }
        
        MyWorld world = (MyWorld)getWorld();
        
        if(getY() >= world.getWidth())
        {
           world.gameOver();
        }
        
    }
}
Spock47 Spock47

2022/6/12

#
You could keep track of the bubble speed in the world when spawning a new bubble:
    private int bubbleSpeed = 10;
    public void spawnBubbles()
    {
        Bubbles b = new Bubbles();
        int x = 500;
        int y = 290;
        b.speed = bubbleSpeed;
        ++bubbleSpeed;
        addObject (b, x, y);
    }
But there is a problem in your source code: The attribute speed in class Bubbles is actually not a speed, it is the opposite of speed. If you increase the value of Bubbles::speed, the bubble will move less often; so it results in a smaller actual speed. So, you definitely want to change your source code first in a way that the name of the attribute and its actual meaning match each other: higher value of the speed attribute should result in faster/more movement per time.
You need to login to post a reply.