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

2014/1/10

limit my actors movement

1
2
3
4
5
6
danpost danpost

2014/1/13

#
As long as all your class names and field names are spelt correctly and in the right place. In your last code post, you have 'MainWorld' (with an uppercase 'W') in line 10, but in line 5 you use a lowercase one (the first 'Mainworld'). One is correct for your class name (I would hope) and one is definitely not (these two should match exactly).
danpost danpost

2014/1/13

#
Also, in line 5 you have the field name 'mainworld' with a lowercase 'w'; in line 15 you have 'mainWorld' with an uppercase 'W'. These two should also match (use 'mainWorld'). And another? in line 15 you have the field name 'World' and in line 10 you have 'world; these also should match (use 'world').
NickyL212 NickyL212

2014/1/13

#
Thanks for pointing that 1 out! But it still gives a problem at my 'Back' actor. It keeps telling me its not a statement.
    public class Back extends Actor  
    {  
        /** 
         * Act - do whatever the Back wants to do. This method is called whenever 
         * the 'Act' or 'Run' button gets pressed in the environment. 
         */  
      
        public void act()    
        {    
            if (Greenfoot.mouseClicked(this))   
            {  
                      Greenfoot.setWorld((MenuWorld)getWorld().MainWorld);    
            }  
        }    
      
    }  
public class MenuWorld extends World
{
        
  
    public MainWorld mainWorld;
    /**
     * Constructor for objects of class MenuWorld.
     * 
     */
    public MenuWorld(MainWorld world) 
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        
        super(760, 760, 1);
        mainWorld = world; 
        addObject(new Menu(), 380, 380);  
        addObject(new Back(), 230, 520);  
    }  
} 
danpost danpost

2014/1/13

#
The field name in the MenuWorld class is not 'MainWorld' as you show in line 12 of the Back class; it is 'mainWorld'.
NickyL212 NickyL212

2014/1/13

#
It says it cant find the variable 'mainWorld'. Did i missed an instance that i need to add in my 'back' actor?
danpost danpost

2014/1/13

#
No. I just realized you were missing a set of parenthesis. It should be:
Greenfoot.setWorld(((MenuWorld)getWorld()).mainWorld);
NickyL212 NickyL212

2014/1/13

#
Thanks danpost. for now i got a game where i got a plane that flies around a bit and the game is over when his health reaches zero. The next thing i try to add is an 'score' counter where my plain gains like 100 'score' each 10 seconds it survives. But i also want to use score i builed to buy the 'shield' i mentioned earlier. Is it possible to create such a counter in greenfoot? a score that is deductible if you buy the shield in short really..
danpost danpost

2014/1/13

#
NickyL212 wrote...
The next thing i try to add is an 'score' counter where my plain gains like 100 'score' each 10 seconds it survives. But i also want to use score i builed to buy the 'shield' i mentioned earlier. Is it possible to create such a counter in greenfoot?
Of course it is possible. A Counter object is just an Actor that displays some text and a number. It has the added effect of moving the int score field away from the object that is doing the scoring and creating more code. A Text object just displays Text (the number can be combined with the text to make one String object out of it, which makes a counter that is controlled from outside the Text class; but that is where the control of the Counter is to begin with). Furthermore, a Text class is just an Actor that displays an image (an image of some text). All Actor objects have an image and therefore, a separate class for Text is not even necessary (we can just create an unnamed Actor, add it to the world and change its (text) image when the value of the int score field changes). I will have a scenario up shortly that demonstrates what I mean. I will put a link to it here.
NickyL212 NickyL212

2014/1/13

#
Ill keep an eye on it ! For now i already got some sort of counter and the game stops after 1 minute or if its health is gone for now. Dont got a score counter yet though, but there are some scenarios that provide me the info i need for that. But i couldnt find 1 that was deductible and if the amount is deductible it could provide my plane with this 'shield'. still a bit clueless about how to even start this besides setting up the actors i think i need. I hope to get a bit more insight after ive seen your scenario.
danpost danpost

2014/1/13

#
It is up. It is called FPS Checker (with source). All you would need to do is subtract the cost from the int score field and update the image of the actor.
NickyL212 NickyL212

2014/1/13

#
that was really fast. expected it to be done tomorrow. Thanks for this quick update! I adjusted it a bit to the parts i THINK i need. Wich is the frame counter. Trying to understand what you did there in the act method, but its still a bit gibberish to me. Could you point out where you resetted to check the frames again? cause what i would need is that the points add up so i can substract them later right? or wasnt that the point of you showing met this scenario? This is what i plucked out of the source you made.
import greenfoot.*;

public class Speed extends World
{
    Actor fpsDisplay = new Actor(){};
    boolean hasSet, hasBegun;
    int speed = 6, frames;
    
    public Speed()
    {
        super(400, 100, 1);
        Greenfoot.setSpeed(speed);
        updateFPS();
        addObject(fpsDisplay, 200, 75);
    }

    private void updateFPS()
    {
        fpsDisplay.setImage(new GreenfootImage("Score "+frames, 48, java.awt.Color.black, null));
    }
    
    public void act()
    {
        int d = 0;
        
        if (d != 0 && speed+d > 0 && speed+d <= 100)
        {
            frames = 0;
            hasSet = false;
            hasBegun = false;
            speed += d;
            updateFPS();
            Greenfoot.setSpeed(speed);
        }
        int millis = (int)(System.currentTimeMillis()%1000);
        if (!hasSet && !hasBegun)
        {
            if (millis > 100) hasSet = true;
            return;
        }
        if (hasSet && !hasBegun)
        {
            if (millis < 100) { hasBegun = true; hasSet = false; }
            return;
        }
        if (!hasSet && hasBegun)
        {
            frames++;
            if (millis > 100) hasSet = true;
            return;
        }
        if (hasSet && hasBegun)
        {
            frames++;
            if (millis < 100)
            {
                hasSet = false;
                updateFPS();
                frames = 0;
            }
        }
    }
}
danpost danpost

2014/1/13

#
One major problem with your code is in your act method the first thing you do is declare and set the 'd' field to zero (which is what I did in my example scenario). But you went right from there to checking if 'd' had changed at all (where in my example, I gave the value a chance to change by including a couple of conditional checks. Your lines 26 through 34 will never execute because 'd' will be zero and the condition will always fail. The last inner 'if' block is where the fps display is updated (line 58) and the field that counts frames is reset (line 59). I will document the class and update the scenario. Will inform here when done.
danpost danpost

2014/1/14

#
Ok. Documentation has been added. I think I should tell you that it is the idea behind how I use the setting of the actor images which is important, not the surrounding code that is only pertinent to that specific scenario. Basically, what you do is: Declare an Actor field and set it to an Actor for displaying the score. Declare an int field (default value is zero) in the same class for the score itself. Initialize image of the score displaying Actor in the constructor of the class. Add it to the world (if a World subclass, in the constructor; in an Actor subclass, use the 'addedToWorld' method). Any time the value of the field changes, update the text image of the score displaying Actor on the next line.
Aaron91 Aaron91

2014/1/14

#
Like NickyL212 iam sort of making an score counter aswell. Trying to let my score add 100 points every 10 seconds the scenario is running. But why isnt this doing the trick?
public class Score extends Actor
{
    
    int score = 0;
    
    private void maakScore()
    {
        setImage(new GreenfootImage("Score "+score, 48, java.awt.Color.black, null));
    }
    
    /**
     * Act - do whatever the Score wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       
       
        
        updateScore();
        maakScore();
    }   
    
    public void updateScore()
   {
     long initialTime;
     initialTime = System.currentTimeMillis();
     if(System.currentTimeMillis() == initialTime + 11000)  
     {
      score += 100;
     }
   
   }
}
danpost danpost

2014/1/14

#
@Aaron91, you are creating a local field called 'initialTime' at line 26 inside the 'updateScore' method. It is then set to the current time followed by comparing it to the current time + 11000. The result of the comparison will always be false. In the next cycle you again create (not adjust) the 'initialTime' field and set it to the current time. Move line 26 to line 23 (or better, line 5) and either add a constructor (or better, an 'addedToWorld' method) to move line 27 into (or just change it to 'if (initialTime == 0) initialTime = System.currentTimeMillis();'. Two more things, change the comparison in line 28 to '>=' and update the 'initialTime' field when score is increased. As a sidenote: '11000' milliseconds would be eleven seconds, not ten.
There are more replies on the next page.
1
2
3
4
5
6