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

2013/3/3

Animating an object

1
2
Gingervitis Gingervitis

2013/3/3

#
You are a genius! How do you know how to do all this?
Gingervitis Gingervitis

2013/3/3

#
Yes. I'm sorry, I should have been more specific. I did mean the rocket in the MainMenu world. ( I added a separate class for the image changing so rocket doesn't exist in the MainMenu world anymore)
danpost danpost

2013/3/3

#
All I am doing is creating code to make it do what I want it to do, when I want it to do it, step by step. Ok. I still need to show you my two world class codes. The Intro world has the delay for after the Rock has landed and I decided to do the platform animation directly on the background of the Menu (MainMenu) world. Here they are:
 // the Intro world class code
import greenfoot.*;

public class Intro extends World
{
    private int groundedTimer;
    
    public Intro()
    {    
        super (600, 500, 1);
        setPaintOrder(Rock.class, Rocket.class);
        addObject(new Rocket(), 203, 441);
        addObject(new Rock(),10 ,10 );
    }
    
    public void act()
    {
        timerOne();
    }
    
    private void timerOne()
    {
        if (groundedTimer<100 && ((Rock)getObjects(Rock.class).get(0)).isGrounded())
        {
            groundedTimer++;
            if (groundedTimer==100)
            {
                addObject(new Turtle(), 203, 441);
                addObject(new Skip(), 493, 132);
            }
        }
    }
}

// the Menu (MainMenu) world class code
import greenfoot.*;
import java.awt.Color;

public class Menu extends World
{
    private int platformSize;
    
    public Menu()
    {    
        super(600, 500, 1);
        getBackground().setColor(Color.darkGray);
        getBackground().fillOval(260, 211, 88, 82);
    }
    
    public void act()
    {
        if(platformSize<215)
        {
            platformSize++;
            GreenfootImage platform = new GreenfootImage(2+2*platformSize/5, 2+2*platformSize/5);
            platform.setColor(Color.lightGray);
            platform.fillOval(0, 3, 2*platformSize/5, 2*platformSize/5-6);
            getBackground().drawImage(platform, 304-platformSize/5, 252-platformSize/5);
        }
    }            
}
danpost danpost

2013/3/3

#
I do not know if the I have the correct color changing in the Menu world (whether it goes from darkGray to lightGray, or the other way around). I will post an updated version of the Menu class after I add the turtle to it.
Gingervitis Gingervitis

2013/3/3

#
I haven't added the MainMenu code yet, but when I added the Intro code, it created two rocks and two turtles, should I take out the code from the actor classes that creates those objects?
Gingervitis Gingervitis

2013/3/3

#
The code in the MainMenu is perfect!!
danpost danpost

2013/3/3

#
You should not be mixing my code with your code. And if you are using any of my code, use all of my code and none of yours. I had to adjust the Turtle class code to accommodate the new world, so here is an updated Turtle class plus the revised Menu world:
// the adjusted Turtle class code
import greenfoot.*;

public class Turtle extends Actor
{
    private int timer = 100;
    
    public void act()
    {
        if(getWorld() instanceof Intro)
        {
            if(timer==0)
            {
                move(2);
                if(getRotation()==0 && getX()>=355) timer = 136;
                if(getRotation()==180 && getX()<=202) Greenfoot.setWorld(new Menu());
            }
            else
            {
                timer--;
                if(getX()>=355 && timer<36) setRotation(getRotation()-5);
            }
        }
    }
}

// the revised Menu class code with the turtle on the platform
import greenfoot.*;
import java.awt.Color;

public class Menu extends World
{
    private int platformSize;
    private Turtle turtle = new Turtle();
    private GreenfootImage turtleImage;
    
    public Menu()
    {    
        super(600, 500, 1);
        getBackground().setColor(Color.darkGray);
        getBackground().fillOval(260, 211, 88, 82);
        turtleImage = turtle.getImage();
        addObject(turtle, 306, 252);
    }
    
    public void act()
    {
        if(platformSize<215)
        {
            platformSize++;
            GreenfootImage platform = new GreenfootImage(2+2*platformSize/5, 2+2*platformSize/5);
            platform.setColor(Color.lightGray);
            platform.fillOval(0, 3, 2*platformSize/5, 2*platformSize/5-6);
            getBackground().drawImage(platform, 304-platformSize/5, 252-platformSize/5);
            GreenfootImage turtleImg = new GreenfootImage(turtleImage);
            double factor = (double)(2+2*platformSize/5)/86;
            turtleImg.scale((int)(factor*62), (int)(factor*46));
            turtle.setImage(turtleImg);
        }
    }            
}
Gingervitis Gingervitis

2013/3/3

#
Thank you so much! I know I ask a lot questions, but that is how I learn the most, but is there a way to make it so you go into a world, then go to a different world, but then go back to the first world, but the world you went back to is different. I know this kind of confusing so I will explain what I am trying to do. I want to make it so every time you beat a level, you go back to the main menu but the next level is unlocked.
danpost danpost

2013/3/3

#
You can pass the MainMenu world object to each level as you create them, like
Greenfoot.setWorld(new Level1(this)); // from the world class, or
Greenfoot.setWorld(new Level1((MainMenu)getWorld())); // from an actor class
Then, in each level:
// add this instance field
private MainMenu mainMenu;

// add a secondary constructor
public MainMenu(MainMenu world)
{
    this(); // calls the main constructor
    mainMenu = world; // saves the menu world
}

// to return to the saved MainMenu world
Greenfoot.setWorld(mainMenu); // from the world class
To return from an actor class (which is probably where you will determine when it is time to return), it would be best to super class all your level worlds with the same 'Level' world and put the mainMenu variable in that world. This way you know the name of the world that contains the variable and can simply code the following (without having to check all the level classes to see which class is currently active):
Greenfoot.setWorld(((Level)getWorld()).getMenuWorld());
You will have to add the following method to the 'Level' world class code:
public MainMenu getMenuWorld()
{
    return mainMenu;
}
Gingervitis Gingervitis

2013/3/3

#
That is kinda confusing, so when I create level 1 and when I want to go back to the main menu I will try what you said. Do you think you could try explaining it again?
danpost danpost

2013/3/3

#
Line 5 in my last code post should read
public Level1(MainMenu world)
Ask about explaining it again when you get to that.
Gingervitis Gingervitis

2013/3/3

#
Ok. It might be a little while before I ask you to explain it again because I haven't thought about what to do for my game.... I am creating it as I go along....
laziaf laziaf

2013/3/12

#
@danpost, i have trouble. I made a game using a countdown timer using Edit> import class> counter, a maximum of 50 to 0, and if up to 0 game over. the problem is, time is running very fast, and I do not know how to slow down time as a matter of seconds. I need your help. thanks
steved steved

2013/3/12

#
@laziaf you should probably start a new topic you know. It would be alot more effective...
You need to login to post a reply.
1
2