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

2014/8/11

timer change mover after a time

1
2
meddis meddis

2014/8/11

#
I'm creating a snowman game. The snowman will check for a fire and it can shoot ice. If the ice hits a fire it will "ice it", so it turns into a trappedfire. If a snowman walks into a trapped it will be removed. If nothing touches the trappedfire it will become a fire again after a few seconds. My problem is the last part, that the trapped fire will become a fire again after a few seconds. Help?
private void checkForFire()
{ 
         Fire fire = (Fire)getOneIntersectingObject(Fire.class);

        if(fire != null)
        {
         TrappedFire trappedFire = new TrappedFire();
            trappedFire.setRotation(fire.getRotation());                
            getWorld().addObject(trappedFire, fire.getX(), fire.getY());
            getWorld().removeObject(fire);
            getWorld().removeObject(this);
            
danpost danpost

2014/8/11

#
Though not stated, the code given looks like it belongs in the Ice class and looks ok as far as detecting fire, changing it into a trappedfire object with the proper location/rotation and removing of the fire object and self. The code that needs worked on, determined by 'the trapped fire will become a fire again after a few seconds', is that of the TrappedFire class. It is its behavior that needs to be fixed. Obviously, you will need an instance 'int' field as an act counter to time the 'few' seconds and if it reaches some pre-specified limit, it should add a new fire object with its location/rotation and remove itself. A scenario running at normal speed (around the middle of the speed slider, or at 50) will execute between 50 and 60 act cycles per second; so 160 might be close to the limit you want.
meddis meddis

2014/8/11

#
Yes, this code is in the Ice class. So for the trapped fire to become a fire again i have to put a code in the trapped fire class? i really don't now how that code would look like.
danpost danpost

2014/8/11

#
meddis wrote...
Yes, this code is in the Ice class. So for the trapped fire to become a fire again i have to put a code in the trapped fire class? i really don't now how that code would look like.
What do you currently have in your TrappedFire class?
meddis meddis

2014/8/11

#
I have no code at all. I'm really new at this and our book doesn't give any information about timers etc.
danpost danpost

2014/8/11

#
It should, however, have something on 'instance fields' or 'object state'. A 'timer' is just an instance 'int' field (each object created from a class, when created, is assigned a new field for each non-static field declared within the class; these field remain with the object for as long as it exists). Adding an instance (non-static) 'int' field to your TrappedFire class means that each trappedfire object will have its own field that holds an 'int' value with the name defined in the field declaration line.
// instance field (outside of any method; but, inside the class)
private int age = 0; // instance field declaration line
// act method
public void act()
{
    age++; // counts the act cycles
    if (age == 160) // test for limit
    {
        // similar code to changing fire object to trappedfire object
    }
}
meddis meddis

2014/8/11

#
So this class should go in the TrappedFire class? Will this make a trappedfire turn into a fire again?
danpost danpost

2014/8/11

#
I did not give the whole class -- just the code which deals with the 'timer'. You still have to insert this into your TrappedFire class and complete the 'act' method.
meddis meddis

2014/8/11

#
Yeah, I'm sorry I mean't code. If i insert this to my TrappedFire class, what else do i have to add? Because the first code that I posted is in my Ice class
danpost danpost

2014/8/11

#
You should not need to do more than I suggested. The Ice object was removed from the world when the fire object was 'trapped'. What more can it do!?! The ice does what it does; the trappedfire does what it does. That is the what happens and that is what you program them to do.
meddis meddis

2014/8/11

#
I tried doing the code like this but it wont work
private int age = 0; // instance field declaration line  
// act method  
public void act()  
{  
    age++;
    if (age == 160) // test for limit  
    {  
          Fire fire = (Fire)getOneIntersectingObject(Fire.class);

        if(fire != null) 
        {
            TrappedFire trappedFire = new Fire();
            trappedFire.setRotation(fire.getRotation());                
            getWorld().addObject(Fire, TrappedFire.getX(), TrappedFire.getY());
            getWorld().removeObject(TrappedFire);
            getWorld().removeObject(this);
    }  
danpost danpost

2014/8/11

#
First, line 12 is trying to set a Fire object to a variable that holds a TrappedFire object -- you should be getting an error here. Line 8 through 11 are not needed at all. There is no collision or intersecting of objects related to the removal of the trappedfire object due to the passage of time. Remove those lines. The location/rotation of the fire object should take on the same values from 'this' trappedfire object and you should only be removing one object from the world, the trappedfire object, or 'this'. Lines 12 through 16 should be like this:
Fire fire = new Fire();
fire.setRotation(getRotation()); // or 'this.getRotation()'
getWorld().addObject(fire, getX(), getY()); // or 'this.getX()' and 'this.getY()'
getWorld().removeObject(this);
I meant 'similar' as far as changing the fire object to a trappedfire object, but reversed.
meddis meddis

2014/8/11

#
public class TrappedFire extends Actor
{    
 // instance field (outside of any method; but, inside the class)  
private int age = 0; // instance field declaration line    
; // act method    
public void act()    
{    
    age++;  
    if (age == 160) // test for limit    
    {    
        
         Fire fire = new Fire();  
         fire.setRotation(getRotation()); // or 'this.getRotation()'  
         getWorld().addObject(fire, getX(), getY()); // or 'this.getX()' and 'this.getY()'  
         getWorld().removeObject(this);  
    }    
}
}
Says; constructor Fire in class Fire cannot be applied to given types required int; found: no arguments; reason: actual and formal arguments differ in length
danpost danpost

2014/8/11

#
What is the argument for constructing a Fire object? is it the rotation that it is to have? Show your Fire class code.
meddis meddis

2014/8/11

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

/**
 * Fires that want to hit the 
 * 
 */
public class Fire extends Mover
{
    private GreenfootImage[] images = new GreenfootImage[2];
    private double currentImage = 0; 
    private int[] directions = { Mover.LEFT, Mover.UP, Mover.RIGHT, Mover.DOWN };
    private int currentDirection;
    
    /**
     * Creates a fire with the given speed.
     */
    public Fire(int speed)
    {
        super(speed);
        currentDirection = Greenfoot.getRandomNumber(directions.length);
        images[0] = new GreenfootImage("fire1.png");
        images[1] = new GreenfootImage("fire2.png");
        setImage(images[0]); 
    }
    
    /**
     * Switching the image.
     */
    private void switchImage() {
        currentImage = currentImage + 0.2;
        if(currentImage >= images.length) {
            currentImage = 0;
        }        
        setImage(images[(int)currentImage]);
    }
    
    /**
     * The fireballs are trying to catch the snowman.
     */
    public void act() 
    {
        switchImage();
        turnAtEdge();
        randomTurn();
        if(moveActor(directions[currentDirection]))
        {
            currentDirection = (currentDirection += 1) % directions.length;
        }
        checkForSnowman();

    }    
    
    /**
     * Check if the fire is at the egde of the world, if it is, turn, if not, do nothing.
     */
    private void turnAtEdge()
    {
        if (isAtWorldEdge()) 
        {           
            currentDirection = (currentDirection += 2) % directions.length;
        }
    }
        
    /**
     * Random turns.
     */
    private void randomTurn()
    {
        if (Greenfoot.getRandomNumber(100) > 97) {
            currentDirection = Greenfoot.getRandomNumber(directions.length);            
        }
    }

    /**
     * If a fire hits a snowman, the snowman will be removed from the game.
     */
    private void checkForSnowman()
    {
        if (getOneIntersectingObject(Snowman.class) != null) 
        {
            removeTouching(Snowman.class);            
            Greenfoot.stop();
        }
    }
}
There are more replies on the next page.
1
2