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

2018/2/7

Play charater idle animation if there aren't any keys pressed

2
3
4
5
6
7
danpost danpost

2018/2/21

#
I think you will have to separate speed from direction so you can move by 'speed*direction' where direction is only one or negative one.
LaurentiuRohan LaurentiuRohan

2018/2/22

#
danpost wrote...
I think you will have to separate speed from direction so you can move by 'speed*direction' where direction is only one or negative one.
Yes, it works, thanks! I have a problem with player collecting coins. I want that Puppy collect coins every time that he touches one, but the problem is that he collect the coins only if he is jumping when he has the same position as coin! Please help me!
public void CollectCoins()
    {
        Actor Coin = getOneObjectAtOffset (0, getImage().getHeight(), Coin.class);
        if (isTouching(Coin.class))
       {
        World myWorld=getWorld();
       Greenfoot.setWorld(new Level1());
       myWorld.removeObject(Coin);
       coin.play();
       coin.stop();
       } 
    }
danpost danpost

2018/2/22

#
Remove line 7.
LaurentiuRohan LaurentiuRohan

2018/2/23

#
danpost wrote...
Remove line 7.
Thanks! Hou could I do in a simple way to move an object randomly on the Y axis?
danpost danpost

2018/2/23

#
LaurentiuRohan wrote...
Hou could I do in a simple way to move an object randomly on the Y axis?
Pick a random y value and save it in a field and turn the actor toward that location. If the actor is to change images for left and right, use the rotation to determine the image to set and what value, plus or minus one, to set to a direction field; then reset the rotation back to zero. To control movement, use a double condition check for getting a new y coordinate value to move to:
if (dir == -1 && getY() < targetY || dir == 1 && getY() > targetY)
    targetY = Greenfoot.getRandomNumber(getWorld().getHeight());
The top and bottom edges will be less traveled by the actor this way, so there is room for improving the code.
LaurentiuRohan LaurentiuRohan

2018/2/25

#
danpost wrote...
LaurentiuRohan wrote...
Hou could I do in a simple way to move an object randomly on the Y axis?
Pick a random y value and save it in a field and turn the actor toward that location. If the actor is to change images for left and right, use the rotation to determine the image to set and what value, plus or minus one, to set to a direction field; then reset the rotation back to zero. To control movement, use a double condition check for getting a new y coordinate value to move to:
if (dir == -1 && getY() < targetY || dir == 1 && getY() > targetY)
    targetY = Greenfoot.getRandomNumber(getWorld().getHeight());
The top and bottom edges will be less traveled by the actor this way, so there is room for improving the code.
Thanks a lot ! I've Another problem. I have 2 actors for swithcing level when he is at the end of it. 2 Actors, Door_Orc and Orc_Level_Locked. I want that Door_Orc be visible only if the actor collect an amount number of coins, else if coin number > 20 I want to be bisible Orc_Level_Locked, how could I do? I tried this in public void buildWorld but won't show any of them...
if(coins > 20) { addObject(new Orc_Level_Locked(),9544,333); } else if(coins == 20) { addObject(new Door_Orc(),9544,333); }
danpost danpost

2018/2/25

#
Sounds like the build of the world would already be complete before the number of coins would even start to increase. Move the 'if'-else if' codes to the 'act' method of the class (add one if not there, yet).
danpost danpost

2018/2/25

#
You might also want to include checks to see if the actors are not yet in the world before adding them (or you will get many of them -- one atop another.
LaurentiuRohan LaurentiuRohan

2018/2/26

#
danpost wrote...
Sounds like the build of the world would already be complete before the number of coins would even start to increase. Move the 'if'-else if' codes to the 'act' method of the class (add one if not there, yet).
Wll I tried to move in the act method but doesen't works.. And I have one more problem. I wanted to make rebuildWorld if puppy touches, intersecting Water.class and loose 1 life. When he is touching the water lifes are decreasing, world is rebuilding , but Puppy isn't spawned, I' ve tried almost 10 combinations to make it spawn, I moved/copied thi s4 lines code or/and only the first 2 lines into buildWorld, rebuildWorld, areaOne, to make him spawn, but at every try appear's another problem, there is no background, other actors aren't spawned correctcly and a few more.Can you tell me please how could I solve this problem and the one with doors? Those are the line I played with in the buildWorld, rebuildWorld, areaOne methodes.
setPaintOrder(Puppy_Player.class);
        setMainActor(new Puppy_Player(), 100, 342); 
        mainActor.setLocation(100, 342); 
        GreenfootImage bg = new GreenfootImage("Background1.png"); 
        setScrollingBackground(bg);
And this is the entire code of the level:
import greenfoot.*;
import java.util.List;
import java.util.ArrayList;
  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Mace_Level here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mace_Level extends Scrooling_Worlds
{
    private ScoreBoard scoreBoard;
    static GreenfootSound theme = new GreenfootSound("Mace Menu Theme.mp3");
    static GreenfootSound start = new GreenfootSound("Spawn Sound Effect.mp3");
    /**
     * Constructor for objects of class Mace_Level.
     * 
     */
    public Mace_Level()
    {
        super(960, 580, 1, 9600);
        setPaintOrder(Puppy_Player.class);
        setMainActor(new Puppy_Player(), 100, 342); 
        mainActor.setLocation(100, 342); 
        GreenfootImage bg = new GreenfootImage("Background1.png"); 
        setScrollingBackground(bg);
        lives = 3;
        ammunition = 0;
        coins = 0;
        enemies = 0;      
        if (!theme.isPlaying())
        {
            theme.playLoop();
        }
        buildWorld();
        if(!start.isPlaying())
        {
            start.play();
        }
         
        if(coins > 20)
        {
            addObject(new Orc_Level_Locked(),9544,333);
        }
        else if(coins == 20)
        {
            addObject(new Door_Orc(),9544,333);
        }
    }


    public void rebuildWorld()
    {
         
        if (lives < 0)
        {
            stopMusic();
            Greenfoot.setWorld(new GameOver_World());
        }
        ammunition = 0;
        List objects = getObjects(null);
        removeObjects(objects);
        buildWorld();
       
        
    }
    
    public void stopMusic()
    {
        theme.stop();
    }
    
    public void updateScoreboard()
    {
        scoreBoard.updateScore(lives, ammunition, coins, enemies);
    }
    
    private void addScoreboard()
    {
        scoreBoard = new ScoreBoard();
        addObject(scoreBoard, 71, 45, false);
        scoreBoard.updateScore(lives, ammunition, coins, enemies);
    }
    
    private void buildWorld()
    {
        
        startingArea();
        areaTwo();
        areaThree();
        areaFour();
        areaFive();
        addScoreboard();        
    }
    
    private void startingArea()
    {   
        addObject(new Long_Grass1(),237,551);
        addObject(new Grass_Normal_Left(),320,498);
        addObject(new Grass_Normal_Left(),379,442);
        addObject(new Grass_Normal_Center(),438,442);
        addObject(new Ground_Center(),320,557);
        addObject(new Ground_Center(),379,557);
        addObject(new Ground_Center(),379,499);
        addObject(new Ground_Right(),439,499);
        addObject(new Ground_Right(),439,557);
        
        addObject(new Water(),596,518);
        addObject(new Water(),849,518);
        
        addObject(new Coin(),123,495);
        addObject(new Coin(),232,495);
        addObject(new Coin(),325,444);
        addObject(new Coin(),415,386);
        addObject(new Coin(),558,312);
        addObject(new Coin(),558,312);
        addObject(new Coin(),681,252);
        addObject(new Coin(),801,199);
        addObject(new Coin(),925,128);
        addObject(new Coin(),1258,128);
        
        addObject(new Grass_Normal_Left(),560,368);
        addObject(new Grass_Normal_Left(),681,308);
        addObject(new Grass_Normal_Left(),802,258);
        addObject(new Grass_Normal_Right(),923,184);
        addObject(new Mace(),672,406);
        
        addObject(new Mace(),1848,406);

        
    }
    
    private void areaTwo()
    {
        addObject(new Water(),1102,518);
        addObject(new Water(), 1354-getScrolledX(), 518);
        addObject(new Water(), 1609-getScrolledX(), 518);
        addObject(new Water(), 1864-getScrolledX(), 518);
        
        addObject(new Long_Grass1(), 1260-getScrolledX(), 184);
        addObject(new Grass_Normal_Left(),1591,184);
        addObject(new Grass_Normal_Center(),1712, 258);
        addObject(new Grass_Normal_Center(),1833, 308);
        addObject(new Grass_Normal_Left(),1954, 368);
        addObject(new Grass_Normal_Left(),1954, 368);
        
        addObject(new Mace(),1255, 296);
        addObject(new Coin(),1950,312);
        addObject(new Coin(),1836,252);
        addObject(new Coin(),1703,199);
        addObject(new Coin(),1591,128);
    }
    
    private void areaThree()
    {
        addObject(new Long_Grass1(),2231,551);
        addObject(new Grass_Normal_Center(),2022,498);
        addObject(new Long_Grass1(),2229,551);
        
        addObject(new Coin(),2165,492);
        addObject(new Coin(),2165,492);
        addObject(new Coin(),2282,492);
        addObject(new Coin(),2403,492);
        
        addObject(new Mace(),2561,185);
        
        addObject(new Spike_Up(),2482,546);
        addObject(new Spike_Up(),2531,546);
        addObject(new Spike_Up(),2574,546);
        addObject(new Spike_Up(),2622,546);
        addObject(new Coin(),2654,416);
        addObject(new MovingX_Platform(),2603,467);
        
        addObject(new Spike_Up(),2675,546);
        addObject(new Spike_Up(),2726,546);
        addObject(new Spike_Up(),2772,546);
        addObject(new Spike_Up(),2826,546);
        
        addObject(new Coin(),2914,494);
        addObject(new Coin(),2914,494);
        addObject(new Coin(),3039,494);
        addObject(new Coin(),3039,494);
    }
    
    private void areaFour()
    {
        addObject(new Long_Ground1(),3088,551);
        
        addObject(new Coin(),3181,494);
        addObject(new Coin(),3291,436);
        
        addObject(new Ground_Center(),3296,493);
        addObject(new Ground_Center(),3354,493);
        
        addObject(new Coin(),3357,378);
        addObject(new Ground_Center(),3412,493);
        addObject(new Coin(),3416,324);
        
        addObject(new Ground_Center(),3353,437);
        addObject(new Ground_Center(),3412,437);
        addObject(new Ground_Center(),3412,379);
        addObject(new Ground_Center(),3354,551);
        addObject(new Ground_Center(),3354,551);
        addObject(new Ground_Center(),3412,551);
        addObject(new Ground_Center(),3472,551);
        addObject(new Ground_Center(),3532,551);
        addObject(new Ground_Center(),3532,551);
        
        addObject(new Coin(),3509,627);
        addObject(new Coin(),3587,251);
        
        addObject(new Ground_Center(),3591,551);
        addObject(new Ground_Center(),3650,551);
        addObject(new Ground_Center(),3710,551);
        addObject(new Ground_Center(),3770,551);
        addObject(new Ground_Center(),3770,551);
        addObject(new Ground_Center(),3786,497);
        addObject(new Ground_Center(),3727,497);
        addObject(new Ground_Center(),3786,551);
        addObject(new Ground_Center(),3726,439);
        
        addObject(new Coin(),3754,384);
        addObject(new Ground_Center(),3786,439);
        addObject(new Water(),3570,438);
        addObject(new Grass_Normal_Left(),3535,310);
        addObject(new Grass_Normal_Center(),3595,310);
        addObject(new Grass_Normal_Right(),3655,310);
        
    }
    
    private void areaFive()
    {
       addObject(new Spike_Up(),3859,539);
       
       
       addObject(new Spike_Up(),3944,539);
       addObject(new Spike_Up(),4029,539);
       addObject(new Spike_Up(),3944,539);
       addObject(new Spike_Up(),4114,539);
       addObject(new Spike_Up(),4199,539);
       addObject(new Spike_Up(),4284,539);
       addObject(new Spike_Up(),4369,539);
       addObject(new Spike_Up(),4454,539);
       addObject(new Spike_Up(),4539,539);
       addObject(new Spike_Up(),4539,539);
       addObject(new Spike_Up(),4624,539);
       addObject(new Spike_Up(),4709,539);
       addObject(new Spike_Up(),4794,539);
       addObject(new Spike_Up(),4879,539);
       addObject(new Spike_Up(),4964,539);
       
       /** Platforme Normale Jos*/
       addObject(new MovingX_Platform(),3970,408);
       addObject(new MovingX_Platform(),4401,408);
       addObject(new MovingX_Platform(),4832,408);
       addObject(new MovingX_Platform(),5233,408);
       addObject(new MovingX_Platform(),5634,408);
       addObject(new MovingX_Platform(),6035,408);
       addObject(new MovingX_Platform(),6436,408);
       addObject(new MovingX_Platform(),6837,408);
       addObject(new MovingX_Platform(),7238,408);
       addObject(new MovingX_Platform(),7639,408);
       addObject(new MovingX_Platform(),8040,408);
       
       
       /** Platforme Rapide Jos */
       addObject(new MovingXPlatform_Faster(),4190,282);
       addObject(new MovingXPlatform_Faster(),4621,282);
       addObject(new MovingXPlatform_Faster(),5052,282);
       addObject(new MovingXPlatform_Faster(),5483,282);
       addObject(new MovingXPlatform_Faster(),5914,282);
       addObject(new MovingXPlatform_Faster(),6345,282);
       addObject(new MovingXPlatform_Faster(),6776,282);
       addObject(new MovingXPlatform_Faster(),7207,282);
       addObject(new MovingXPlatform_Faster(),7638,282);
       addObject(new MovingXPlatform_Faster(),7638,282);
       addObject(new MovingXPlatform_Faster(),8069,282);
      
       /** Platforme Normale Sus */      
       addObject(new MovingX_Platform(),3970,162);
       addObject(new MovingX_Platform(),4401,162);
       addObject(new MovingX_Platform(),4832,162);
       addObject(new MovingX_Platform(),5233,162);
       addObject(new MovingX_Platform(),5634,162);
       addObject(new MovingX_Platform(),6035,162);
       addObject(new MovingX_Platform(),6436,162);
       addObject(new MovingX_Platform(),6837,162);
       addObject(new MovingX_Platform(),7238,162);
       addObject(new MovingX_Platform(),7639,162);
       addObject(new MovingX_Platform(),8040,162);
       
       addObject(new Mace(),5215,515);
       addObject(new Mace(),5837,515);
       addObject(new Mace(),6459,515);
       addObject(new Mace(),7081,515);
       addObject(new Mace(),7703,515);
       addObject(new Mace(),7703,515);
       
       addObject(new HighGround_Up(),8742,329);
       addObject(new Spike_Down(),8483,549);
       addObject(new Spike_Down(),8742,549);
       addObject(new Spike_Down(),8483,549);
       addObject(new Spike_Down(),8999,549);
        
       addObject(new Spike_Up(),8740,340);
       addObject(new Spike_Up(),8740,300);
       addObject(new Spike_Up(),8740,260);
       addObject(new Spike_Up(),8740,220);
       addObject(new Spike_Up(),8740,180);

        addObject(new Coin(),8482,122);
        addObject(new Coin(),8596,122);
        addObject(new Coin(),8754,61);
        addObject(new Coin(),8855,122);
        addObject(new Coin(),8974,122);
        
        addObject(new Ground_Left(),9178,28);
        addObject(new Ground_Left(),9178,86);
        addObject(new Ground_Left(),9178,144);
        addObject(new Ground_Center(),9235,144);
        addObject(new Ground_Center(),9285,144);
        addObject(new Ground_Center(),9345,144);
        addObject(new Ground_Center(),9405,144);
        addObject(new Ground_Center(),9465,144);
        addObject(new Ground_Center(),9525,144);
       
        addObject(new Mace(),9286,54);
        
        addObject(new MovingX_Platform(),9146,356);
        addObject(new MovingXPlatform_Faster(),9146,430);
        
        addObject(new Grass_Normal_Left(),9508,418);
        addObject(new Grass_Normal_Right(),9567,418);
        addObject(new Door_Orc(),9544,333);
        
       addObject(new Spike_Up(),5049,539);
       addObject(new Spike_Up(),5134,539); 
 
    }        
}
And this one for the player when touches the water in his act method :
  if(getOneObjectAtOffset (0, getImage().getHeight()/2 - 2, Water.class)!= null)
       {
         Mace_Level Mace_Level = (Mace_Level)getWorld();
         Mace_Level.decreaseLives();
         Mace_Level.rebuildWorld();
       } 
danpost danpost

2018/2/26

#
Do not duplicate those 4 lines anywhere else. Just replace line 64 in the 'rebuildWorld' method with the following lines:
int maX = mainActor.getX();
int maY = mainActor.getY();
removeObjects(objects);
addObject(mainActor, maX, maY);
LaurentiuRohan LaurentiuRohan

2018/2/26

#
danpost wrote...
Do not duplicate those 4 lines anywhere else. Just replace line 64 in the 'rebuildWorld' method with the following lines:
int maX = mainActor.getX();
int maY = mainActor.getY();
removeObjects(objects);
addObject(mainActor, maX, maY);
I done like you've told me but now the terminal window appear's when Puppy touches the water.Class....
java.lang.NullPointerException
	at greenfoot.World.addObject(World.java:412)
	at Scrooling_Worlds.addObject(Scrooling_Worlds.java:144)
	at Scrooling_Worlds.addObject(Scrooling_Worlds.java:150)
	at Mace_Level.rebuildWorld(Mace_Level.java:68)
	at Puppy_Player.act(Puppy_Player.java:65)
	at greenfoot.core.Simulation.actActor(Simulation.java:604)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
	at greenfoot.core.Simulation.runContent(Simulation.java:221)
	at greenfoot.core.Simulation.run(Simulation.java:211)
danpost danpost

2018/2/26

#
LaurentiuRohan wrote...
I done like you've told me but now the terminal window appear's when Puppy touches the water.Class.... << Terminal Output Omitted >>
Okay. Try replacing with this instead:
objects.remove(mainActor);
removeObjects(objects);
LaurentiuRohan LaurentiuRohan

2018/2/26

#
danpost wrote...
LaurentiuRohan wrote...
I done like you've told me but now the terminal window appear's when Puppy touches the water.Class.... << Terminal Output Omitted >>
Okay. Try replacing with this instead:
objects.remove(mainActor);
removeObjects(objects);
Well now, no more terminal windows but he is spawing at antoher X position (438 instead 100), Y postion is still the same. And the objects are spawning incorrectly...I think that their spawn is determined by how much window has scrolled before rebuildWorld to be initialised. This how the rebuildWorld looks like now :
public void rebuildWorld()
    {
         if (lives < 0)
        {
            stopMusic();
            Greenfoot.setWorld(new GameOver_World());
        }
        ammunition = 0;
        List objects = getObjects(null);
        objects.remove(mainActor);
        removeObjects(objects);
        buildWorld();
    }
danpost danpost

2018/2/26

#
LaurentiuRohan wrote...
the objects are spawning incorrectly...I think that their spawn is determined by how much window has scrolled before rebuildWorld to be initialised.
Yes, if you want to place actors into the world with respect to the scrolling area when the scrolled amount is not zero then you must incorporate the scrolled amounts in the location coordinates at which the actor is to be placed. So all x-coordinate parameters in all your build methods (except the first as the scrolled amount is zero at the time it is executed) need the value returned by 'getScrolledX()' subtracted from them.
he is spawing at antoher X position (438 instead 100), Y postion is still the same.
Everything should at this point spawn correcly with respect to the main actor, If you want, you could set its location to 100 any time in the 'rebuildWorld' method':
mainActor.setLocation(100, mainActor.getY());
LaurentiuRohan LaurentiuRohan

2018/2/26

#
So the reason wich for my actors aren't spawned correctly is that's why because I didn't set their x coordinates correct? I mean, I've seen when the level start's for the first time before touching the water, If I click Inspect on Puppy it say's that his position is 100 and after that he spawn he is at 438, but after rebuilding in the left side is almost 200 to the begining of the level, and that area is empty. I am a little bit confused. You said that if I want to place actors into the world with respect to the scrolling area when the scrolled amount is not zero then I must incorporate the scrolled amounts in the location coordinates at which the actor is to be placed . Well, after I built the firstarea of the level I was going with the actor to scroll the world until the objects from area one weren't visible so then I was adding a new actor I was checking it's X position then I was checking scrolledX amount with right click on the background, So x position of the actor is equal with Xposition of the actor + scrolledX, right? But I'm still confused why in the first spawn on the level before puppy lose any life, objects are swpaned correctly from the begining of the left side, and after rebuilding the world, there is that little empty space 250-300... Is something wrong with the scrolling or the objects really aren't spawned correctly. Please help!
There are more replies on the next page.
2
3
4
5
6
7