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

2014/1/10

limit my actors movement

3
4
5
6
7
danpost danpost

2014/1/19

#
Ok. So, this method is non-conditionally called from the act method of the class; and when this actor is clicked on, your 'boot' object is to change into an 'UpgradeBoot' object for a short period of time. Line 3: you will need the 'score' Line 4: why is the 'heart' object needed when upgrading boot Line 5: why is the 'barrel' object needed when upgrading boot Line 6; why is the 'Bomb' object needed when upgrading boot Line 7: depending on how you wish to qualify the purchase of a shield, you may or may not need this one at this time Lines 8: the 'if' should be asked before getting any objects (lines 3 through 7; or what is left of them). Getting these values before we even know we need them is wasteful. Every act cycle several object fields would need to be set up and assigned values using long calls, just to have them sent to the garbage collector 99.99+ percent of the time. Better would be to start the method with this line:
if (!Greenfoot.mouseClicked(this) || score.getValue() < 500) return;
After which you set up what fields you need, apply the upgrade and deduct the purchase amount from the score. You still did not say whether you would allow purchase of a shield upgrade while one was already in effect or not. You could work this at least three different ways: (1) no purchase while one is currently in effect (2) purchase allowed anytime, but overrides any previous purchase (loses remaining time and resets the time value) and (3) purchase allowed anytime and time value is applied to the time remaining to present one (does not lose any remaining time and a new allotment of time is added to that remaining time).
Aaron91 Aaron91

2014/1/19

#
danpost wrote...
Ok. So, this method is non-conditionally called from the act method of the class; and when this actor is clicked on, your 'boot' object is to change into an 'UpgradeBoot' object for a short period of time. Line 3: you will need the 'score' Line 4: why is the 'heart' object needed when upgrading boot Line 5: why is the 'barrel' object needed when upgrading boot Line 6; why is the 'Bomb' object needed when upgrading boot Line 7: depending on how you wish to qualify the purchase of a shield, you may or may not need this one at this time Lines 8: the 'if' should be asked before getting any objects (lines 3 through 7; or what is left of them). Getting these values before we even know we need them is wasteful. Every act cycle several object fields would need to be set up and assigned values using long calls, just to have them sent to the garbage collector 99.99+ percent of the time.
Line 4 = I removed it. Cause the heart object isnt needed in this matter. My bad. Line 5 and 6 = Cause the damaged should be invalid when hitting these actors right? line 7 = I got 2 upgrades total. 1 is 'RestoreHeart' (restores health if hitted) and the other is the 'Shield'. They cant override eachother right? cause the 'RestoreHeart' only affects the 'heart' Actor. Or is it like if i purchase the 'Shield' first then the 'RestoreHeart' that the shield will be lifted? If so i think the best would be 'no purhcase while one is currently in effect.' if iam not mistaking though
Super_Hippo Super_Hippo

2014/1/19

#
Aaron91 wrote...
True. the getShield method is in my 'Shield' actor and the 'Shield' actor is placed in my MenuWorld. And by pressing this 'Shield' actor iam trying to make it invincible and replace the current main actor with the 'UpgradeBoot' actor for a few seconds. To give you an image of the other actor part, when i press on this 'Shield' actor the main actor 'boot' turns temporarly into the actor 'UpgradeBoot' wich is the same boot but only with an sort of shield around it.
You do not have to replace the actor by another actor. Instead, you can have a 'private/public boolean shieldOn' (or something like that) and set this to true and change the image of the actor when activating the shield. Now only check whether it is hit by a bomb or barrel if 'shieldOn' is false. If 'shieldOn' is true, you add 1 to a timer and if this timer reaches a specific value, you deactivating the shield again (set the image back and set shieldOn to false). Place the colliding methods into your main actor.
danpost danpost

2014/1/19

#
Aaron91 wrote...
Line 5 and 6 = Cause the damaged should be invalid when hitting these actors right?
This may be, but (1) the upgrade has not even been added yet; (2) no damage can be made in your BotenUpgrade world while the MenuWorld is running (the BotenUpgrade world is in suspended animation -- frozen -- not running at the moment). As Super_Hippo said, this collision checking should be done in the boot class.
line 7 = I got 2 upgrades total. 1 is 'RestoreHeart' (restores health if hitted) and the other is the 'Shield'. They cant override eachother right? cause the 'RestoreHeart' only affects the 'heart' Actor. Or is it like if i purchase the 'Shield' first then the 'RestoreHeart' that the shield will be lifted? If so i think the best would be 'no purhcase while one is currently in effect.' if iam not mistaking though
I was not saying that one upgrade would override a different kind of upgrade. I was saying that, let us say that an upgrade for shield was purchased and before it was removed (still had time on it) the MenuWorld was again brought up and the upgrade for a shield was again attempted. Should it, or should it not, be allowed; and, if so, how would the upgrade be applied.
Aaron91 Aaron91

2014/1/19

#
Oke iam trying to fix my boot first. It looks like this now. The only thing is that the actor isnt in my world anymore? But iam pretty sure i added it there. These are my 'BotenUpgrade' world and and 'boot' actor. Only showed the part that matters of my world. Wich is the addObject(new boot(), 350, 600); ]java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867) at boot.<init>(boot.java:15) at BotenUpgrade.<init>(BotenUpgrade.java:47) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at greenfoot.core.Simulation.newInstance(Simulation.java:581) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:409) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:468) at greenfoot.core.Simulation.maybePause(Simulation.java:281) at greenfoot.core.Simulation.runContent(Simulation.java:212) at greenfoot.core.Simulation.run(Simulation.java:205)
 public BotenUpgrade()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(760, 760, 1); 

        GreenfootImage background = new GreenfootImage(760, 760);
        scrollingImage = getScrollingImage(760, 760);
        background.drawImage(scrollingImage, 500, 500);
        setBackground(background);

        addObject(new boot(), 350, 600);

        setBackground("Backgroundje.png");

        addHeart();


        timeText.setImage(new GreenfootImage("Meters: 1000", 30, java.awt.Color.black, null));  

        addObject(timeText, 660, 50);

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

/**
 * Write a description of class boot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class boot extends Actor
{
    /**
     * Act - do whatever the boot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
       Actor barrel = getOneObjectAtOffset(0,0,barrel.class);
         Actor Bomb = getOneObjectAtOffset(0,0,Bomb.class);
    
    public void act() 
    {
        
        
        if (Greenfoot.isKeyDown("left"))
        {
         move(-3);   
        }
        if (Greenfoot.isKeyDown("right"))
        {
         move(3);   
        }
         if (Greenfoot.isKeyDown("up"))
        {
         setRotation(90);
         move(-3);
         setRotation(0);
        }
        if (Greenfoot.isKeyDown("down"))
        {
         setRotation(-90);
         move(-3);
         setRotation(0);   
        }    
        
         if(isGeraakt())    
      {    
        removeHealth();  
        getWorld().removeObject(Bomb);  
        getWorld().removeObject(barrel);  
        return;         
           
       } 
        
        
       if (getY()<480) setLocation( getX(),480); 
       

     
    }    

    public boot()  
    {      
        GreenfootImage image = getImage();    
        image.scale(100, 80);  
        setImage(image);  
    }
    
    public void removeLive()    
    {    
        int lives = getWorld().getObjects(heart.class).size();    
        getWorld().removeObject((Actor)getWorld().getObjects(heart.class).get(lives-1));  

        if (lives != 1)  
        {  
            //
        }  
        else  
        {     
            Greenfoot.stop();  
        }  
    }   
    
    public boolean isGeraakt()
   {
     boolean raak = false;

     if ( Bomb !=null || barrel !=null )
     {
      raak = true;
      
     }
     return raak;
   }
    
   public void removeHealth()
   {
     BotenUpgrade subWorld = (BotenUpgrade) getWorld();   
     heart heartObject = (heart) subWorld.getObjects(heart.class).get(0);   
     heartObject.adjustValue(-1);    
       
     if (heartObject.getValue() == 0) removeLive();  
     
   } 

} 
danpost danpost

2014/1/19

#
In lines 15 and 16, you are trying to use methods that require that the object be in a world prior to that object being placed in a world. In fact, the object has not been completely constructed, yet. They should be the first two lines in your 'isGeraakt' method.
Aaron91 Aaron91

2014/1/19

#
I thought that i could place them outside the methodes so both Act and isGeraakt could acces them. I placed them in both methodes now. It looks like this at the moment and its doing what it should do at the moment.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class boot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class boot extends Actor
{
    /**
     * Act - do whatever the boot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
       
    
    public void act() 
    {
        
        
        if (Greenfoot.isKeyDown("left"))
        {
         move(-3);   
        }
        if (Greenfoot.isKeyDown("right"))
        {
         move(3);   
        }
         if (Greenfoot.isKeyDown("up"))
        {
         setRotation(90);
         move(-3);
         setRotation(0);
        }
        if (Greenfoot.isKeyDown("down"))
        {
         setRotation(-90);
         move(-3);
         setRotation(0);   
        }   
        
        if (getY()<480) setLocation( getX(),480); 
       
       if(isGeraakt())    
       {     
        Actor Bomb = getOneObjectAtOffset(0,0,Bomb.class);
        Actor barrel = getOneObjectAtOffset(0,0,barrel.class);
        removeHealth();  
        getWorld().removeObject(Bomb);
        getWorld().removeObject(barrel); 
        return;         
           
        }   
       
        
    }    

    public boot()  
    {      
        GreenfootImage image = getImage();    
        image.scale(100, 80);  
        setImage(image);  
    }
    
    public void removeLive()    
    {    
        int lives = getWorld().getObjects(heart.class).size();    
        getWorld().removeObject((Actor)getWorld().getObjects(heart.class).get(lives-1));  

        if (lives != 1)  
        {  
            //
        }  
        else  
        {     
            Greenfoot.setWorld(new GameOver());
        }  
    }   
    
    public boolean isGeraakt()
   {
     Actor barrel = getOneObjectAtOffset(0,0,barrel.class);
     Actor Bomb = getOneObjectAtOffset(0,0,Bomb.class);
       boolean raak = false;

     if ( Bomb !=null || barrel !=null )
     {
      raak = true;
      
     }
     return raak;
   }
    
   public void removeHealth()
   {
     BotenUpgrade subWorld = (BotenUpgrade) getWorld();   
     heart heartObject = (heart) subWorld.getObjects(heart.class).get(0);   
     heartObject.adjustValue(-1);    
       
     if (heartObject.getValue() == 0) removeLive();  
     
   } 

} 
So we could remove the 'heart' 'barrel' and 'Bomb' actors. Cause the collision check is now in the 'boot' actor right?
This may be, but (1) the upgrade has not even been added yet;
So i can comment the upgrade for now? or remove the whole line?
public void getShield()
    {
       Score score = ((MenuWorld)getWorld()).botenUpgrade.getScoreObject();  
       boot ship = ((MenuWorld)getWorld()).botenUpgrade.getBootObject();  
       //UpgradeBoot upgrade = ((MenuWorld)getWorld()).botenUpgrade.getUpgradeBootObject();
        if (Greenfoot.mouseClicked(this) && score.getValue() >= 500)  
        {                
            //             
        }  
    }  
danpost danpost

2014/1/19

#
Aaron91 wrote...
So we could remove the 'heart' 'barrel' and 'Bomb' actors. Cause the collision check is now in the 'boot' actor right?
It would appear so.
So i can comment the upgrade for now? or remove the whole line?
Again, that depends on how you want to qualify the purchasing of the shield. If you do not wish to add any further conditions on it, then remove it. If you are not sure, comment it out.
Aaron91 Aaron91

2014/1/19

#
I was not saying that one upgrade would override a different kind of upgrade. I was saying that, let us say that an upgrade for shield was purchased and before it was removed (still had time on it) the MenuWorld was again brought up and the upgrade for a shield was again attempted. Should it, or should it not, be allowed; and, if so, how would the upgrade be applied.
Lets say that if the upgrade was purchased and when theres still time on the upgrade. You can purchase it again and it gets back its full time. This is sort of what iam looking for.
danpost danpost

2014/1/19

#
Then it would not matter if one is in the world or not; so you can remove the upgade line.
Aaron91 Aaron91

2014/1/19

#
Ok removed it. And the getShield method can now acces the score and boot actor. But iam still clueless about changing the actor. Super_Hippo mentioned a boolean in my 'boot' actor to change the image. But how must i call this other image to replace the current 'boot' actor?
danpost danpost

2014/1/20

#
Use the Actor class method 'setImage'. You will need more than just a boolean; you will need an int timer field as well. In fact you only need the int timer field. Add an instance field to the boot class -- call it 'shieldTimer'. If its value is greater than zero, decrement its value, check to see if it has become zero, and, if it has, change the image back to the normal one. To initiate the shield, change the image and set 'shieldTimer' to an appropriate value (approx. 60 times the number of seconds you with the shield to be active. You will need two methods -- one for the initial shield setting (actually, this is the only one required) and another for checking the active state of the shield:
private boolean isShieldActive()
{
    return shieldTimer > 0;
}
I guess this could be considered the boolean field that was previously mentioned; however, using this method is a bit better. (1) one less instance field (2) the current state is always returned regardless of the value of a field which may or may not reflect the current state (a fields requires that its value be kept current where the current state is always current). You might think that we are still using a field (the 'shieldTimer' field) for this; but it is the only field and the state is already dependent on it. The other method should be public and called from the MenuWorld actor that receives the click to purchase a shield at the code location where the shield is actually purchased (after verified that actor was clicked on and enough score has been accumulated to make the purchase).
danpost danpost

2014/1/20

#
Oh, I said the second method (the one given above) was not required. Instead of calling this method, you could put the condition that is within the method in place of calling it. In other words:
// instead of this:
if (isShieldActive())
// you could use this:
if (shieldTimer > 0)
Aaron91 Aaron91

2014/1/20

#
Oke ill come back later to the ShieldTimer. I need to do the set image first. I placed this boolean in my Actor. and the second piece of code in my world. But when i click on it, the 'UpgradeBoot' isnt appearing.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Shield here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shield extends Actor
{
    /**
     * Act - do whatever the Shield wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public static boolean shield = false;
    public int teller = 0;
 
   
    public void act() 
    {
        
   
        
       imgResize();
       getShield();
    }    
   
    
    public void imgResize()  
    {      
        GreenfootImage image = getImage();    
        image.scale(70, 80);  
        setImage(image);  
    }
    
    public void getShield()
    {
       Score score = ((MenuWorld)getWorld()).botenUpgrade.getScoreObject();  
       boot ship = ((MenuWorld)getWorld()).botenUpgrade.getBootObject();  
        if (Greenfoot.mouseClicked(this) && score.getValue() >= 500)  
        {                
          teller++;
           shield = true;
           
           if(teller == 2)
           {
               teller = 0;
               shield = false;
           }          
        }  
    }  
}
public BotenUpgrade()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(760, 760, 1); 

        GreenfootImage background = new GreenfootImage(760, 760);
        scrollingImage = getScrollingImage(760, 760);
        background.drawImage(scrollingImage, 500, 500);
        setBackground(background);
        
        
        
         if(Shield.shield)
        {
            addObject(new UpgradeBoot(), 350, 600);
            removeObjects(getObjectsAt(350,600, boot.class));
        }
        else
        {
            addObject(new boot(), 350, 600);
        }
}
danpost danpost

2014/1/20

#
I do not think the boolean was intended to be in the Shield class, but in the boot class to signify whether the upgrade was active or not. Also, the code you added to the World constructor, I am not sure what that is!
There are more replies on the next page.
3
4
5
6
7