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

2016/3/30

Why isn't this code working? 2

IanWasHere IanWasHere

2016/3/30

#
I want this block to appear if the actCounter = 90... But I can;t seem to figure out why it isn't working... import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; import java.awt.Shape; /** * Write a description of class CopyOfBlock here. * * @author (your name) * @version (a version number or a date) */ public class CopyOfBlock extends Actor { private int actCounter; private int grn = 6; public int getTransparency; private int bttl = 300; private int mrn = 10; public CopyOfBlock() { setRotation(90); getImage().setColor(Color.BLACK); fillBlock(); actCounter = 100; secondBlock(); setTransparency(); } public void act() { moveDown(); } /** * Change the colour of the CopyOfBlock. */ private void fillBlock() { getImage().fill(); } public void setTransparency() { getImage().setTransparency(0); } /** * */ public void actCounter() { actCounter--; } /** * Move a random amount of steps. Call "secondBlock()". */ private void moveDown() { secondBlock(); move(Greenfoot.getRandomNumber(mrn)); } public void secondBlock() { if (actCounter == 90) { setLocation(295, 22); getImage().setTransparency(255); if (getY() == 499) { getImage().setTransparency(0); } else { if (isTouching(Dot.class)) { if (getTransparency !=255) { World world = getWorld(); world.removeObjects(world.getObjects(null)); //removes all the objects in the world; world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2); //adds the game over screen in the middle of the world; } } } } } }
danpost danpost

2016/3/30

#
IanWasHere wrote...
I want this block to appear if the actCounter = 90... But I can;t seem to figure out why it isn't working...
I do not see where the value of 'actCounter' is changing. I see a method to decrement its value; but, it is not being called from anywhere within the class. I have noticed several other less major issues within the code that can be dealt with later.
IanWasHere IanWasHere

2016/3/30

#
I have changed the code and called actCounter in the constructor because I want the actCounter to go down right away. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; import java.awt.Shape; /** * Write a description of class CopyOfBlock here. * * @author (your name) * @version (a version number or a date) */ public class CopyOfBlock extends Actor { private int actCounter; private int grn = 6; public int getTransparency; private int bttl = 300; private int mrn = 10; public CopyOfBlock() { setRotation(90); getImage().setColor(Color.BLACK); fillBlock(); actCounter = 100; setTransparency(); actCounter(); } public void act() { moveDown(); } /** * Change the colour of the CopyOfBlock. */ private void fillBlock() { getImage().fill(); } public void setTransparency() { getImage().setTransparency(0); } /** * */ public void actCounter() { actCounter--; secondBlock(); } /** * Move a random amount of steps. Call "secondBlock()". */ private void moveDown() { move(Greenfoot.getRandomNumber(mrn)); } public void secondBlock() { if (actCounter == 90) { setLocation(295, 22); getImage().setTransparency(255); if (getY() == 499) { getImage().setTransparency(0); } else { if (isTouching(Dot.class)) { if (getTransparency !=255) { World world = getWorld(); world.removeObjects(world.getObjects(null)); //removes all the objects in the world; world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2); //adds the game over screen in the middle of the world; } } } } } }
danpost danpost

2016/3/31

#
IanWasHere wrote...
I have changed the code and called actCounter in the constructor because I want the actCounter to go down right away.
You should set the value of 'actCounter' to whatever initial value you want in the constructor (you are currently setting it at 100 and then, now, calling, the 'actCounter' method which decrements it to 99 and calls the 'secondBlock' method (which will at that time do nothing because the value is not yet 90). So, you could just set the initial value to 99 and not call the 'actCounter' method. With the 'actCounter' method call in the constructor, it will only execute once for each CopyOfBlock object created. To have it repeatedly execute, you need to place the call to the 'actCounter' method in the 'act' method. There, it will execute once each act cycle (usually around 55 times per second) for as long as the CopyOfBlock object is in the world.
IanWasHere IanWasHere

2016/3/31

#
wow Thanks!
IanWasHere IanWasHere

2016/4/3

#
I have been playing around with this for a while now... I can't seem to get it right. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; import java.awt.Shape; /** * Write a description of class CopyOfBlock here. * * @author (your name) * @version (a version number or a date) */ public class CopyOfBlock extends Actor { private int actCounter; private int grn = 6; public int getTransparency; private int bttl = 300; private int mrn = 10; public CopyOfBlock() { setRotation(90); getImage().setColor(Color.BLACK); fillBlock(); actCounter = 99; setTransparency(); } public void act() { moveDown(); actCounter(); } private void actCounter() { actCounter--; if (actCounter == 90) { secondBlock(); } } /** * Change the colour of the CopyOfBlock. */ private void fillBlock() { getImage().fill(); } public void setTransparency() { getImage().setTransparency(0); } /** * Move a random amount of steps. Call "secondBlock()". */ private void moveDown() { move(Greenfoot.getRandomNumber(mrn)); } public void secondBlock() { setLocation(295, 22); getImage().setTransparency(255); if (getY() == 499) { getImage().setTransparency(0); } else { if (isTouching(Dot.class)) { if (getTransparency !=255) { World world = getWorld(); world.removeObjects(world.getObjects(null)); //removes all the objects in the world; world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2); //adds the game over screen in the middle of the world; } } } } }
danpost danpost

2016/4/3

#
You should avoid using individual methods for one-line commands unless they are being called from several different places within your code. So, methods like 'moveDown', 'setTransparency' and 'fillBlock' just complicate matters (makes more code instead of less and causes program flow to jump around more than necessary. You should use code tags when posting code to give the code line numbering. It helps when discussing the code. You can use the 'code' link below the reply box or just key the world 'code' between square brackets before an after the code. The closing tag needs a slash after the opening brackets to signify that it is the end of the code block ( ' ' -- without the spaces). Without all the unnecessary parts and simplifying a bit and making a few minor adjustments to the code, we get this:
import greenfoot.*;
import java.awt.Color;

public class CopyOfBlock extends Actor
{
    private int actCounter;

    public CopyOfBlock()
    {
        setRotation(90);
        getImage().setColor(Color.BLACK);
        getImage().fill();
        getImage().setTransparency(0);
        actCounter = 99;
    }

    public void act()
    {        
        move(Greenfoot.getRandomNumber(10));
        actCounter--;
        if (actCounter == 90) secondBlock();
    }

    public void secondBlock()
    {      
        setLocation(295, 22);
        getImage().setTransparency(255);
        if (getY() == 499)
        {
            getImage().setTransparency(0);
        }
        else
        {
            if (isTouching(Dot.class) && getTransparency() !=255)
            {
                World world = getWorld();
                world.removeObjects(world.getObjects(null));
                world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2);
            }
        }
    } 
}
From what I can tell, the game will not end unless a Dot object touches the CopyOfBlock object within the first 9 act cycles it is in the active world (while the CopyOfBlock object is invisible). This is usually equivalent to about 2/10ths of a second. After that, the images will have a transparency value of 255 until it reaches the bottom of the screen (y = 499). Then, the condition to end the game is that the y-coordinate NOT be 499 as that code is in the 'else' part of the condition. Therefore, the only time the game will end is as stated previously (the first 2/10th of a second the object is in the world; and that is if it touches a Dot object within those 2/10th of a second).
You need to login to post a reply.