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

2017/1/18

Referencing a Method

1
2
BrownDwarf BrownDwarf

2017/1/18

#
I have this code for my game. I have been trying to spawn a boss when the user hits 15 points, but in order to spawn I need to be in the World Class. And the problem i'm having is trying to GET the score variable in the world class, to check if it is 15. My world class is as follows. The error is at the bottom.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Space extends World
{

    public int guess;
    Counter counter = new Counter();
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1100, 600, 1); 
        prepare();
        //PlatformAlien();

    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    public void prepare()
    {
        Hero hero = new Hero();
        addObject(hero,298,137);
        hero.setLocation(560,287);
        RegularPlatform regularplatform = new RegularPlatform();
        addObject(regularplatform,539,398);
        regularplatform.setLocation(553,366);
        addObject(counter, 100, 40);
        // int amount = Greenfoot.getRandomNumber (0);

        int rand = Greenfoot.getRandomNumber (2);
        if (rand == 1)
        {
            Missiles[] missiles = new Missiles [2];
            for (int i = 0; i < missiles.length; i++)
            {
                missiles[i] = new Missiles(); 
                int misslesX = 1;
                int misslesY = Greenfoot.getRandomNumber (600);
                addObject(missiles[i], misslesX, misslesY);
            }
        }
        else {
            Missiles[] missiles = new Missiles [1];
            for (int i = 0; i < missiles.length; i++)
            {
                missiles[i] = new Missiles(); 
                int misslesX = 1;
                int misslesY = Greenfoot.getRandomNumber (600);
                addObject(missiles[i], misslesX, misslesY);
            }
        }
        PlatformAlien();

    }

    public void PlatformAlien()
    {
        RegularPlatform[] platforms = new RegularPlatform [ 4];
        Alien[] aliens = new Alien [2];
        int platformX1 = 0;
        int platformY1 = 0;
        int platformX2 = 0;
        int platformY2 = 0; 

        for (int i = 0; i < platforms.length; i++)
        {
            platforms[i] = new RegularPlatform();

            // guess = Greenfoot.getRandomNumber (2);
            //int platformX = Greenfoot.getRandomNumber(1100); 
            /// if (guess == 1)
            // {
            //   platformX = 1;

            //}
            // else {
            //     platformX = 1100;
            // }
            int platformX = Greenfoot.getRandomNumber(1100); 
            int platformY = Greenfoot.getRandomNumber (450) + 110;
            if (i == 0)
            {
                platformX1 = platformX;
                platformY1 = platformY;
            }
            else if (i ==1)
            {
                platformY2 = platformY;
                platformX2 = platformX;
            }
            //int platformY = Greenfoot.getRandomNumber (450) + 110;
            addObject(platforms [i] , platformX,platformY);

            //Actor offset = (RegularPlatform) getOneObjectAtOffset (0, 40, RegularPlatform.class);
            // Actor RegularPlatform; 
            //RegularPlatform = getOneObjectAtOffset (platformX, platformY, RegularPlatform.class);
            // if ( RegularPlatform !=null)
            //   
            //      platforms[i].setLocation(platformX+40, platformY + 40);

            //tryna get it so that some spawn form left or just when it hits left go back

        }
        for (int i = 0; i < aliens.length; i++)
        {
            aliens[i] = new Alien();
            if (i == 0)
            {
                addObject (aliens[i], platformX1, platformY1 - 70);
            }
            else if (i == 1)
            {
                addObject (aliens[i], platformX2, platformY2 - 70);
            }
            //aliens[i].setLocation (RegularPlatform.getXplatform(), 100);
        }
    }

    public int getGuess()
    {
        return guess;
    }

    public Counter getCounter()
    {
        return counter;
    }

    public  void boss()
    {
        int theScore = Counter.getScore();
        if (theScore == 15)
        {
            BigAlien bigalien = new BigAlien();
            addObject(bigalien, 0, 0);
        }
    }
}

This is the method I am referencing here at the bottm
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    /**
     * Act - do whatever the Score wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int score = 0;
    
    
    
    public  int getScore()
    {
        return score;
    }
    
    public  void reset()
    {
        score = 0;
    }
    public void addScore()
    {
        score += 5;
    }
    public void act() 
    {
       setImage(new GreenfootImage("Score: " + score, 25, Color.BLACK, Color.lightGray));
       
    }    
}
THANKS IF YOU CAN HELP!
BrownDwarf BrownDwarf

2017/1/18

#
Sorry if I wasn't clear. on line 143 of the first code, there is an error when I use the getScore() method, I don't know how to reference it properly.
danpost danpost

2017/1/18

#
BrownDwarf wrote...
Sorry if I wasn't clear. on line 143 of the first code, there is an error when I use the getScore() method, I don't know how to reference it properly.
Line 143 is in the Space class where the Counter object is already referenced by 'counter' (see line 13). The problem with line 143 is that you are trying to get the score value from the Counter class -- not the Counter object. Use 'counter' instead of 'Counter'.
BrownDwarf BrownDwarf

2017/1/18

#
Oh, I see. Thanks danpost for the quick replies!
BrownDwarf BrownDwarf

2017/1/18

#
Btw, I have another problem, that is I have been trying to get my alien(enemy) class to spawn WITH my platforms. The platforms scroll to the right, and reappear on the left after going off screen. I have gotten them to spawn with the platforms initially, but after the aliens get to the edge, I haven't figured out a way to have them respawn with the platforms.
danpost danpost

2017/1/19

#
BrownDwarf wrote...
I haven't figured out a way to have them respawn with the platforms.
Have the platform move the alien along with it (do not have the aliens remove themselves at the edges).
BrownDwarf BrownDwarf

2017/1/19

#
Ok. the aliens move along the platforms, back and forth. So once the platform disappears, what code do I write that will make the alien reappear with the platform, on the same xy. How would I make it so that the alien sticks to the platform in all cases, even if it disappears.
danpost danpost

2017/1/19

#
BrownDwarf wrote...
Ok. the aliens move along the platforms, back and forth. So once the platform disappears, what code do I write that will make the alien reappear with the platform, on the same xy. How would I make it so that the alien sticks to the platform in all cases, even if it disappears.
First, you will need to unbound your world. Change line 21 of your Space class to this:
super(1100, 600, 1, false);
Then, show the code in your Platform class. Also, the enemy class (need to see the relationship between the two when an alien is on a platform; as well as what code you are using to keep the enemy on the platforms).
BrownDwarf BrownDwarf

2017/1/19

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
//import Actor.*;

/**
 * Write a description of class Platform here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class RegularPlatform extends Actor
{
    /**
     * Act - do whatever the Platform wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public RegularPlatform()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() - 760, image.getHeight() - 325);
        setImage(image);
    }

    public void act() 
    {
        //setLocation(getX() + 1, getY());
        // spawnRandom();
        ///////moving();
        whichWay();
        // speed();
        //////////direction();
        //moving();
       ///////////touching();
        //scale();
        //moveBack();
        
    }    

    public void spawnRandom()
    {
        // int xValue = 0;
        //int num = Greenfoot.getRandomNumber(500);
        ////if (num == 1)
        //{
        //  RegularPlatform Reg1 = new RegularPlatform();
        //MyWorld world = (MyWorld) getWorld();
        //int y = Greenfoot.getRandomNumber(600);
        //world.addObject(Reg1, 0, y);

        //}
        //for (int i = 0; i < 5; i++)
        //{
        //// RegularPlatform Reg1 = new RegularPlatform();
        // MyWorld world = (MyWorld) getWorld();
        // int y = Greenfoot.getRandomNumber(600);
        //// world.addObject(Reg1, 0, y);
        // if (isAtEdge())
        // {
        //  RegularPlatform.setLocation(0, y);
        // int amount = Greenfoot.getRandomNumber (4);
        //RegularPlatform[] platforms = new RegularPlatform [amount + 4];
        // for (int i = 0; i < platforms.length; i++)
        // {

        // platforms[i] = new RegularPlatform();
        //  int platformX = Greenfoot.getRandomNumber (1); 
        // int platformY = Greenfoot.getRandomNumber (600);
        //addObject(platforms [i] , platformX,platformY);

        // Actor offset = (RegularPlatform) getOneObjectAtOffset (0, 40, RegularPlatform.class);
        //  Actor RegularPlatform; 
        //RegularPlatform = getOneObjectAtOffset (platformX, platformY, RegularPlatform.class);
        //if ( RegularPlatform !=null)
        //{
        //  platforms[i].setLocation(platformX+40, platformY + 40);
        //}
        //}

    }

    
   
    public   int speed = Greenfoot.getRandomNumber(3) + 1;

    public void moving()
    {

        if ( speed == 1)
        {

            move(1);
        }
        else if (speed == 2)
        {
            move (2);
        }
        else if ( speed == 3){
            move (3);
        }
    }

    public void moveBack()
    {

        if ( speed == 1)
        {
            move(-1);
        }
        else if ( speed == 2)
        {
            move (-2);
        }
        else if ( speed == 3)
        {
            move (-3);
        }
    }
    public static int Y1;
    public static int Y2;
    public void whichWay()
    {
        moving();
        for (int i = 0; i <= 2; i++)
        {
        if (isAtEdge())
        {
           int yValue = Greenfoot.getRandomNumber(450) + 110;
            setLocation (0, yValue);

            // moveBack(); 

        
        if (i == 0)
        {
        Y1 = yValue;
    }
        else if (i == 1)
        {
            Y2 = yValue;
        }
        //else {
        // moving();
        //}
    }
    }
}

    public void touching()
    {
        while (isTouching())
        {

            setLocation (getX(), getY() +- 40);

        
        }
    }

    public boolean isTouching()
    {
        Actor under = getOneObjectAtOffset (0, 100 / 2, RegularPlatform.class);
        return under != null; 

    }

    public void direction()
    {
        if (getX() == 1099 )
        {
            moveBack();
        }

        else if ( getX() == 1 ){
            moving();
        }

    }
    int guess = Greenfoot.getRandomNumber (2);

    public void speed()
    {
        if (guess == 1)
        {
            moving();
        }
        else {
            moveBack();
        }
    }

    public int X()
    {
        return getX();

    }
    public int getY1()
    {
        return Y1;
    }
    public int getY2()
    {
        return Y2;
    }

    public  int getSpeed()
    {
        return speed;
    }
    public int x()
    {
        return getX();
    }
    public  int getXplatform()
    {
       int x = getX();
       
        return x;
        
    }
}

// private void scale()
// {

//   GreenfootImage image = getImage();
//  image.scale(image.getWidth() - 60, image.getHeight() - 25);
//  setImage(image);
// }
// size by scaling, speed, location, 
//WHILE TOUCHING THE PLATFORM X OF HERO EQUALS GETX OF PLATFORM, ELSE IT IS ITS OWN THING. SO IT STAYS ON 
// if platform gets to edge then either respawn back at x is 0, 

next is Alien class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Alien here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Alien extends Actor
{
    /**
     * Act - do whatever the Alien wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     public Alien()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() - 260, image.getHeight() - 350);
        setImage(image);
        
    }
     private int spriteHeight = getImage().getHeight() - 350;
     
     private int spriteWidth = getImage().getWidth() - 260;
     private int lookForGroundDistance = (int)spriteHeight/2;
     private int lookForEdge = (int)spriteWidth/2;
     //int movespeed = RegularPlatform.getSpeed() + 3;
     int now = 5;
     int look = 5;
    public void act() 
    {
     
     move();
     offEdge();
     death();
    }    
    public void set()
    {
       // RegularPlatform theWorld = (RegularPlatform) getWorld();
       // int newX = theWorld.getX();
        
        
    }
    public void move()
    {
        Actor ground = getOneObjectAtOffset( lookForEdge, lookForGroundDistance, RegularPlatform.class);
        if (ground == null)
        {
            now *= -1;
            lookForEdge *= -1;
        }
        else {
            move(now);
        }
    }
    public void offEdge()
    {
        if (getX() == 1099)
        {
            RegularPlatform regularPlatform = new RegularPlatform();
               int yValue = regularPlatform.getY1();
            setLocation (0, yValue - 70);

            // moveBack(); 

        }
    }
    Hero hero = new Hero();
    boolean killing = hero.isKill();
    public void death()
    {
        if (isTouching(Hero.class) && killing == false)
        {
            
            removeTouching(Hero.class);
        }
    }
    public void boss()
    {
        
        World myWorld = getWorld();
             Space space = (Space) myWorld;
           Counter counter = space.getCounter();
           //int theCount = space.getScore();
           //if (theCount >=  15)
          // {
               
            //}
    }
}
danpost danpost

2017/1/19

#
Wow! You need to clean things up first -- way too many fields (some unnecessary/some not used); the code is way too bulky (much can be done to improve here); way too many commented lines (maybe not); poor use of static fields (definitely). Keeping things simple is the way to go. That means the less fields, the better. Only declare fields that are absolutely necessary (for values and references to objects that need to be retained from one act cycle to the next. If there is a way to obtain the value by way of method calls on the spot, that would be the best approach. You are more apt to get the correct current value and you do not have to update the fields you created. Static fields do not belong to any one object of a class. The 'Y1' and 'Y2' values in the RegularPlatform class, for example, will both be set to the new y ('yValue', from line 126) of the last platform that touched an edge. The 'for' loop that it is in, in itself, is troublesome. It is executed twice. Everything it accomplishes during the first iteration is nullified by the second iteration. Anyways, this is just one area where all my concerns can be pointed out at (except for commented lines). My next post will start somewhere (I am not sure where yet).
danpost danpost

2017/1/19

#
Space class: I noticed that you declare all your variables that will be used as parameters in method calls on separate lines before the line that calls the method. For example, you currently have the following in lines 55 through 62:
Missiles[] missiles = new Missiles [1];
for (int i = 0; i < missiles.length; i++)
{
    missiles[i] = new Missiles(); 
    int misslesX = 1;
    int misslesY = Greenfoot.getRandomNumber (600);
    addObject(missiles[i], misslesX, misslesY);
}
The following does the exact same thing:
addObject(new Missiles(), 1, Greenfoot.getRandomNumber(600));
Unless the values stored in 'missiles' (the array), 'misslesX' and 'misslesY' are to be use elsewhere, there is no need to hold their values in variables. Next, I noticed that you have the coordinate values for the first two platforms saved in variables to be used later to place the aliens on them. Instead of going round-about and saving the coordinates, you can just add the aliens in right there. The Space class can therefore be reduced to the following:
import greenfoot.*;

public class Space extends World
{
    Counter counter = new Counter();
    public int guess;

    public Space()
    {    
        super(1100, 600, 1); 
        prepare();
    }
 
    public void prepare()
    {
        Hero hero = new Hero();
        addObject(hero, 560, 287);
        addObject(new RegularPlatform(), 553, 366);
        addObject(counter, 100, 40);
        for (int i=0; i<1+Greenfoot.getRandomNumber(2); i++)
        {
            addObject(new Missiles(), 1, Greenfoot.getRandomNumber(600));
        }
        for (int i=0; i<4; i++)
        {
            int platformX = Greenfoot.getRandomNumber(1100); 
            int platformY = 110+Greenfoot.getRandomNumber(450);
            addObject(new RegularPlatform(), platformX, platformY);
            if (i < 2)
            {
                addObject (new Alien(), platformX, platformY-70);
            }                
        }
    }
 
    public int getGuess()
    {
        return guess;
    }
 
    public Counter getCounter()
    {
        return counter;
    }
 
    public  void boss()
    {
        if (counter.getScore() == 15)
        {
            addObject(new BigAlien(), 0, 0);
        }
    }
}
I get the impression that lines 6 and lines 36 through 40 can also be removed. Counter class: I am not quite sure why you have a 'reset' method in the Counter class. Also, the 'addScore' method is limiting in what can be added to the score. It really is okay, provided you will only ever add 5 points at a time. The 'score field (line 15) should probably be made 'private' instead of 'public'.
BrownDwarf BrownDwarf

2017/1/19

#
Thanks for the edits. Yah I realized I have really redundant code. But I still need a way to make the aliens stick with their platforms. Thanks though, I made all these changes.
danpost danpost

2017/1/19

#
The RegularPlatform class: The following is not quite yet what you want. Here, I have the platform moving back and forth -- not jumping from one side to the other. We will work on that in a little bit (have patience). I renamed the 'guess' field to 'dir' and gave it a value set of { -1, 1 } instead of { 0, 1 }. You can see how I used it in conjunction with the 'speed' value for moving. This cleaned up the code quite a bit. Here is the current version:
import greenfoot.*;

public class RegularPlatform extends Actor
{
    private int speed = 1+Greenfoot.getRandomNumber(3);
    private int dir = 1-2*Greenfoot.getRandomNumber(2);

    public RegularPlatform()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth()-760, image.getHeight()-325);
    }

    public void act() 
    {
        move(dir*speed);
        if (isAtEdge())
        {
            dir = -dir; // reverse direction (to be changed)
        }
    }
    
    public int getSpeed()
    {
        return speed;
    }
}
danpost danpost

2017/1/19

#
The Alien class: I did the same thing, changing 'now' to 'dir' and using it for moving and platform checking. Here is the revision:
import greenfoot.*;

public class Alien extends Actor
{
    private int dir = 1;
    private int speed = 5;

    public Alien()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth()-260, image.getHeight()-350);         
    }

    public void act() 
    {
        move(dir*speed);
        if (getOneObjectAtOffset(dir*speed, getImage().getHeight()/2, RegularPlatform.class) == null)
        {
            dir = -dir;
        }
        if (isTouching(Hero.class))
        {
            removeTouching(Hero.class);
        }
    }
}
danpost danpost

2017/1/19

#
With the above, the aliens will stick with the platforms. If you really want to have the platforms continue as you suggested, jumping across the screen when hitting an edge, just say so. It will be a lot easier to handle with the simplified code.
There are more replies on the next page.
1
2