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

2017/2/8

Combat system

AuroraGamer AuroraGamer

2017/2/8

#
So, I'm trying to work out a couple issues right now, 1 I'm trying to make it so that when you attack an enemy it doesn't die right away, I would like it to be liek you have an 'attack strength' that goes up with you level (that's issue 2) but, obviously I'm not really sure how to do that. 2, a way to level up every time you kill, let's say, ten enemies, I have a set of rose images, that every two enemies you kill the image changes, slwly blooming, then starting over in a different color, I sort of have that part working, but currently it just keeps going, not just when an enemy dies. Before anyone asks for code, The part for the rose changing image is a modified imported counter. I don't have any code for the first issue.
davmac davmac

2017/2/8

#
I don't have any code for the first issue.
Ok, then read this: general help for questions. Have you learnt the Java basics? What variables, methods, classes will you need? Have a think about it and explain your thoughts. Then ask questions specific to the parts that you can't do. You should be able to write a little bit of code, even if it's not fully functional or has errors, before you ask for questions. Right now your question is too vague. I'd have to write pages to comprehensively answer it.
Before anyone asks for code, The part for the rose changing image is a modified imported counter
Ok, but you still need to post the code...
AuroraGamer AuroraGamer

2017/2/8

#
Ok, sorry, it's not so much not having an idea of how it would work as much as just, not knowing how to put that into words that another human being would understand. I'll try to think about the other part a little bit more and get back to it. This is the counter code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A Counter class that allows you to display a numerical value on screen.
 * 
 * The Counter is an actor, so you will need to create it, and then add it to
 * the world in Greenfoot.  If you keep a reference to the Counter then you
 * can adjust its value.  Here's an example of a world class that
 * displays a counter with the number of act cycles that have occurred:
 * 
 * <pre>
 * class CountingWorld
 * {
 *     private Counter actCounter;
 *     
 *     public CountingWorld()
 *     {
 *         super(600, 400, 1);
 *         actCounter = new Counter("Act Cycles: ");
 *         addObject(actCounter, 100, 100);
 *     }
 *     
 *     public void act()
 *     {
 *         actCounter.setValue(actCounter.getValue() + 1);
 *     }
 * }
 * </pre>
 * 
 * @author Neil Brown and Michael Kölling 
 * @version 1.0
 */
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    private String prefix;
    
    public Counter()
    {
        this(new String());
    }

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        this.prefix = prefix;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
        
        if (value <= 5)
        {
           setImage("LevelOneRose"+ value +".png");
        }
        else if (value >= 6 && value <= 10)
        {
            setImage("LevelTwoRose"+ value +".png");
        }
        else if (value >= 11 && value <= 15)
        {
            setImage("LevelThreeRose"+ value +".png");
        }
        else if (value >= 16 && value <= 20)
        {
            setImage("LevelFourRose"+ value +".png");
        }
        else if (value >= 21 && value <= 25)
        {
            setImage("LevelFiveRose"+ value +".png");
        }
        else if (value >= 26 && value <= 30)
        {
            setImage("LevelSixRose"+ value +".png");
        }
        else if (value >= 31 && value <= 35)
        {
            setImage("LevelSevenRose"+ value +".png");
        }
        else if (value >= 36 && value <= 40)
        {
            setImage("LevelEightRose"+ value +".png");
        }
        else if (value >= 41 && value <= 45)
        {
            setImage("LevelNineRose"+ value +".png");
        }
        else if (value >= 56 && value <= 50)
        {
            setImage("LevelTenRose"+ value +".png");
        }
        else if (value >= 50)
        {
            setImage("LevelTenRose50.png");
        }
    }

    /**
     * Add a new score to the current counter value.  This will animate
     * the counter over consecutive frames until it reaches the new value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return target;
    }

    /**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
    
    /**
     * Sets a text prefix that should be displayed before
     * the counter value (e.g. "Score: ").
     */
    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + value , 22, Color.BLACK, transparent);
        
        if (text.getWidth() > image.getWidth() - 20)
        {
            image.scale(text.getWidth() + 20, image.getHeight());
        }
        
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
This is the world code,
import greenfoot.*;

/**
 *
 */
public class LevelOne extends SWorld
{
    private int timer = 0;
    Counter counter = new Counter("");
    private int levelUp;
    /**
     * Creates a scrolling world using a main actor, a background, some obstacles, and a non-scrolling score.
     */
    public LevelOne()
    {    
        super(1000, 600, 1, 2000); // scroll world constructor call; last parameter is scroll width
        // in the following statement, the main actor is placed in the center of the window
        setMainActor(new Girl(), 250, 300); // the int parameters are centered window x and y ranges
        // to start the main actor elsewhere
        mainActor.setLocation(100, 342);
        GreenfootImage bg = new GreenfootImage("Background1.jpeg");
        setScrollingBackground(bg); // set the scolling background image
        // add other scrollable objects normally
        //''addObject(new Ground(), 248, 607);''
        // use of the following also adds scrollable objects
        //''addObject(new Box(), 380, 345, true);'' // the boolean determines scrollable state
        // use the following for non-scrollable objects
        //''addObject(new Score(), 40, 390, false);''
        addObject(counter, 66, 106, false);
    }

    public void act()
    {
        if (getObjects(Enemy.class).isEmpty())
        {
            timer++;
            if(timer == 1)
            {
               // killCount();
            }

            if (timer >= 100)
            {                
                //addObject(counter2, 400, 350);
                addObject(new Enemy(), 400, 400);
                timer = 0;
            }
        }
        if (levelUp++ == 20)
            {
                killCount();
                levelUp = 0;
            }
    }

    public void killCount()
    {
        counter.add(1);
    }

}
danpost danpost

2017/2/8

#
Lines 49 in your LevelOne class is executed every act cycle. Nothing is restricting the incrementing of 'levelUp' there. So, every third of a second or so, the counter will be added to without regard to anything else. That is the explanation for the continuing counter. How to fix it is not really clear because of the lack of detail in what you actually want. Obviously, you are counting 20 of something and they are not supposed to be act cycles -- but, what they are is unclear (and 20 is not 10, which you stated were the number of enemies killed before leveling up -- a bit confusing!).
You need to login to post a reply.