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

2013/4/6

Barrier help

GrimCreeper312 GrimCreeper312

2013/4/6

#
some Where in the code below there is an error that tells the world that the player has bought the barriers when they only move the mouse over the button
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Button4 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Button4 extends Store
{
   
    private boolean isHovering;
     public static boolean e;
     /**
     * Act - do whatever the Button4 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        Bought();
    }    
    
     public void Bought()
    {
       if(!isHovering && Greenfoot.mouseMoved(this) && getWorld().getObjectsAt(497, 205, Pic4.class).isEmpty())
       {
           isHovering = true;
           if( e == true)
           {
               getWorld().addObject(new Purchased(),497,205);
            }
           getWorld().addObject(new Pic4(),497,205);
        }
       if (isHovering && Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this))  
       {
           getWorld().removeObjects(getWorld().getObjectsAt(497,205, Pic4.class));
           if( e == true)
           {
              getWorld().removeObjects(getWorld().getObjectsAt(497,205, Purchased.class));
              
            }
           isHovering = false;
           
        }
       
        if(Greenfoot.mouseClicked(this))
        {
        if(Cannon.score >= 10000)
        {
            if(e == false)
            {
                e = true;
                Atmosphere.boughtBarriers = true;
                Cannon.score = Cannon.score - 10000;
            }
        }
       }
    }
}
danpost danpost

2013/4/6

#
There are a couple of things you could try to make the code more precise. First, you can tell if barriers are bought by the value of Atmosphere.boughtBarriers, so you can eliminate e in this class and use that. Second, if a Pic4 object is to be in the world anytime the mouse is hovering over the Button4 object, then you can check for its presence instead of using the boolean 'isHovering'. By using current state instead of a variable, you avoid unwanted behaviour when the value of a variable is not kept up to date. The same goes for when you do need a variable (when you cannot directly determine the state without the use of one); use as few variables as necessary to track the same state (that is, only use one and refer to it even from other classes if necessary). In the first case above, you have two variables tracking the same state (that of barriers being purchased -- 'Atmosphere.boughtBarriers' in the Atmosphere class and 'e' in the 'Button4' class. In the second case above, you have a boolean tracking the state of the mouse hovering over the button and an object that is in the world only when the mouse is hovering over the button. Removing the boolean helps to remove possible improper value setting. The revised code I came up with follows:
import greenfoot.*;

public class Button4 extends Store
{
    public void act() 
    {
        bought();
    }    
    
    public void bought()
    {
        if (Greenfoot.mouseMoved(this) && 
            getWorld().getObjectsAt(497, 205, Pic4.class).isEmpty())
        {
           getWorld().addObject(new Pic4(),497, 205);
           if(Atmosphere.boughtBarriers) getWorld().addObject(new Purchased(), 497, 205);
        }
        if (!getWorld().getObjectsAt(497, 205, Pic4.class).isEmpty() &&
            Greenfoot.mouseMoved(null) && 
            !Greenfoot.mouseMoved(this))  
        {
            getWorld().removeObjects(getWorld().getObjectsAt(497,205, Pic4.class));
            getWorld().removeObjects(getWorld().getObjectsAt(497,205, Purchased.class));
        }
        if(Greenfoot.mouseClicked(this) &&
           Cannon.score >= 10000 &&
           !Atmosphere.boughtBarriers)
        {
            Atmosphere.boughtBarriers = true;
            Cannon.score = Cannon.score - 10000;
        }
    }
}
GrimCreeper312 GrimCreeper312

2013/4/6

#
i used the code above and i still got the error the only other place that would cause it is in the barriers method in the world it gets called in the worlds act method.
public void barriers()
    {
        if(boughtBarriers == true)
        {
            if(storeOpen == false)
            {
            removeObjects(getObjects(Barrier.class));
            
            
            
            addObject(barrier1, 200, 425);
            addObject(barrier2, 500, 425);
            addObject(barrier3, 800, 425);
            
            Barrier.life = 100;
            Barrier.life2 = 100;
            Barrier.life3 = 100;

            boughtBarriers = false;
           }
        }
    }
danpost danpost

2013/4/6

#
I wonder if 'storeOpen' is being set back to false.
GrimCreeper312 GrimCreeper312

2013/4/6

#
    public void closeStore()
    {
        if(!storeOpen == true)
        {
            storeOpen = false;
            
            button1.e = false;
            button3.e = false;

            
            
            removeObject(button1);
            removeObject(button2);
            removeObject(button3);
            removeObject(button4);
            removeObject(button5);
            
            removeObject(button6);
            removeObject(button7);
            removeObject(store); 

            boughtBarriers = false;
            
            canMove = true;
            
        }
    }
GrimCreeper312 GrimCreeper312

2013/4/6

#
in a little bit i will update my game and put source code with it
danpost danpost

2013/4/6

#
It makes no sense to set 'storeOpen' to a value you just found it was. Look at the condition and then the first statement within the 'if' block (your 'if' statement should probably be 'if (storeOpen == true)'). Also, the only time 'boughtBarriers' should be reset to 'false' is after placing the new barriers in the world (remove line 22).
GrimCreeper312 GrimCreeper312

2013/4/6

#
it didn't work so im updating my game now its intergalactic war it may take a min my internet not the fasetest
GrimCreeper312 GrimCreeper312

2013/4/6

#
the game has been updated now you can see the whole thing
danpost danpost

2013/4/6

#
I have looked over your code and have not been able to find where the problem is as yet. Your use of variables is over-whelming.
GrimCreeper312 GrimCreeper312

2013/4/8

#
i have narrowed the error to the following code the error seems to happen when the mouse hovers over the button
private boolean isHovering;
    public static boolean e;

    public void act()   
    {  
        bought();  
    }      
      
    public void bought()  
    {  
       if(!isHovering && Greenfoot.mouseMoved(this) && getWorld().getObjectsAt(497, 205, Pic4.class).isEmpty())
       {
           isHovering = true;
           getWorld().addObject(new Pic4(),497,205);
           if(e == true)
           {
                getWorld().addObject(new Purchased(),497,230);
            }
        }
       if (isHovering && Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this))  
       {
           getWorld().removeObjects(getWorld().getObjectsAt(497,205, Pic4.class));
           
           if(e == true)
           {
               getWorld().removeObjects(getWorld().getObjectsAt(497,230, Purchased.class)); 
               
            }
           isHovering = false;
           
        }
         if(Greenfoot.mouseClicked(this))
        {
        if(Ship.$$ >= 10000)
        {
            if(e == false)
            {
                
                Space.boughtBarriers = true;
                e = true;
                Ship.$$ = Ship.$$ - 10000;
            }
            
        }
       } 
       
    }  
danpost danpost

2013/4/8

#
The un-wanted behaviour is coming from somewhere else. The basics for the code above is logically sound (tried and tested). Actually, I think the fault is in the Atmosphere class, in how you are handling the opening and closing of the store and its boolean variables.
GrimCreeper312 GrimCreeper312

2013/4/8

#
i believe i fixed the error it seems that since Pic4 was a subclass of barrier it reset the life values of the barriers
danpost danpost

2013/4/8

#
Yeah. What you probably should have done, instead of creating a seperate class for each of the Pic#s, is create one class (I would call it Mimic, because it mimics the image) and create one instance of it when the mouse is over a button. Pass it an object of the class the button refers to in the parameter so it can access its image.
// create the Mimic object with
getWorld().addObject(new Mimic(new Shield()), 497, 205);
// the Mimic class code
import greenfoot.*;

public class Mimic extends Actor
{
    public Mimic(Actor actor)
    {
        setImage(actor.getImage());
    }
}
I used 'Shield' in the example above, but it can be any one of the objects that any one of the buttons refers to. Just like all your buttons could be created from the same Button class after slight modification of one of your Button classes code and adjusting the calls to give the specifics for each button. All your alien bullet classes can also be combined into one class with some code modifications. All your barriers should be from the same class as there is only one difference in the instances of that class: their location in the world. Again, I stress that the excessive use of classes, sub-classes, variables and static fields will continue to cause problems for you. I would like to give you pointers on these subjects at this time, but do not currently have to time to.
You need to login to post a reply.