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

2014/1/10

limit my actors movement

2
3
4
5
6
7
Aaron91 Aaron91

2014/1/18

#
But my restoreHealth method is an placed in my 'RestoreHeart' actor. I think i mentioned that somewhere earlier. I get the same error as before again, but then its refering to getScoreObjects method. I also tried putting this whole method in my 'MenuWorld' world, but it also gives that same error. non-static method getScoreObjects(java.lang.Class) cannot be referenced from a static context? So what i changed in my code is; I placed the the getScoreObject and getHeartObject in my 'BotenUpgrade' and tried to place the restoreHealth in my 'RestoreHeart' actor like this, but it wont compile though.
public void restoreHealth()
    {
        {  
  
        Score score = BotenUpgrade.getScoreObject();  
        heart hertz = BotenUpgrade.getHeartObject();  
        if (Greenfoot.mouseClicked(this) && score.getValue() >= 200 && hertz.getValue() < 3)  
        {    
         hertz.adjustValue(+1);    
         //score.getValue(-200);              
        }  
      }  
    }
danpost danpost

2014/1/18

#
I think I understand. How about this:
public void restoreHealth()
{
    Score score = ((MenuWorld)getWorld()).botenUpgrade.getScoreObject();
    heart hertz = ((MenuWorld)getWorld()).botenUpgrade.getHeartObject();
    // rest of method
What these lines do is: (1) get a reference to the world the RestoreHeart actor is in (typecast as 'MenuWorld' because that is the class the following field is in -- it is not in the World class, which is the type of object 'getWorld' returns) (2) gets the reference to the BotenUpgrade object stored in the 'botenUpgrade' field (3) calls the methods of that BotenUpgrade object
Aaron91 Aaron91

2014/1/18

#
Thanks it worked perfectly! for my next 'upgrade' i wanted to add some invinciblity for my 'boot' actor. But this 1 is a bit more complicated. this 'shield' upgrade works the same as the previous 'restoreHealth' method'. But if i click on it, i want to replace the current main actor with another sprite + no damage. I have no idea how i can replace the actor with the current 1, but the invincibility should be managable. The only thing i struggle with if i do the invincibility is calling the method from another actor. like the following. How can i acces the 'isGeraakt' method in the 'Bomb' or 'barrel' actor. (the Bomb and barrel actor are pretty much the same).
public void getShield()
    {
       Score score = ((MenuWorld)getWorld()).botenUpgrade.getScoreObject();  
       heart hertz = ((MenuWorld)getWorld()).botenUpgrade.getHeartObject();
       barrel ton = ((MenuWorld)getWorld()).botenUpgrade.getBarrelObject();
       Bomb bomb = ((MenuWorld)getWorld()).botenUpgrade.getBombObject();
        if (Greenfoot.mouseClicked(this) && score.getValue() >= 0)  
        {    
            if (isGeraakt()) //if hit
            {
               raak = true;
            }
         score.getValue(-200);              
        }  
      }  
Heres the 'barrel' actor code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class barrel here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class barrel extends Actor
{
    
     /* Act - do whatever the barrel wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.*/
    
        
   public void act()
   
   {  
    if(isGeraakt())    
    {    
        removeHealth();  
        getWorld().removeObject(this);  
        return;         
           
    }    
    int x = Greenfoot.getRandomNumber(2);    
    move(-x);    
    if(atWorldEdge()) getWorld().removeObject(this);    
    
    setSpeed();
    imgResize();
   }  
   
    public void setSpeed()
     {
     Greenfoot.setSpeed(60);  
    }
    
   public void removeHealth()
   {
     BotenUpgrade subWorld = (BotenUpgrade) getWorld();   
     heart heartObject = (heart) subWorld.getObjects(heart.class).get(0);   
     heartObject.adjustValue(-1);    
       
     if (heartObject.getValue() == 0) removeLive();  
     
   } 
  
   public boolean isGeraakt()
   {
     boolean raak = false;
     Actor r = getOneObjectAtOffset(0,0,boot.class);
     if ( r !=null)
     {
      raak = true;
     }
     return raak;
   }
       
   public boolean atWorldEdge()  
   {  
     return getX() == 0 ||   
               getX() == getWorld().getWidth() - 1 ||  
               getY() == 0 ||
               getY() == getWorld().getHeight() - 1;  
   } 
   
   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 void imgResize()  
    {      
        GreenfootImage image = getImage();    
        image.scale(60, 50);  
        setImage(image);  
    }
    
 }
danpost danpost

2014/1/18

#
Use ''ton' to run the method on.
Aaron91 Aaron91

2014/1/18

#
I tried placing the 'ton' in the second if trying to reach the 'isGeraakt' method, but it wont allow me to. Trying to reach 'isGeraakt' cause i want the raak to be 'true' while the shield is on.
danpost danpost

2014/1/18

#
Need to go. Will get back later (if no one has helped in the meantime).
Aaron91 Aaron91

2014/1/18

#
Thanks danpost... hope you can help me out with the upgrades .. its the last part i need for my game ... if i had more time i would do more research in the methodes myself, but the deadline of my game is in 2 days ... so i hope i can rely on you a bit aswell.. gonna try and look into this as far as i can go at the moment..
Super_Hippo Super_Hippo

2014/1/18

#
If the deadline is in two days, then you should not put all your hope into one person. ;) Okay, back to you scenario. If I understand you correctly, then you want to call the method isGeraakt() from another class which is not a subclass of the class. This is your method:
public boolean isGeraakt()
{
     boolean raak = false;
     Actor r = getOneObjectAtOffset(0,0,boot.class);
     if ( r !=null)
     {
          raak = true;
     }
     return raak;
}
I think it would be easier if you have the boolean raak as a 'public boolean raak' at the top. Then you could just access the variable instead of the method. It's not very easy for me to follow you though. Which class wants to call this method and wants to know whether or not the bomb/barrel is 'geraakt'? If you hit a bomb/barrel, you lose a life. If you click on the object with the getShield() method, you have line 9 to 12. Why is this there, what do you want to do? Giving more information is always positive and makes it easier to understand your thinking and to actually help you. Some other things: I don't think that it is very useful to set the executing speed in Greenfoot and scale the image of the actor in every act cycle of the class. It seems useless. (You will also probably get a NullPointerException, when it reaches the end of the world.) You can scale the image once in the constructor and set the speed in the world class. The barrel probably moves a bit ugly with this, because it either does one step or stands still.
Aaron91 Aaron91

2014/1/19

#
Thanks for replieing Super_Hippo. Ill give a short breakdown of my game. It has 3 worlds, 1 irrelevant (the game over world) my main world 'BotenUpgrade'. And my 'MenuWorld' world. In my 'BotenUpgrade' world moves my main actor 'boot' and needs to avoid the enemy actors 'barrel' and 'Bomb'. In my 'MenuWorld' i stored the so called upgrades for my 'boot' actor. There are 3 upgrades i want to create for my game. The 'RestoreHeart' actor(this lets me restore my 'heart' actor if it has les than 3), my 'HeartUpgrade' (i want this to increase the current 3 'hearts' by one) and the 'Shield' upgrade (this makes me lets say invincible for a short period of time). I got the 'RestoreHeart' sort of going for me now. The only thing missing for now is that it doesnt decrease my score cause i want the upgrades to be purchasable. The upgrades is where i am at the moment. WIth code underneat(line 9 to 12) i try to let my 'boot' hit the objects hoping there wont be any 'heart' loss (i dont think its the right mindset to do this, but i gave it a shot).
public void getShield()  
    {  
       Score score = ((MenuWorld)getWorld()).botenUpgrade.getScoreObject();    
       heart hertz = ((MenuWorld)getWorld()).botenUpgrade.getHeartObject();  
       barrel ton = ((MenuWorld)getWorld()).botenUpgrade.getBarrelObject();  
       Bomb bomb = ((MenuWorld)getWorld()).botenUpgrade.getBombObject();  
        if (Greenfoot.mouseClicked(this) && score.getValue() >= 0)    
        {      
            if (isGeraakt()) //if hit  
            {  
               raak = true;  
            }  
         score.getValue(-200);                
        }    
      }    
Some other things: I don't think that it is very useful to set the executing speed in Greenfoot and scale the image of the actor in every act cycle of the class. It seems useless. (You will also probably get a NullPointerException, when it reaches the end of the world.) You can scale the image once in the constructor and set the speed in the world class. The barrel probably moves a bit ugly with this, because it either does one step or stands still.
This is the least of my concern at the moment cause the upgrades are giving me thoughts where iam about to get pack for an asylum. If you got any questions ill give you what anything you need to clear this up.
danpost danpost

2014/1/19

#
Again, the name of the method needs changed (this method does not appear to be getting a Shield object or adding one to your boot actor). Also, if this method is for an actor that resides in your MenuWorld world, then your boot object should not be checked for any collisions (that world is inactive at the moment because the MenuWorld is the active world right now). Basically, if this is to give boot a special ability that is bought, then the only conditions should be whether there is enough score to get the upgrade clicked on (and maybe whether that upgrade is already present and/or active). Then, if there is, subtract the cost from the score and add the upgrade. Basically, what I do not understand is why isGaraakt is being checked to purchase an upgrade.
Aaron91 Aaron91

2014/1/19

#
Iam trying to do this piece by piece really. I already got the sprite ready i want to change the 'boot' actor to, but iam trying to make 'boot' invincible first. Trying to reach 'getGeraakt' method cause this is the part where the 'barrel and 'Bomb' (enemy actors) check if the 'boot' hits them. Or am i thinking wrong here?
Super_Hippo Super_Hippo

2014/1/19

#
So the boot actor is the playable character in which this getShield() method is, right? Then you could also check whether or not it is hit by a bomb/barrel in the boot class. Maybe that would make things easier.
Aaron91 Aaron91

2014/1/19

#
Yea my 'boot' actor is the playable character who gets these upgrades, but I didnt thought i would have more then 1 enemy. The Bomb actor was something i added later in the process. I didnt think this trough while making the enemy actor, so i just placed the hit method there. But now that i got 2 enemys, i made things a bit more complex for myself and i cant pull this off at the moment. i already dropped the 'HeartUpgrade' upgrade cause after the upgrades i noticed some small things that needs to be fixed aswell (+ the bugs i probably forgot for now). But the upgrades is my main concern at the moment.
danpost danpost

2014/1/19

#
Alright, let us get this straight. The 'getShield' is a method in the class of an actor that is placed in the 'MenuWorld' world; Is this correct?
Aaron91 Aaron91

2014/1/19

#
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. Placed this piece in my 'BotenUpgrade' world for the 'UpgradeBoot' actor.
public UpgradeBoot getUpgradeBootObject()  
   {  
    return (UpgradeBoot)getObjects(UpgradeBoot.class).get(0);  
   } 
And this is in my 'Shield' actor at the moment. Removed the isGeraakt method assuming it didnt make sense.
public void getShield()
    {
       Score score = ((MenuWorld)getWorld()).botenUpgrade.getScoreObject();  
       heart hertz = ((MenuWorld)getWorld()).botenUpgrade.getHeartObject();
       barrel ton = ((MenuWorld)getWorld()).botenUpgrade.getBarrelObject();
       Bomb bomb = ((MenuWorld)getWorld()).botenUpgrade.getBombObject();
       UpgradeBoot upgrade = ((MenuWorld)getWorld()).botenUpgrade.getUpgradeBootObject();
        if (Greenfoot.mouseClicked(this) && score.getValue() >= 500)  
        {                
            //             
        }  
    } 
There are more replies on the next page.
2
3
4
5
6
7